WC_REST_Orders_V2_Controller::get_order_item_data()
Expands an order item to get its data.
Method of the class: WC_REST_Orders_V2_Controller{}
No Hooks.
Return
Array
.
Usage
// protected - for code of main (parent) or child class $result = $this->get_order_item_data( $item );
- $item(WC_Order_item) (required)
- Order item data.
WC_REST_Orders_V2_Controller::get_order_item_data() WC REST Orders V2 Controller::get order item data code WC 7.7.0
protected function get_order_item_data( $item ) { $data = $item->get_data(); $format_decimal = array( 'subtotal', 'subtotal_tax', 'total', 'total_tax', 'tax_total', 'shipping_tax_total' ); // Format decimal values. foreach ( $format_decimal as $key ) { if ( isset( $data[ $key ] ) ) { $data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] ); } } // Add SKU, PRICE, and IMAGE to products. if ( is_callable( array( $item, 'get_product' ) ) ) { $data['sku'] = $item->get_product() ? $item->get_product()->get_sku() : null; $data['price'] = $item->get_quantity() ? $item->get_total() / $item->get_quantity() : 0; $image_id = $item->get_product() ? $item->get_product()->get_image_id() : 0; $data['image'] = array( 'id' => $image_id, 'src' => $image_id ? wp_get_attachment_image_url( $image_id, 'full' ) : '', ); } // Add parent_name if the product is a variation. if ( is_callable( array( $item, 'get_product' ) ) ) { $product = $item->get_product(); if ( is_callable( array( $product, 'get_parent_data' ) ) ) { $data['parent_name'] = $product->get_title(); } else { $data['parent_name'] = null; } } // Format taxes. if ( ! empty( $data['taxes']['total'] ) ) { $taxes = array(); foreach ( $data['taxes']['total'] as $tax_rate_id => $tax ) { $taxes[] = array( 'id' => $tax_rate_id, 'total' => $tax, 'subtotal' => isset( $data['taxes']['subtotal'][ $tax_rate_id ] ) ? $data['taxes']['subtotal'][ $tax_rate_id ] : '', ); } $data['taxes'] = $taxes; } elseif ( isset( $data['taxes'] ) ) { $data['taxes'] = array(); } // Remove names for coupons, taxes and shipping. if ( isset( $data['code'] ) || isset( $data['rate_code'] ) || isset( $data['method_title'] ) ) { unset( $data['name'] ); } // Remove props we don't want to expose. unset( $data['order_id'] ); unset( $data['type'] ); // Expand meta_data to include user-friendly values. $formatted_meta_data = $item->get_all_formatted_meta_data( null ); // Filter out product variations. if ( isset( $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 ) { $display_value = wp_kses_post( rawurldecode( (string) $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'], array_fill( 0, count( $data['meta_data'] ), $formatted_meta_data ) ); return $data; }