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
CartRemoveItem.php
110 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Utilities\DraftOrderTrait; |
| 5 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 6 | |
| 7 | /** |
| 8 | * CartRemoveItem class. |
| 9 | */ |
| 10 | class CartRemoveItem extends AbstractCartRoute { |
| 11 | use DraftOrderTrait; |
| 12 | |
| 13 | /** |
| 14 | * The route identifier. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | const IDENTIFIER = 'cart-remove-item'; |
| 19 | |
| 20 | /** |
| 21 | * Get the path of this REST route. |
| 22 | * |
| 23 | * @return string |
| 24 | */ |
| 25 | public function get_path() { |
| 26 | return self::get_path_regex(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Get the path of this rest route. |
| 31 | * |
| 32 | * @return string |
| 33 | */ |
| 34 | public static function get_path_regex() { |
| 35 | return '/cart/remove-item'; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get method arguments for this REST route. |
| 40 | * |
| 41 | * @return array An array of endpoints. |
| 42 | */ |
| 43 | public function get_args() { |
| 44 | return [ |
| 45 | [ |
| 46 | 'methods' => \WP_REST_Server::CREATABLE, |
| 47 | 'callback' => [ $this, 'get_response' ], |
| 48 | 'permission_callback' => '__return_true', |
| 49 | 'args' => [ |
| 50 | 'key' => [ |
| 51 | 'description' => __( 'Unique identifier (key) for the cart item.', 'woocommerce' ), |
| 52 | 'type' => 'string', |
| 53 | ], |
| 54 | ], |
| 55 | ], |
| 56 | 'schema' => [ $this->schema, 'get_public_item_schema' ], |
| 57 | 'allow_batch' => [ 'v1' => true ], |
| 58 | ]; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Handle the request and return a valid response for this endpoint. |
| 63 | * |
| 64 | * @throws RouteException On error. |
| 65 | * @param \WP_REST_Request $request Request object. |
| 66 | * @return \WP_REST_Response |
| 67 | */ |
| 68 | protected function get_route_post_response( \WP_REST_Request $request ) { |
| 69 | $cart = $this->cart_controller->get_cart_instance(); |
| 70 | $cart_item = $this->cart_controller->get_cart_item( $request['key'] ); |
| 71 | |
| 72 | if ( empty( $cart_item ) ) { |
| 73 | throw new RouteException( 'woocommerce_rest_cart_invalid_key', __( 'Cart item no longer exists or is invalid.', 'woocommerce' ), 409 ); |
| 74 | } |
| 75 | |
| 76 | $removed = $cart->remove_cart_item( $request['key'] ); |
| 77 | |
| 78 | if ( $removed ) { |
| 79 | /** |
| 80 | * Fires when a cart item is removed from a user request. |
| 81 | * |
| 82 | * @param string $cart_item_key Cart item key. |
| 83 | * @param \WC_Cart $cart Cart object. |
| 84 | * |
| 85 | * @since 10.6.0 |
| 86 | */ |
| 87 | do_action( 'internal_woocommerce_cart_item_removed_from_user_request', $request['key'], $cart ); |
| 88 | } |
| 89 | |
| 90 | $this->maybe_release_stock(); |
| 91 | |
| 92 | return rest_ensure_response( $this->schema->get_item_response( $cart ) ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * If there is a draft order, releases stock. |
| 97 | * |
| 98 | * @return void |
| 99 | */ |
| 100 | protected function maybe_release_stock() { |
| 101 | $draft_order_id = $this->get_draft_order_id(); |
| 102 | |
| 103 | if ( ! $draft_order_id ) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | wc_release_stock_for_order( $draft_order_id ); |
| 108 | } |
| 109 | } |
| 110 |