How to Add Custom Fields (in order) to Email

You can add a custom field to your order email by specifying the name of the custom order. This can be useful when the checkout field editor is used in combination with the extension, or if you want to include things like 'transaction key' with paypal commands, for example

Copy the following code into your theme functions.php, then go to WooCommerce> Checkout fields to add your newly created custom fields.

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;

}


Here's an example using the checkout field editor extension of WooCommerce> Checkout field settings for a custom field called 'hear_about_us':




Using this example would be the code:

$fields['How did you hear about us?'] = 'hear_about_us';

Comments