From e35c7ac6dcbb8d79beb3c12bcbb07f8312db451e Mon Sep 17 00:00:00 2001 From: Josh Betz Date: Fri, 4 Mar 2022 15:06:43 -0600 Subject: [PATCH] Filter out product variation line_item meta There are cases where we want to display line item meta, similar to the checkout flow on the web. The web filters out variation meta because it's redundant. The product name already includes the relevant meta. ref: https://github.com/woocommerce/woocommerce/blob/8bc310008c4786f3c7a8ecfc99b470accc77c183/plugins/woocommerce/includes/class-wc-order-item.php#L282-L285 --- .../class-wc-rest-orders-v2-controller.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php index 64cc4aa9df6..b80be00ef60 100644 --- a/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php +++ b/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php @@ -210,6 +210,27 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller { // Expand meta_data to include user-friendly values. $formatted_meta_data = $item->get_formatted_meta_data( null, true ); + + // Filter out product variations + // TODO: Add a query arg to activate this. Default should be to keep variation meta for back-compat + if ( $product ) { + $order_item_name = $data['name']; + $data['meta_data'] = array_filter( $data['meta_data'], function( $meta ) use ( $product, $order_item_name ) { + $meta->key = rawurldecode( (string) $meta->key ); + $meta->value = rawurldecode( (string) $meta->value ); + $attribute_key = str_replace( 'attribute_', '', $meta->key ); + $display_key = wc_attribute_label( $attribute_key, $product ); + $display_value = wp_kses_post( $meta->value ); + + // Skip items with values already in the product details area of the product name. + if ( $product && $product->is_type( 'variation' ) && wc_is_attribute_in_product_name( $display_value, $order_item_name ) ) { + return false; + } + + return true; + } ); + } + $data['meta_data'] = array_map( array( $this, 'merge_meta_item_with_formatted_meta_display_attributes' ), $data['meta_data'],