-
-
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; | |
} |
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
'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
This code is wc_doing_it_wrong
as of version 3.0, and should be $order->get_id()
instead of $order->id
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.