Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Customizing checkout fields using actions and filters] Add custom WooCommerce checkout field to emails
/* To use:
1. Add this snippet to your theme's functions.php file
2. Change the meta key names in the snippet
3. Create a custom field in the order post - e.g. key = "Tracking Code" value = abcdefg
4. When next updating the status, or during any other event which emails the user, they will see this field in their email
*/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_order_meta_keys');
function my_custom_order_meta_keys( $keys ) {
$keys[] = 'Tracking Code'; // This will look for a custom field called 'Tracking Code' and add it to emails
return $keys;
}
@accessDatabase

This comment has been minimized.

accessDatabase commented Oct 31, 2018

Hello, I am struggling to get this to work. I am trying to get a shipping contact telephone number to show as the last row of the shipping address in the order confirmation emails.

I have added a custom field called 'shipping_phone' using this in functions.php:
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_phone'] = array(
'label' => __('Phone', 'woocommerce'),
'placeholder' => _x('Phone', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);

 return $fields;

}

/**

  • Display field value on the order edit page
    */

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
echo '

'.__('Phone From Checkout Form').': ' . get_post_meta( $order->id, '_shipping_phone', true ) . '

';
}

When I then try to use the code above by changing 'Tracking Code' for 'shipping_phone' it isn't showing in the confirmation emails.

add_filter('woocommerce_email_order_meta_keys', 'my_custom_order_meta_keys');

function my_custom_order_meta_keys( $keys ) {
$keys[] = 'shipping_phone'; // This will look for a custom field called 'Tracking Code' and add it to emails
return $keys;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment