I installed Checkout Field Editor on my wordpress site. I created the custom field. However, using the following code, the pcustomer field appears both in the "new order" email that arrives to me and to the customer. Instead I want it to come exclusively to me. I tried to edit the code, but it still doesn't work.
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;
}
Since WooCommerce 3, your code is a bit outdated, with some mistakes Try the following:
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 ) {
$meta_key = '_meta_key1'; // <= Here define the correct meta key
$meta_value = $order->get_meta( $meta_key1 );
if ( ! empty( $meta_value1 ) ) {
$fields[ $meta_key1 ] = array(
'label' => __( 'My label 1', "text-domain' ),
'value' => $meta_value1,
);
}
return $fields;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Now to restrict the code only to admin email notifications, you will use $sent_to_admin
argument variable as follows:
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 ) {
if ( $sent_to_admin ) {
$meta_key1 = '_meta_key1'; // <= Here define the correct meta key
$meta_value1 = $order->get_meta( $meta_key1 );
if ( ! empty( $meta_value1 ) ) {
$fields[ $meta_key1 ] = array(
'label' => __( 'My label 1', "text-domain' ),
'value' => $meta_value1,
);
}
}
return $fields;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
This time this custom field will not be displayed on customer email notifications.
Note:
$order->id
is uncorrect since WooCommerce 3. use instead $order->get_id()
.get_post_meta()
function can be replace by WooCommerce WC_Data
get_meta()
method.