Proposed fix for api coupon recalculating

This commit is contained in:
claudiulodro 2017-12-20 11:37:35 -08:00
parent 70455b79a3
commit 1c8dd6acbb
2 changed files with 25 additions and 1 deletions

View File

@ -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 ) {

View File

@ -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(