在电子邮件通知上显示自定义结帐帐单字段
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
在WooCommerce中,我想在结帐时添加一些自定义字段,这些字段将显示在结算 strong>电子邮件确认部分。
我的自定义字段及其值,显示在结帐表单和后端的订单页面上(WooCommerce - &gt;订单)。 到目前为止一切都很好。 p>
问题是我收到的电子邮件不包含自定义字段及其值。 strong> p> \ n
代码如下所示: p>
请帮忙。 p>
提前致谢 p> \ n div>
add_filter('woocommerce_checkout_fields','custom_override_checkout_fields');
函数custom_override_checkout_fields($ fields){
$ fields [' billing'] ['billing_field_testing'] =数组(
'标签'=&gt; __('TestingField','woocommerce'),
'占位符'=&gt; _x('TestingField','placeholder','woocommerce '),
'required'=&gt; false,
'class'=&gt; array('form-row-wide'),
'clear'=&gt; true
);
返回$ fields ;
}
add_action('woocommerce_admin_order_data_after_billing_address','my_custom_checkout_field_display_admin_order_meta',10,1);
函数my_custom_checkout_field_display_admin_order_meta($ order)
{
echo'&lt; p&gt;&lt; strong&gt;'.__( '测试 ingField ')。':其中/强&GT; '。 get_post_meta($ order-&gt; id,'_ billing_field_testing',true)。 '&lt; / p&gt;';
}
code> pre>
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.
You will need to customize email templates under woocommerce/templates/emails/ to your-theme/woocommerce/emails/
Youc an override whatever template you want end add custom field there.
Alternatively, you can also add them using hook from following code:
add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
$fields['meta_key'] = array(
'label' => __( 'Label' ),
'value' => get_post_meta( $order->id, 'meta_key', true ),
);
return $fields;
}