Add a custom field (in an order) to the emails

This is a Developer level doc. If you are unfamiliar with code/templates and resolving potential conflicts, select a WooExpert or Developer for assistance. We are unable to provide support for customizations under our  Support Policy.

You can add any custom field to your order emails by hooking in and specifying the name of the custom field. This can be useful if, for example, you want to include things like the ‘transaction key’ from PayPal orders.

Alternatively, you can add the code snippet using Code Snippet plugin.

View on Github

Here is an example using a custom field that was added with the Checkout Field Editor extension. As of version 1.1.8 the code is no longer required for the extension’s own fields, but the code is still valid for custom fields created in other ways.

In this example, a custom field with the label hear_about_us was added:

WC-example-custom-field

To add the newly added custom field hear_about_us to the order email, the code would look like this:

/**
 * Add a custom field (in an order) to the emails
 */
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['hear_about_us'] = array(
        'label' => __( 'Hear About Us' ),
        'value' => get_post_meta( $order->id, 'hear_about_us', true ),
    );
    return $fields;
}
Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.