Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Created August 11, 2015 22:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikejolley/8f22c991e80124114d6b to your computer and use it in GitHub Desktop.
Save mikejolley/8f22c991e80124114d6b to your computer and use it in GitHub Desktop.
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;
}
@javi2ruiz
Copy link

Hello. I have used a code based on your to add a custom field named "billing_nif" on customer datails fields list.
I have added my cuscom field with woocommerce plugin Checkout Field Editor.
It's very important to add a "_" when call to function "get_post_meta".

add_filter( 'woocommerce_email_customer_details_fields', 'custom_woocommerce_email_customer_details_fields', 10, 3 );
function custom_woocommerce_email_customer_details_fields( $fields, $sent_to_admin, $order ) {
$fields['billing_nif'] = array(
'label' => __( 'NIF' ),
'value' => get_post_meta( $order->id, '_billing_nif', true ),
);
return $fields;
}
Thanks.

@Garconis
Copy link

Garconis commented Sep 5, 2017

How do you add the custom field to a particular area within the email? For example, I want to display the custom order meta field within the opening text of customer-completed-order.php

@grapevinecs
Copy link

grapevinecs commented Nov 28, 2017

'value' => get_post_meta( $order->id, '_billing_nif', true ),

Doesn't seem to be correct (I tested it on mine), it should be: 'value' => get_post_meta( $order->id, 'billing_nif', true ),

Note to anyone using this, if you've added multiple fields add them like so -

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 ),
);
$fields['meta_key_2'] = array(
'label' => __( 'Label2' ),
'value' => get_post_meta( $order->id, 'meta_key_2', true ),
);
$fields['meta_key_3'] = array(
'label' => __( 'Label3' ),
'value' => get_post_meta( $order->id, 'meta_key_3', true ),
);
return $fields;
}

Cheers

@csalzano
Copy link

This code is wc_doing_it_wrong as of version 3.0, and should be $order->get_id() instead of $order->id

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