Display Custom Order Meta Data Value in Email Notifications Woocommerce

Display custom order meta data value in email notifications WooCommerce

You have some minor mistakes, via the if condition "$email->id == ..."
you can target the mails

How to target other WooCommerce order emails

  • 'customer_completed_order'
  • 'customer_processing_order'
  • 'customer_on_hold_order'
  • 'customer_refunded_order'
  • 'customer_reset_password'
  • 'customer_invoice'
  • 'customer_new_account'
  • 'customer_note'
  • 'cancelled_order'
  • 'failed_order'
  • 'new_order'


function woocommerce_email_order_invoice_number( $order, $sent_to_admin, $plain_text, $email ) {
// For 'new order'
if ( $email->id == 'new_order' ) {

// Get post meta
$my_field_name = get_post_meta( $order->get_id(), 'my_field_name', true );

// True and equal to
if ( $my_field_name && $my_field_name == 1 ) {
echo '<p><strong>My custom field: </strong> <span style="color:red;">Is enabled</span></p>';
}
}
}
add_action( 'woocommerce_email_customer_details', 'woocommerce_email_order_invoice_number', 20, 4 );

Changing the order item meta data in WooCommerce email notifications

If you want to adjust these values ​​via the current hook you are using, you can do it like this:

function filter_woocommerce_order_item_get_formatted_meta_data( $formatted_meta, $item ) {
// Only on emails notifications
if ( is_admin() || is_wc_endpoint_url() )
return $formatted_meta;

foreach ( $formatted_meta as $key => $meta ) {
$formatted_meta[$key]->display_key = 'new key';
$formatted_meta[$key]->display_value = 'new value';
}

return $formatted_meta;
}
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'filter_woocommerce_order_item_get_formatted_meta_data', 10, 2 );

However, know that instead of using the above hook the woocommerce_order_item_display_meta_key & woocommerce_order_item_display_meta_value filter hooks are better suited:

function filter_woocommerce_order_item_display_meta_key( $display_key, $meta, $item ) {
// Only on emails notifications
if ( is_admin() || is_wc_endpoint_url() )
return $display_key;

// Display key
$display_key = 'My new key';

return $display_key;
}
add_filter( 'woocommerce_order_item_display_meta_key', 'filter_woocommerce_order_item_display_meta_key', 10, 3 );

function filter_woocommerce_order_item_display_meta_value( $display_value, $meta, $item ) {
// Only on emails notifications
if ( is_admin() || is_wc_endpoint_url() )
return $display_value;

// Display value
$display_value = 'My new value';

return $display_value;
}
add_filter( 'woocommerce_order_item_display_meta_value', 'filter_woocommerce_order_item_display_meta_value', 10, 3 );

Then it is a matter of determining via if conditions which adjustments are specifically concerned, since these are currently generally applied via my answer

Display custom order meta data value in email notifications

You can use the following to display your custom field "Invoice Number" on email notifications:

add_action('woocommerce_email_order_details', 'woocommerce_email_order_invoice_number', 4, 4 );
function woocommerce_email_order_invoice_number( $order, $sent_to_admin, $plain_text, $email ) {
if( $value = get_post_meta( $order->get_id(), '_billing_options', true ) )
echo '<p><strong>'.__('Invoice Number').':</strong> '.$value.'</p>';
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

Hide item meta data in certain WooCommerce email notifications

You could use the woocommerce_display_item_meta hook and return an empty string

function filter_woocommerce_display_item_meta ( $html, $item, $args ) {
$html = '';

return $html;
}
add_filter( 'woocommerce_display_item_meta', 'filter_woocommerce_display_item_meta', 10, 3 );

While the above would work, there would be some issues, namely:

  • The hook doesn't run just for email notifications, so it wouldn't show up anywhere
  • Even if this hook would only be executed for email notifications, we would still need to specify that this should only be the case for certain email notifications. However, this hook does not offer a solution for it by default to make this distinction

So a workaround will be needed, this can be done by creating a global variable through another hook that applies only to email notifications

Step 1) creating and adding a global variable

// Setting global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_id'] = $email->id;
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );

Step 2) In the hook woocommerce_display_item_meta, add and check for specific conditions

  • Only for email notifications
  • Only for specific meta data
  • Only for admin 'new order' email
function filter_woocommerce_display_item_meta ( $html, $item, $args ) {
// For email notifications and specific meta
if ( ! is_wc_endpoint_url() && $item->is_type( 'line_item' ) && $item->get_meta( 'Supplier SKU' ) ) {
// Getting the email ID global variable
$ref_name_globals_var = isset( $GLOBALS ) ? $GLOBALS : '';
$email_id = isset( $ref_name_globals_var['email_id'] ) ? $ref_name_globals_var['email_id'] : '';

// NOT empty and targeting specific email. Multiple statuses can be added, separated by a comma
if ( ! empty ( $email_id ) && ! in_array( $email_id, array( 'new_order' ) ) ) {
$html = '';
}
}

return $html;
}
add_filter( 'woocommerce_display_item_meta', 'filter_woocommerce_display_item_meta', 10, 3 );

Change sender name to value from order meta data in WooCommerce email notifications

It is not necessary to add this filter in the woocommerce_order_status_processing hook, when the order status changes to processing. Because we can apply the reverse. Via $email->id it is possible to target specific email notifications when this hook is triggered.

To obtain meta data belonging to the order, you can use $order->get_meta( 'meta_key' )

So you get:

function filter_woocommerce_email_from_name( $from_name, $email ) {
// Targeting specific email notification via the email id
if ( $email->id == 'customer_processing_order' && is_a( $email->object, 'WC_Order' ) ) {
// Get order
$order = $email->object;

// Get meta
$business_name = $order->get_meta( 'businessname' );

// NOT empty
if ( ! empty ( $business_name ) ) {
$from_name = $business_name;
}
}

return $from_name;
}
add_filter( 'woocommerce_email_from_name', 'filter_woocommerce_email_from_name', 10, 2 );

Display product custom field via woocommerce_email_order_meta hook in WooCommerce admin email notification

The woocommerce_email_order_meta hook has 4 arguments, via $email->id you can target specific email notifications

So you get:

function action_woocommerce_email_order_meta( $order, $sent_to_admin, $plain_text, $email ) {
// Targetting specific email notifications
$email_ids = array( 'new_order' );

// Checks if a value exists in an array
if ( in_array( $email->id, $email_ids ) ) {
// Get items
$items = $order->get_items();

// Loop trough
foreach ( $items as $item ) {
// Get meta
$depot = $item->get_meta( 'depot' );

// NOT empty
if ( ! empty ( $depot ) ) {
echo '<p style="color:green;font-size:50px;">Dépôt: ' . $depot . ' - ' . $item->get_name() . '</p>';
} else {
echo '<p style="color:red;font-size:50px;">Data not found!</p>';
}
}
}
}
add_action( 'woocommerce_email_order_meta', 'action_woocommerce_email_order_meta', 10, 4 );

How to show WooCommerce custom product meta in new order emails?

Try the following instead:

add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 );
function email_confirmation_display_order_items( $item_id, $item, $order ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
$payment_terms = get_post_meta( $item->get_product_id(), 'payment_terms', true );

if ( ! empty($payment_terms) ) {
printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $payment_terms );
}
}
}

Code goes in functions.php file of your active child theme (or active theme). It should works.

Related: WooCommerce Display avanced custom fields (ACF) inside order notification



Related Topics



Leave a reply



Submit