From ea63d5aaf97f0c1e1e233a8fd66fadb5199a1566 Mon Sep 17 00:00:00 2001 From: Shiki Date: Mon, 20 Jul 2020 18:00:38 -0600 Subject: [PATCH] Orders V3: Include display_key and display_value in item meta_data --- .../class-wc-rest-orders-controller.php | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller.php b/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller.php index 6867e4bd612..13a5311b5de 100644 --- a/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller.php +++ b/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller.php @@ -280,4 +280,52 @@ class WC_REST_Orders_Controller extends WC_REST_Orders_V2_Controller { return $params; } + + /** + * Expands an order item to get its data. + * + * @param WC_Order_item $item Order item data. + * @return array + */ + protected function get_order_item_data( $item ) { + $data = parent::get_order_item_data( $item ); + + $product = is_callable( array( $item, 'get_product' ) ) ? $item->get_product() : false; + + $data['meta_data'] = array_map( + array( $this, 'get_order_item_meta_data' ), + $data['meta_data'], + array_fill( 0, count( $data['meta_data'] ), $product ) + ); + + return $data; + } + + /** + * Convert a {@link WC_Meta_Data} to an array with the expected API response keys and values. + * + * @param WC_Meta_Data $meta The metadata taken from the {@link WC_Order_Item::$meta_data} array. + * @param WC_Product $product The product that the metadata belongs to. + * @return array + */ + private function get_order_item_meta_data( $meta, $product ) { + $attribute_key = str_replace( 'attribute_', '', $meta->key ); + $display_key = wc_attribute_label( $attribute_key, $product ); + + $display_value = wp_kses_post( $meta->value ); + if ( taxonomy_exists( $attribute_key ) ) { + $term = get_term_by( 'slug', $meta->value, $attribute_key ); + if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) { + $display_value = $term->name; + } + } + + return array( + 'id' => $meta->id, + 'key' => $meta->key, + 'value' => $meta->value, + 'display_key' => $display_key, + 'display_value' => $display_value, + ); + } }