From 0c4173b8857d31ff6f8f709e67a8d6bbeb126b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Dumont?= Date: Wed, 3 Jul 2019 15:51:32 +0200 Subject: [PATCH] Improved responses for updating items so developers can get the quantity of the item. --- includes/api/class-cocart-controller.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/includes/api/class-cocart-controller.php b/includes/api/class-cocart-controller.php index 0caa429e..e32c20f2 100644 --- a/includes/api/class-cocart-controller.php +++ b/includes/api/class-cocart-controller.php @@ -763,17 +763,30 @@ public function update_item( $data = array() ) { return new WP_REST_Response( $cart_contents, 200 ); } + $response = array(); + // Return response based on product quantity increment. if ( $quantity > $current_data['quantity'] ) { /* translators: 1: product name, 2: new quantity */ - return new WP_REST_Response( sprintf( __( 'The quantity for "%1$s" has increased to "%2$s".', 'cart-rest-api-for-woocommerce' ), $product_data->get_name(), $new_data['quantity'] ), 200 ); + $response = array( + 'message' => sprintf( __( 'The quantity for "%1$s" has increased to "%2$s".', 'cart-rest-api-for-woocommerce' ), $product_data->get_name(), $new_data['quantity'] ), + 'quantity' => $new_data['quantity'] + ); } else if ( $quantity < $current_data['quantity'] ) { /* translators: 1: product name, 2: new quantity */ - return new WP_REST_Response( sprintf( __( 'The quantity for "%1$s" has decreased to "%2$s".', 'cart-rest-api-for-woocommerce' ), $product_data->get_name(), $new_data['quantity'] ), 200 ); + $response = array( + 'message' => sprintf( __( 'The quantity for "%1$s" has decreased to "%2$s".', 'cart-rest-api-for-woocommerce' ), $product_data->get_name(), $new_data['quantity'] ), + 'quantity' => $new_data['quantity'] + ); } else { /* translators: %s: product name */ - return new WP_REST_Response( sprintf( __( 'The quantity for "%s" has not changed.', 'cart-rest-api-for-woocommerce' ), $product_data->get_name() ), 200 ); + $response = array( + 'message' => sprintf( __( 'The quantity for "%s" has not changed.', 'cart-rest-api-for-woocommerce' ), $product_data->get_name() ), + 'quantity' => $quantity + ); } + + return new WP_REST_Response( $response, 200 ); } else { return new WP_Error( 'cocart_can_not_update_item', __( 'Unable to update item quantity in cart.', 'cart-rest-api-for-woocommerce' ), array( 'status' => 500 ) ); }