douxian5076 2017-03-30 11:08
浏览 62
已采纳

在电子邮件通知上显示自定义结帐帐单字段

In WooCommerce, I want to add some custom fields on checkout which will be displayed under the billing section on the e-mail confirmation. My custom fields and their values, shown on the checkout form and on the order page on back end (WooCommerce -> Orders). So far everything works great.

The problem is that the e-mail that i receive does not contain the custom fields and their values.

Code shown below:

    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    function custom_override_checkout_fields( $fields ) {
        $fields['billing']['billing_field_testing'] = array(
            'label'     => __('TestingField', 'woocommerce'),
            'placeholder'   => _x('TestingField', 'placeholder', 'woocommerce'),
            'required'  => false,
            'class'     => array('form-row-wide'),
            'clear'     => true
             );
        return $fields;
    }

    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
    function my_custom_checkout_field_display_admin_order_meta($order)
    {
        echo '<p><strong>'.__('TestingField').':</strong> ' . get_post_meta( $order->id, '_billing_field_testing', true ) . '</p>';
    }

Please help.

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dpy3846 2017-03-30 11:38
    关注

    Updated: To display your custom checkout field in email notifications below the billing address, use this function hooked in woocommerce_email_customer_details action hook with a priority above 20:

    add_action('woocommerce_email_customer_details','add_custom_checkout_field_to_emails_notifications', 25, 4 );
    function add_custom_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
    
        $output = '';
        $billing_field_testing = get_post_meta( $order->id, '_billing_field_testing', true );
    
        if ( !empty($billing_field_testing) )
            $output .= '<div><strong>' . __( "Some text:", "woocommerce" ) . '</strong> <span class="text">' . $billing_field_testing . '</span></div>';
    
        echo $output;
    }
    

    The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?