Add `order_item_display_meta` query parameter

By default we will show all meta for order items to maintain backwards
compatibility. When this new parameter is set, we will filter out
variation meta just as core does on the web.
This commit is contained in:
Josh Betz 2022-03-17 14:20:17 -05:00
parent e35c7ac6dc
commit bb9feb7d68
1 changed files with 18 additions and 16 deletions

View File

@ -211,24 +211,26 @@ 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 );
// Filter out product variations.
if ( $product && 'true' === $this->request['order_item_display_meta'] ) {
$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;
// 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;
}
return true;
} );
);
}
$data['meta_data'] = array_map(