I need to change/customize a specific order item meta data for WooCommerce email notifications, but I can't find a solution.
I found one, but it is for unsetting unneeded order item meta data from Woocommerce email notifications.
Can the mentioned code can be adapted for changing meta data for emails?
I tried with $formatted_meta[$key] = 'new value';
but that does not give the desired result. Any advice?
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'unset_specific_order_item_meta_data', 10, 2);
function unset_specific_order_item_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] = 'new value';
}
return $formatted_meta;
}
CodePudding user response:
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