AI
1 year ago
Agentic
4 months ago
AbstractCartRoute.php
4 months ago
AbstractRoute.php
3 months ago
AbstractTermsRoute.php
4 months ago
Batch.php
4 months ago
Cart.php
1 year ago
CartAddItem.php
4 months ago
CartApplyCoupon.php
1 year ago
CartCoupons.php
3 months ago
CartCouponsByCode.php
1 year ago
CartExtensions.php
2 years ago
CartItems.php
2 years ago
CartItemsByKey.php
2 years ago
CartRemoveCoupon.php
1 year ago
CartRemoveItem.php
4 months ago
CartSelectShippingRate.php
5 months ago
CartUpdateCustomer.php
4 months ago
CartUpdateItem.php
4 months ago
Checkout.php
1 month ago
CheckoutOrder.php
1 year ago
Order.php
7 months ago
Patterns.php
1 year ago
ProductAttributeTerms.php
1 month ago
ProductAttributes.php
1 year ago
ProductAttributesById.php
1 year ago
ProductBrands.php
1 year ago
ProductBrandsById.php
1 year ago
ProductCategories.php
1 year ago
ProductCategoriesById.php
1 year ago
ProductCollectionData.php
11 months ago
ProductReviews.php
4 months ago
ProductTags.php
1 year ago
Products.php
3 months ago
ProductsById.php
3 months ago
ProductsBySlug.php
3 months ago
ShopperListItems.php
1 month ago
ShopperListItemsByKey.php
1 month ago
ShopperLists.php
1 month ago
ShopperListsBySlug.php
1 month ago
ShopperListsNonceCheck.php
1 month ago
CartUpdateItem.php
96 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 3 | |
| 4 | /** |
| 5 | * CartUpdateItem class. |
| 6 | */ |
| 7 | class CartUpdateItem extends AbstractCartRoute { |
| 8 | /** |
| 9 | * The route identifier. |
| 10 | * |
| 11 | * @var string |
| 12 | */ |
| 13 | const IDENTIFIER = 'cart-update-item'; |
| 14 | |
| 15 | /** |
| 16 | * Get the path of this REST route. |
| 17 | * |
| 18 | * @return string |
| 19 | */ |
| 20 | public function get_path() { |
| 21 | return self::get_path_regex(); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Get the path of this rest route. |
| 26 | * |
| 27 | * @return string |
| 28 | */ |
| 29 | public static function get_path_regex() { |
| 30 | return '/cart/update-item'; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get method arguments for this REST route. |
| 35 | * |
| 36 | * @return array An array of endpoints. |
| 37 | */ |
| 38 | public function get_args() { |
| 39 | return [ |
| 40 | [ |
| 41 | 'methods' => \WP_REST_Server::CREATABLE, |
| 42 | 'callback' => [ $this, 'get_response' ], |
| 43 | 'permission_callback' => '__return_true', |
| 44 | 'args' => [ |
| 45 | 'key' => [ |
| 46 | 'description' => __( 'Unique identifier (key) for the cart item to update.', 'woocommerce' ), |
| 47 | 'type' => 'string', |
| 48 | ], |
| 49 | 'quantity' => [ |
| 50 | 'description' => __( 'New quantity of the item in the cart.', 'woocommerce' ), |
| 51 | 'type' => 'number', |
| 52 | 'arg_options' => [ |
| 53 | 'sanitize_callback' => 'wc_stock_amount', |
| 54 | ], |
| 55 | ], |
| 56 | ], |
| 57 | ], |
| 58 | 'schema' => [ $this->schema, 'get_public_item_schema' ], |
| 59 | 'allow_batch' => [ 'v1' => true ], |
| 60 | ]; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Handle the request and return a valid response for this endpoint. |
| 65 | * . |
| 66 | * |
| 67 | * @param \WP_REST_Request $request Request object. |
| 68 | * @return \WP_REST_Response |
| 69 | */ |
| 70 | protected function get_route_post_response( \WP_REST_Request $request ) { |
| 71 | $cart = $this->cart_controller->get_cart_instance(); |
| 72 | |
| 73 | if ( isset( $request['quantity'] ) ) { |
| 74 | $cart_item = $cart->get_cart_item( $request['key'] ); |
| 75 | $old_quantity = $cart_item['quantity'] ?? 0; |
| 76 | $this->cart_controller->set_cart_item_quantity( $request['key'], $request['quantity'] ); |
| 77 | |
| 78 | if ( $old_quantity !== (int) $request['quantity'] ) { |
| 79 | /** |
| 80 | * Fires when a cart item quantity is updated from a user request. |
| 81 | * |
| 82 | * @param string $cart_item_key Cart item key. |
| 83 | * @param int $quantity New quantity. |
| 84 | * @param int|float $old_quantity Old quantity. |
| 85 | * @param \WC_Cart $cart Cart object. |
| 86 | * |
| 87 | * @since 10.6.0 |
| 88 | */ |
| 89 | do_action( 'internal_woocommerce_cart_item_updated_from_user_request', $request['key'], (int) $request['quantity'], $old_quantity, $cart ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return rest_ensure_response( $this->schema->get_item_response( $cart ) ); |
| 94 | } |
| 95 | } |
| 96 |