Skip to content

Commit

Permalink
Proposed fix for api coupon recalculating
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiulodro committed Dec 20, 2017
1 parent 70455b7 commit 1c8dd6a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
23 changes: 22 additions & 1 deletion includes/api/class-wc-rest-orders-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ protected function prepare_object_for_database( $request, $creating = false ) {
case 'line_items' :
case 'shipping_lines' :
case 'fee_lines' :
case 'coupon_lines' :
if ( is_array( $value ) ) {
foreach ( $value as $item ) {
if ( is_array( $item ) ) {
Expand All @@ -443,6 +442,28 @@ protected function prepare_object_for_database( $request, $creating = false ) {
}
}
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 ) {
Expand Down
3 changes: 3 additions & 0 deletions tests/unit-tests/api/orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ public function test_update_order_remove_items() {
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(
Expand Down

0 comments on commit 1c8dd6a

Please sign in to comment.