Skip to content

Commit

Permalink
Manual set order tests
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiulodro committed Dec 22, 2017
1 parent 1c8dd6a commit 09ef219
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
23 changes: 1 addition & 22 deletions includes/api/class-wc-rest-orders-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ 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 @@ -442,28 +443,6 @@ 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
21 changes: 18 additions & 3 deletions tests/unit-tests/api/orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ public function test_update_order_remove_items() {
/**
* 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(
Expand All @@ -351,6 +351,13 @@ public function test_update_order_add_coupons() {
'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();
Expand All @@ -365,11 +372,12 @@ public function test_update_order_add_coupons() {
/**
* 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();
Expand All @@ -390,6 +398,13 @@ public function test_update_order_remove_coupons() {
'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();
Expand Down

0 comments on commit 09ef219

Please sign in to comment.