From 1c8dd6acbb698fe111d3f055f9284b6e09ca5006 Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Wed, 20 Dec 2017 11:37:35 -0800 Subject: [PATCH 1/2] Proposed fix for api coupon recalculating --- .../api/class-wc-rest-orders-controller.php | 23 ++++++++++++++++++- tests/unit-tests/api/orders.php | 3 +++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/includes/api/class-wc-rest-orders-controller.php b/includes/api/class-wc-rest-orders-controller.php index 97d909c53b6..5b815c450e4 100644 --- a/includes/api/class-wc-rest-orders-controller.php +++ b/includes/api/class-wc-rest-orders-controller.php @@ -430,7 +430,6 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { case 'line_items' : case 'shipping_lines' : case 'fee_lines' : - case 'coupon_lines' : if ( is_array( $value ) ) { foreach ( $value as $item ) { if ( is_array( $item ) ) { @@ -443,6 +442,28 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { } } break; + case 'coupon_lines' : + if ( is_array( $value ) ) { + foreach ( $value as $item ) { + if ( is_array( $item ) ) { + if ( $this->item_is_null( $item ) ) { + $item = $order->get_item( $item['id'] ); + if ( $item && method_exists( $item, 'get_code' ) ) { + $order->remove_coupon( $item->get_code() ); + } else { + $order->remove_item( $item['id'] ); + } + } else { + if ( ! empty( $item['code'] ) ) { + $order->apply_coupon( $item['code'] ); + } else { + $this->set_item( $order, $key, $item ); + } + } + } + } + } + break; case 'meta_data' : if ( is_array( $value ) ) { foreach ( $value as $meta ) { diff --git a/tests/unit-tests/api/orders.php b/tests/unit-tests/api/orders.php index a13e31932f3..68a56acb7bc 100644 --- a/tests/unit-tests/api/orders.php +++ b/tests/unit-tests/api/orders.php @@ -338,6 +338,9 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case { public function test_update_order_add_coupons() { wp_set_current_user( $this->user ); $order = WC_Helper_Order::create_order(); + $coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' ); + $coupon->set_amount( 5 ); + $coupon->save(); $request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); $request->set_body_params( array( From 09ef2195be7995238cfb0e91a98532f660c40e34 Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Fri, 22 Dec 2017 11:35:52 -0800 Subject: [PATCH 2/2] Manual set order tests --- .../api/class-wc-rest-orders-controller.php | 23 +------------------ tests/unit-tests/api/orders.php | 21 ++++++++++++++--- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/includes/api/class-wc-rest-orders-controller.php b/includes/api/class-wc-rest-orders-controller.php index 5b815c450e4..97d909c53b6 100644 --- a/includes/api/class-wc-rest-orders-controller.php +++ b/includes/api/class-wc-rest-orders-controller.php @@ -430,6 +430,7 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { case 'line_items' : case 'shipping_lines' : case 'fee_lines' : + case 'coupon_lines' : if ( is_array( $value ) ) { foreach ( $value as $item ) { if ( is_array( $item ) ) { @@ -442,28 +443,6 @@ class WC_REST_Orders_Controller extends WC_REST_Legacy_Orders_Controller { } } break; - case 'coupon_lines' : - if ( is_array( $value ) ) { - foreach ( $value as $item ) { - if ( is_array( $item ) ) { - if ( $this->item_is_null( $item ) ) { - $item = $order->get_item( $item['id'] ); - if ( $item && method_exists( $item, 'get_code' ) ) { - $order->remove_coupon( $item->get_code() ); - } else { - $order->remove_item( $item['id'] ); - } - } else { - if ( ! empty( $item['code'] ) ) { - $order->apply_coupon( $item['code'] ); - } else { - $this->set_item( $order, $key, $item ); - } - } - } - } - } - break; case 'meta_data' : if ( is_array( $value ) ) { foreach ( $value as $meta ) { diff --git a/tests/unit-tests/api/orders.php b/tests/unit-tests/api/orders.php index 68a56acb7bc..dcedb963518 100644 --- a/tests/unit-tests/api/orders.php +++ b/tests/unit-tests/api/orders.php @@ -333,15 +333,15 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case { /** * Tests updating an order and adding a coupon. * - * @since 3.0.0 + * @since 3.3.0 */ public function test_update_order_add_coupons() { wp_set_current_user( $this->user ); $order = WC_Helper_Order::create_order(); + $order_item = current( $order->get_items() ); $coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' ); $coupon->set_amount( 5 ); $coupon->save(); - $request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); $request->set_body_params( array( 'coupon_lines' => array( @@ -351,6 +351,13 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case { 'discount_tax' => '0', ), ), + 'line_items' => array( + array( + 'id' => $order_item->get_id(), + 'product_id' => $order_item->get_product_id(), + 'total' => '35.00', + ), + ), ) ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); @@ -365,11 +372,12 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case { /** * Tests updating an order and removing a coupon. * - * @since 3.0.0 + * @since 3.3.0 */ public function test_update_order_remove_coupons() { wp_set_current_user( $this->user ); $order = WC_Helper_Order::create_order(); + $order_item = current( $order->get_items() ); $coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' ); $coupon->set_amount( 5 ); $coupon->save(); @@ -390,6 +398,13 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case { 'code' => null, ), ), + 'line_items' => array( + array( + 'id' => $order_item->get_id(), + 'product_id' => $order_item->get_product_id(), + 'total' => '40.00', + ), + ), ) ); $response = $this->server->dispatch( $request ); $data = $response->get_data();