Recently, I asked, and received the answers for two solutions:
of some order items. Solutions that will work only for WooCommerce email notifications sent on completed orders.
The problem with the received solutions is that in both, to limit them only to email notifications, the is_wc_endpoint_url()
function is used, which, in my particular case (I don't know why!), makes them to work only for notifications sent to administrators, but not to customers, who are in fact the main recipients of the metadata in question.
Is there any way to around this issue with the is_wc_endpoint_url()
function?
The first answer, adding metadata:
function action_woocommerce_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
// On email notifications
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
echo '<ul ><li><strong >Label</strong><p>Value order id = ' . $order->get_id() . '</p></li></ul>';
}
}
add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10, 4 );
The second answer (one of the options), changing metadata:
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 );
CodePudding user response:
An alternative to using is_wc_endpoint_url()
for WooCommerce email notifications can be done by applying a global variable
To determine if it is a WooCommerce email notification, we can create a global variable through a hook (woocommerce_email_before_order_table
) which only applies in WooCommerce email notifications
// Setting global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_data'] = array(
'email_id' => $email->id, // The email ID (to target specific email notification)
'is_email' => true // When it concerns a WooCommerce email notification
);
}
add_action('woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );
This can be further extended as desired
Then we can check in other hooks, in this specific case the woocommerce_order_item_meta_start
hook (but it can be applied in any WooCommerce email notification related hook) to see if that global variable is available, and if it is, we can perform some actions based on that.
- Example 1: is it an email notification?
- Example 2: use the
$email->id
to target specific email notifications in case the email id is not already passed by default to the desired hook
function action_woocommerce_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
// Getting the custom 'email_data' global variable
$ref_name_globals_var = $GLOBALS;
// Isset & NOT empty
if ( isset ( $ref_name_globals_var ) && ! empty( $ref_name_globals_var ) ) {
// Isset
$email_data = isset( $ref_name_globals_var['email_data'] ) ? $ref_name_globals_var['email_data'] : '';
// NOT empty
if ( ! empty( $email_data ) ) {
// Example 1
// When true
if ( $email_data['is_email'] ) {
echo '<p style="color: red; font-size: 30px;">This is a WooCommerce email notification</p>';
}
// Example 2
// Add the desired email IDs
$target_emails = array( 'customer_processing_order', 'customer_on_hold_order', 'customer_refunded_order' );
// Target specific WooCommerce email notifications
if ( in_array( $email_data['email_id'], $target_emails ) ) {
echo '<p style="color: red; font-size: 30px;">Email ID = ' . $email_data['email_id'] . '</p>';
}
}
}
}
add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10, 4 );
Used in this answer: