Reputation: 95
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
Upvotes: 2
Views: 4882
Reputation: 254373
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.
Upvotes: 5
Reputation: 1078
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;
}
Upvotes: 0