Skip to content

Commit

Permalink
Merge pull request #70 from co-cart/update-item
Browse files Browse the repository at this point in the history
Improved responses for updating items so developers can get the quantity of the item.
  • Loading branch information
seb86 committed Jul 3, 2019
2 parents b58808b + 0c4173b commit d212b4a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions includes/api/class-cocart-controller.php
Expand Up @@ -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 ) );
}
Expand Down

0 comments on commit d212b4a

Please sign in to comment.