From 4ea40d997b7e8c89600315fa904d08193c3d4e83 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 10 Mar 2017 17:20:27 -0300 Subject: [PATCH 1/3] [REST API] Improved order of items in Orders endpoint schema --- .../api/class-wc-rest-orders-controller.php | 235 +++++++++++------- 1 file changed, 143 insertions(+), 92 deletions(-) diff --git a/includes/api/class-wc-rest-orders-controller.php b/includes/api/class-wc-rest-orders-controller.php index cc629d67c66..dfecdfa8353 100644 --- a/includes/api/class-wc-rest-orders-controller.php +++ b/includes/api/class-wc-rest-orders-controller.php @@ -184,6 +184,94 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { return $data; } + /** + * Get formatted item data. + * + * @since 2.7.0 + * @param WC_Data $object WC_Data instance. + * @return array + */ + protected function get_formatted_item_data( $object ) { + $item = $object->get_data(); + $format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' ); + $format_date = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' ); + $format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' ); + + // Format decimal values. + foreach ( $format_decimal as $key ) { + $item[ $key ] = wc_format_decimal( $item[ $key ], $this->request['dp'] ); + } + + // Format date values. + foreach ( $format_date as $key ) { + $datetime = $item[ $key ]; + $item[ $key ] = wc_rest_prepare_date_response( $datetime, false ); + $item[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); + } + + // Format the order status. + $item['status'] = 'wc-' === substr( $item['status'], 0, 3 ) ? substr( $item['status'], 3 ) : $item['status']; + + // Format line items. + foreach ( $format_line_items as $key ) { + $item[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $item[ $key ] ) ); + } + + // Refunds. + $item['refunds'] = array(); + foreach ( $object->get_refunds() as $refund ) { + $item['refunds'][] = array( + 'id' => $refund->get_id(), + 'refund' => $refund->get_reason() ? $refund->get_reason() : '', + 'total' => '-' . wc_format_decimal( $refund->get_amount(), $this->request['dp'] ), + ); + } + + return array( + 'id' => $object->get_id(), + 'parent_id' => $item['parent_id'], + 'number' => $item['number'], + 'order_key' => $item['order_key'], + 'created_via' => $item['created_via'], + 'version' => $item['version'], + 'status' => $item['status'], + 'currency' => $item['currency'], + 'date_created' => $item['date_created'], + 'date_created_gmt' => $item['date_created_gmt'], + 'date_modified' => $item['date_modified'], + 'date_modified_gmt' => $item['date_modified_gmt'], + 'discount_total' => $item['discount_total'], + 'discount_tax' => $item['discount_tax'], + 'shipping_total' => $item['shipping_total'], + 'shipping_tax' => $item['shipping_tax'], + 'cart_tax' => $item['cart_tax'], + 'total' => $item['total'], + 'total_tax' => $item['total_tax'], + 'prices_include_tax' => $item['prices_include_tax'], + 'customer_id' => $item['customer_id'], + 'customer_ip_address' => $item['customer_ip_address'], + 'customer_user_agent' => $item['customer_user_agent'], + 'customer_note' => $item['customer_note'], + 'billing' => $item['billing'], + 'shipping' => $item['shipping'], + 'payment_method' => $item['payment_method'], + 'payment_method_title' => $item['payment_method_title'], + 'transaction_id' => $item['transaction_id'], + 'date_paid' => $item['date_paid'], + 'date_paid_gmt' => $item['date_paid_gmt'], + 'date_completed' => $item['date_completed'], + 'date_completed_gmt' => $item['date_completed_gmt'], + 'cart_hash' => $item['cart_hash'], + 'meta_data' => $item['meta_data'], + 'line_items' => $item['line_items'], + 'tax_lines' => $item['tax_lines'], + 'shipping_lines' => $item['shipping_lines'], + 'fee_lines' => $item['fee_lines'], + 'coupon_lines' => $item['coupon_lines'], + 'refunds' => $item['refunds'], + ); + } + /** * Prepare a single order output for response. * @@ -193,49 +281,12 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { * @return WP_REST_Response */ public function prepare_object_for_response( $object, $request ) { - $this->request = $request; - $data = $object->get_data(); - $format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' ); - $format_date = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' ); - $format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' ); - - // Format decimal values. - foreach ( $format_decimal as $key ) { - $data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] ); - } - - // Format date values. - foreach ( $format_date as $key ) { - $datetime = $data[ $key ]; - $data[ $key ] = wc_rest_prepare_date_response( $datetime, false ); - $data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); - } - - // Format the order status. - $data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status']; - - // Format line items. - foreach ( $format_line_items as $key ) { - $data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) ); - } - - // Refunds. - $data['refunds'] = array(); - foreach ( $object->get_refunds() as $refund ) { - $data['refunds'][] = array( - 'id' => $refund->get_id(), - 'refund' => $refund->get_reason() ? $refund->get_reason() : '', - 'total' => '-' . wc_format_decimal( $refund->get_amount(), $this->request['dp'] ), - ); - } - - ksort( $data ); - - $data = array_merge( array( 'id' => $object->get_id() ), $data ); - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - $response = rest_ensure_response( $data ); + $this->request = $request; + $data = $this->get_formatted_item_data( $object ); + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $data, $request ); + $data = $this->filter_response_by_context( $data, $context ); + $response = rest_ensure_response( $data ); $response->add_links( $this->prepare_links( $object, $request ) ); /** @@ -749,6 +800,30 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { 'type' => 'integer', 'context' => array( 'view', 'edit' ), ), + 'number' => array( + 'description' => __( 'Order number.', 'woocommerce' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + 'order_key' => array( + 'description' => __( 'Order key.', 'woocommerce' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + 'created_via' => array( + 'description' => __( 'Shows where the order was created.', 'woocommerce' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + 'version' => array( + 'description' => __( 'Version of WooCommerce when the order was made.', 'woocommerce' ), + 'type' => 'integer', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), 'status' => array( 'description' => __( 'Order status.', 'woocommerce' ), 'type' => 'string', @@ -763,18 +838,6 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { 'enum' => array_keys( get_woocommerce_currencies() ), 'context' => array( 'view', 'edit' ), ), - 'version' => array( - 'description' => __( 'Version of WooCommerce when the order was made.', 'woocommerce' ), - 'type' => 'integer', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'prices_include_tax' => array( - 'description' => __( 'True the prices included tax during checkout.', 'woocommerce' ), - 'type' => 'boolean', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), 'date_created' => array( 'description' => __( "The date the order was created, in the site's timezone.", 'woocommerce' ), 'type' => 'date-time', @@ -841,18 +904,35 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { 'context' => array( 'view', 'edit' ), 'readonly' => true, ), + 'prices_include_tax' => array( + 'description' => __( 'True the prices included tax during checkout.', 'woocommerce' ), + 'type' => 'boolean', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), 'customer_id' => array( 'description' => __( 'User ID who owns the order. 0 for guests.', 'woocommerce' ), 'type' => 'integer', 'default' => 0, 'context' => array( 'view', 'edit' ), ), - 'order_key' => array( - 'description' => __( 'Order key.', 'woocommerce' ), + 'customer_ip_address' => array( + 'description' => __( "Customer's IP address.", 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), + 'customer_user_agent' => array( + 'description' => __( 'User agent of the customer.', 'woocommerce' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + 'customer_note' => array( + 'description' => __( 'Note left by customer during checkout.', 'woocommerce' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + ), 'billing' => array( 'description' => __( 'Billing address.', 'woocommerce' ), 'type' => 'object', @@ -983,29 +1063,18 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { 'type' => 'string', 'context' => array( 'view', 'edit' ), ), - 'customer_ip_address' => array( - 'description' => __( "Customer's IP address.", 'woocommerce' ), - 'type' => 'string', + 'date_paid' => array( + 'description' => __( "The date the order has been paid, in the site's timezone.", 'woocommerce' ), + 'type' => 'date-time', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), - 'customer_user_agent' => array( - 'description' => __( 'User agent of the customer.', 'woocommerce' ), - 'type' => 'string', + 'date_paid_gmt' => array( + 'description' => __( "The date the order has been paid, as GMT.", 'woocommerce' ), + 'type' => 'date-time', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), - 'created_via' => array( - 'description' => __( 'Shows where the order was created.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'customer_note' => array( - 'description' => __( 'Note left by customer during checkout.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - ), 'date_completed' => array( 'description' => __( "The date the order was completed, in the site's timezone.", 'woocommerce' ), 'type' => 'date-time', @@ -1018,30 +1087,12 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { 'context' => array( 'view', 'edit' ), 'readonly' => true, ), - 'date_paid' => array( - 'description' => __( "The date the order has been paid, in the site's timezone.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), - 'date_paid_gmt' => array( - 'description' => __( "The date the order has been paid, as GMT.", 'woocommerce' ), - 'type' => 'date-time', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), 'cart_hash' => array( 'description' => __( 'MD5 hash of cart items to ensure orders are not modified.', 'woocommerce' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), - 'number' => array( - 'description' => __( 'Order number.', 'woocommerce' ), - 'type' => 'string', - 'context' => array( 'view', 'edit' ), - 'readonly' => true, - ), 'meta_data' => array( 'description' => __( 'Meta data.', 'woocommerce' ), 'type' => 'array', From 65ed14635e1eccf25c474c7f3b4b851f777ad5ae Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 10 Mar 2017 17:22:38 -0300 Subject: [PATCH 2/3] Change bback $item to $data --- .../api/class-wc-rest-orders-controller.php | 98 +++++++++---------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/includes/api/class-wc-rest-orders-controller.php b/includes/api/class-wc-rest-orders-controller.php index dfecdfa8353..f9889402f39 100644 --- a/includes/api/class-wc-rest-orders-controller.php +++ b/includes/api/class-wc-rest-orders-controller.php @@ -192,35 +192,35 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { * @return array */ protected function get_formatted_item_data( $object ) { - $item = $object->get_data(); + $data = $object->get_data(); $format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' ); $format_date = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' ); $format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' ); // Format decimal values. foreach ( $format_decimal as $key ) { - $item[ $key ] = wc_format_decimal( $item[ $key ], $this->request['dp'] ); + $data[ $key ] = wc_format_decimal( $data[ $key ], $this->request['dp'] ); } // Format date values. foreach ( $format_date as $key ) { - $datetime = $item[ $key ]; - $item[ $key ] = wc_rest_prepare_date_response( $datetime, false ); - $item[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); + $datetime = $data[ $key ]; + $data[ $key ] = wc_rest_prepare_date_response( $datetime, false ); + $data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); } // Format the order status. - $item['status'] = 'wc-' === substr( $item['status'], 0, 3 ) ? substr( $item['status'], 3 ) : $item['status']; + $data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status']; // Format line items. foreach ( $format_line_items as $key ) { - $item[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $item[ $key ] ) ); + $data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) ); } // Refunds. - $item['refunds'] = array(); + $data['refunds'] = array(); foreach ( $object->get_refunds() as $refund ) { - $item['refunds'][] = array( + $data['refunds'][] = array( 'id' => $refund->get_id(), 'refund' => $refund->get_reason() ? $refund->get_reason() : '', 'total' => '-' . wc_format_decimal( $refund->get_amount(), $this->request['dp'] ), @@ -229,46 +229,46 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { return array( 'id' => $object->get_id(), - 'parent_id' => $item['parent_id'], - 'number' => $item['number'], - 'order_key' => $item['order_key'], - 'created_via' => $item['created_via'], - 'version' => $item['version'], - 'status' => $item['status'], - 'currency' => $item['currency'], - 'date_created' => $item['date_created'], - 'date_created_gmt' => $item['date_created_gmt'], - 'date_modified' => $item['date_modified'], - 'date_modified_gmt' => $item['date_modified_gmt'], - 'discount_total' => $item['discount_total'], - 'discount_tax' => $item['discount_tax'], - 'shipping_total' => $item['shipping_total'], - 'shipping_tax' => $item['shipping_tax'], - 'cart_tax' => $item['cart_tax'], - 'total' => $item['total'], - 'total_tax' => $item['total_tax'], - 'prices_include_tax' => $item['prices_include_tax'], - 'customer_id' => $item['customer_id'], - 'customer_ip_address' => $item['customer_ip_address'], - 'customer_user_agent' => $item['customer_user_agent'], - 'customer_note' => $item['customer_note'], - 'billing' => $item['billing'], - 'shipping' => $item['shipping'], - 'payment_method' => $item['payment_method'], - 'payment_method_title' => $item['payment_method_title'], - 'transaction_id' => $item['transaction_id'], - 'date_paid' => $item['date_paid'], - 'date_paid_gmt' => $item['date_paid_gmt'], - 'date_completed' => $item['date_completed'], - 'date_completed_gmt' => $item['date_completed_gmt'], - 'cart_hash' => $item['cart_hash'], - 'meta_data' => $item['meta_data'], - 'line_items' => $item['line_items'], - 'tax_lines' => $item['tax_lines'], - 'shipping_lines' => $item['shipping_lines'], - 'fee_lines' => $item['fee_lines'], - 'coupon_lines' => $item['coupon_lines'], - 'refunds' => $item['refunds'], + 'parent_id' => $data['parent_id'], + 'number' => $data['number'], + 'order_key' => $data['order_key'], + 'created_via' => $data['created_via'], + 'version' => $data['version'], + 'status' => $data['status'], + 'currency' => $data['currency'], + 'date_created' => $data['date_created'], + 'date_created_gmt' => $data['date_created_gmt'], + 'date_modified' => $data['date_modified'], + 'date_modified_gmt' => $data['date_modified_gmt'], + 'discount_total' => $data['discount_total'], + 'discount_tax' => $data['discount_tax'], + 'shipping_total' => $data['shipping_total'], + 'shipping_tax' => $data['shipping_tax'], + 'cart_tax' => $data['cart_tax'], + 'total' => $data['total'], + 'total_tax' => $data['total_tax'], + 'prices_include_tax' => $data['prices_include_tax'], + 'customer_id' => $data['customer_id'], + 'customer_ip_address' => $data['customer_ip_address'], + 'customer_user_agent' => $data['customer_user_agent'], + 'customer_note' => $data['customer_note'], + 'billing' => $data['billing'], + 'shipping' => $data['shipping'], + 'payment_method' => $data['payment_method'], + 'payment_method_title' => $data['payment_method_title'], + 'transaction_id' => $data['transaction_id'], + 'date_paid' => $data['date_paid'], + 'date_paid_gmt' => $data['date_paid_gmt'], + 'date_completed' => $data['date_completed'], + 'date_completed_gmt' => $data['date_completed_gmt'], + 'cart_hash' => $data['cart_hash'], + 'meta_data' => $data['meta_data'], + 'line_items' => $data['line_items'], + 'tax_lines' => $data['tax_lines'], + 'shipping_lines' => $data['shipping_lines'], + 'fee_lines' => $data['fee_lines'], + 'coupon_lines' => $data['coupon_lines'], + 'refunds' => $data['refunds'], ); } From b36fea1ce57bbcdd4c55f63b6a4bc5cd2f199352 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Fri, 10 Mar 2017 17:26:36 -0300 Subject: [PATCH 3/3] [REST API] Use the same order as schema for refunds response --- ...class-wc-rest-order-refunds-controller.php | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/includes/api/class-wc-rest-order-refunds-controller.php b/includes/api/class-wc-rest-order-refunds-controller.php index 68e88debc3f..155fd9be3e7 100644 --- a/includes/api/class-wc-rest-order-refunds-controller.php +++ b/includes/api/class-wc-rest-order-refunds-controller.php @@ -129,25 +129,13 @@ class WC_REST_Order_Refunds_Controller extends WC_REST_Orders_Controller { } /** - * Prepare a single order output for response. + * Get formatted item data. * * @since 2.7.0 - * @param WC_Data $object Object data. - * @param WP_REST_Request $request Request object. - * @return WP_REST_Response + * @param WC_Data $object WC_Data instance. + * @return array */ - public function prepare_object_for_response( $object, $request ) { - $this->request = $request; - $order = wc_get_order( (int) $request['order_id'] ); - - if ( ! $order ) { - return new WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 ); - } - - if ( ! $object || $object->get_parent_id() !== $order->get_id() ) { - return new WP_Error( 'woocommerce_rest_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 404 ); - } - + protected function get_formatted_item_data( $object ) { $data = $object->get_data(); $format_decimal = array( 'amount' ); $format_date = array( 'date_created' ); @@ -170,18 +158,39 @@ class WC_REST_Order_Refunds_Controller extends WC_REST_Orders_Controller { $data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) ); } - // Unset unwanted data. - unset( - $data['parent_id'], $data['status'], $data['currency'], $data['prices_include_tax'], - $data['version'], $data['date_modified'], $data['discount_total'], $data['discount_tax'], - $data['shipping_total'], $data['shipping_tax'], $data['cart_tax'], $data['cart_total'], - $data['total'], $data['total_tax'], $data['tax_lines'], $data['shipping_lines'], - $data['fee_lines'], $data['coupon_lines'] - ); + return array( + 'id' => $object->get_id(), + 'date_created' => $data['date_created'], + 'date_created_gmt' => $data['date_created_gmt'], + 'amount' => $data['amount'], + 'reason' => $data['reason'], + 'refunded_by' => $data['refunded_by'], + 'meta_data' => $data['meta_data'], + 'line_items' => $data['line_items'], + ); + } - ksort( $data ); + /** + * Prepare a single order output for response. + * + * @since 2.7.0 + * @param WC_Data $object Object data. + * @param WP_REST_Request $request Request object. + * @return WP_REST_Response + */ + public function prepare_object_for_response( $object, $request ) { + $this->request = $request; + $order = wc_get_order( (int) $request['order_id'] ); - $data = array_merge( array( 'id' => $object->get_id() ), $data ); + if ( ! $order ) { + return new WP_Error( 'woocommerce_rest_invalid_order_id', __( 'Invalid order ID.', 'woocommerce' ), 404 ); + } + + if ( ! $object || $object->get_parent_id() !== $order->get_id() ) { + return new WP_Error( 'woocommerce_rest_invalid_order_refund_id', __( 'Invalid order refund ID.', 'woocommerce' ), 404 ); + } + + $data = $this->get_formatted_item_data( $object ); $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object( $data, $request ); $data = $this->filter_response_by_context( $data, $context );