In the order email templates (for example email-order-items.php
), WooCommerce uses the function wc_display_item_meta
to display product details in the order table. The function code is present in the wc-template-functions.php
file (line number 3011). I am copying the function code below for reference
function wc_display_item_meta( $item, $args = array() ) { $strings = array(); $html = ''; $args = wp_parse_args( $args, array( 'before' => '<ul class="wc-item-meta"><li>', 'after' => '</li></ul>', 'separator' => '</li><li>', 'echo' => true, 'autop' => false, ) ); foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) { $value = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( trim( $meta->display_value ) ) ); $strings[] = '<strong class="wc-item-meta-label">' . wp_kses_post( $meta->display_key ) . ':</strong> ' . $value; } if ( $strings ) { $html = $args['before'] . implode( $args['separator'], $strings ) . $args['after']; } $html = apply_filters( 'woocommerce_display_item_meta', $html, $item, $args ); if ( $args['echo'] ) { echo $html; // WPCS: XSS ok. } else { return $html; } }
The problem is: it doesn’t take any arguments that can help me filter out item data that I don’t want to show in the order email. I don’t want to change this function in the wc-template-functions.php
as it’s a core file. So, I want to know if there’s a piece of code that I can add to functions.php
that’ll somehow modify this wc_display_item_meta
function to filter out specific item meta.
Note: I know someone might suggest why not just remove that particular item data from the product details, but that data is essential to internal order processing. I just don’t want it to show to the customers.
Update #1: What meta data I don’t want to show in the order email? Below is a screenshot of an order email. I have highlighted three item data..”Qty Selector”, “Qty” and “Total”. I want all these three to not show in the order email.
Advertisement
Answer
Try the following without any guarantee (as I don’t really have the real necessary keys):
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 ){ if( in_array( $meta->key, array('Qty Selector', 'Qty', 'Total') ) ) unset($formatted_meta[$key]); } return $formatted_meta; }
Code goes in function.php file of your active child theme (active theme). Tested with other meta data than yours and works. I hope it will work for you too.
Now, the hook used with this code is the right filter hook. It’s located in the
WC_Order_Item
methodget_formatted_meta_data()
and allows to filter the order item meta data.