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
CartAddItem.php
151 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 5 | |
| 6 | /** |
| 7 | * CartAddItem class. |
| 8 | */ |
| 9 | class CartAddItem extends AbstractCartRoute { |
| 10 | /** |
| 11 | * The route identifier. |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | const IDENTIFIER = 'cart-add-item'; |
| 16 | |
| 17 | /** |
| 18 | * Get the path of this REST route. |
| 19 | * |
| 20 | * @return string |
| 21 | */ |
| 22 | public function get_path() { |
| 23 | return self::get_path_regex(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Get the path of this rest route. |
| 28 | * |
| 29 | * @return string |
| 30 | */ |
| 31 | public static function get_path_regex() { |
| 32 | return '/cart/add-item'; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get method arguments for this REST route. |
| 37 | * |
| 38 | * @return array An array of endpoints. |
| 39 | */ |
| 40 | public function get_args() { |
| 41 | return [ |
| 42 | [ |
| 43 | 'methods' => \WP_REST_Server::CREATABLE, |
| 44 | 'callback' => [ $this, 'get_response' ], |
| 45 | 'permission_callback' => '__return_true', |
| 46 | 'args' => [ |
| 47 | 'id' => [ |
| 48 | 'description' => __( 'The cart item product or variation ID.', 'woocommerce' ), |
| 49 | 'type' => 'integer', |
| 50 | 'context' => [ 'view', 'edit' ], |
| 51 | 'sanitize_callback' => 'absint', |
| 52 | ], |
| 53 | 'quantity' => [ |
| 54 | 'description' => __( 'Quantity of this item to add to the cart.', 'woocommerce' ), |
| 55 | 'type' => 'number', |
| 56 | 'context' => [ 'view', 'edit' ], |
| 57 | 'arg_options' => [ |
| 58 | 'sanitize_callback' => 'wc_stock_amount', |
| 59 | ], |
| 60 | ], |
| 61 | 'variation' => [ |
| 62 | 'description' => __( 'Chosen attributes (for variations).', 'woocommerce' ), |
| 63 | 'type' => 'array', |
| 64 | 'context' => [ 'view', 'edit' ], |
| 65 | 'items' => [ |
| 66 | 'type' => 'object', |
| 67 | 'properties' => [ |
| 68 | 'attribute' => [ |
| 69 | 'description' => __( 'Variation attribute name.', 'woocommerce' ), |
| 70 | 'type' => 'string', |
| 71 | 'context' => [ 'view', 'edit' ], |
| 72 | ], |
| 73 | 'value' => [ |
| 74 | 'description' => __( 'Variation attribute value.', 'woocommerce' ), |
| 75 | 'type' => 'string', |
| 76 | 'context' => [ 'view', 'edit' ], |
| 77 | ], |
| 78 | ], |
| 79 | ], |
| 80 | ], |
| 81 | ], |
| 82 | ], |
| 83 | 'schema' => [ $this->schema, 'get_public_item_schema' ], |
| 84 | 'allow_batch' => [ 'v1' => true ], |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Handle the request and return a valid response for this endpoint. |
| 90 | * |
| 91 | * @throws RouteException On error. |
| 92 | * @param \WP_REST_Request $request Request object. |
| 93 | * @return \WP_REST_Response |
| 94 | */ |
| 95 | protected function get_route_post_response( \WP_REST_Request $request ) { |
| 96 | // Do not allow key to be specified during creation. |
| 97 | if ( ! empty( $request['key'] ) ) { |
| 98 | throw new RouteException( 'woocommerce_rest_cart_item_exists', esc_html__( 'Cannot create an existing cart item.', 'woocommerce' ), 400 ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Filters cart item data sent via the API before it is passed to the cart controller. |
| 103 | * |
| 104 | * This hook filters cart items. It allows the request data to be changed, for example, quantity, or |
| 105 | * supplemental cart item data, before it is passed into CartController::add_to_cart and stored to session. |
| 106 | * |
| 107 | * CartController::add_to_cart only expects the keys id, quantity, variation, and cart_item_data, so other values |
| 108 | * may be ignored. CartController::add_to_cart (and core) do already have a filter hook called |
| 109 | * woocommerce_add_cart_item, but this does not have access to the original Store API request like this hook does. |
| 110 | * |
| 111 | * @since 8.8.0 |
| 112 | * |
| 113 | * @param array $add_to_cart_data An array of cart item data. |
| 114 | * @return array |
| 115 | */ |
| 116 | $add_to_cart_data = apply_filters( |
| 117 | 'woocommerce_store_api_add_to_cart_data', |
| 118 | array( |
| 119 | 'id' => $request['id'], |
| 120 | 'quantity' => $request['quantity'], |
| 121 | 'variation' => $request['variation'], |
| 122 | 'cart_item_data' => [], |
| 123 | ), |
| 124 | $request |
| 125 | ); |
| 126 | |
| 127 | $item_id = $this->cart_controller->add_to_cart( $add_to_cart_data ); |
| 128 | $cart = $this->cart_controller->get_cart_instance(); |
| 129 | $cart_item = $cart->get_cart_item( $item_id ); |
| 130 | |
| 131 | if ( ! empty( $cart_item ) ) { |
| 132 | $product_id = $cart_item['variation_id'] ? $cart_item['variation_id'] : $cart_item['product_id']; |
| 133 | $quantity = $add_to_cart_data['quantity'] ?? $cart_item['quantity']; |
| 134 | |
| 135 | /** |
| 136 | * Fires when an item is added to the cart from a user request. |
| 137 | * |
| 138 | * @param int $product_id Product ID (variation ID for variable products). |
| 139 | * @param int|float $quantity Quantity added to the cart. |
| 140 | * |
| 141 | * @since 10.6.0 |
| 142 | */ |
| 143 | do_action( 'internal_woocommerce_cart_item_added_from_user_request', $product_id, $quantity ); |
| 144 | } |
| 145 | |
| 146 | $response = rest_ensure_response( $this->schema->get_item_response( $this->cart_controller->get_cart_for_response() ) ); |
| 147 | $response->set_status( 201 ); |
| 148 | return $response; |
| 149 | } |
| 150 | } |
| 151 |