Show Custom Order Fields in Order Emails
Adds the order details you choose, such as a delivery date or gift message saved by another plugin, to WooCommerce's order emails with friendly labels.
Opens WPIDE on
Code
$wpide_email_meta = array( // Meta key => array( label, admin only ).
'_delivery_date' => array( 'Delivery date', false ),
'_gift_message' => array( 'Gift message', false ),
'_source' => array( 'Order source', true ),
);
add_filter( 'woocommerce_email_order_meta_fields', function ( $fields, $sent_to_admin, $order ) use ( $wpide_email_meta ) {
foreach ( $wpide_email_meta as $key => $config ) {
$value = $order->get_meta( $key );
if ( '' === $value || is_array( $value ) || ( $config[1] && ! $sent_to_admin ) ) {
continue;
}
$fields[ 'wpide_' . sanitize_key( $key ) ] = array(
'label' => $config[0],
'value' => wp_strip_all_tags( (string) $value ),
);
}
return $fields;
}, 10, 3 );
Notes
List meta keys and labels in $wpide_email_meta, and set the second value to true for details only the shop should see.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.