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
CartItemsByKey.php
156 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 5 | |
| 6 | /** |
| 7 | * CartItemsByKey class. |
| 8 | */ |
| 9 | class CartItemsByKey extends AbstractCartRoute { |
| 10 | /** |
| 11 | * The route identifier. |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | const IDENTIFIER = 'cart-items-by-key'; |
| 16 | |
| 17 | /** |
| 18 | * The routes schema. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | const SCHEMA_TYPE = 'cart-item'; |
| 23 | |
| 24 | /** |
| 25 | * Get the path of this REST route. |
| 26 | * |
| 27 | * @return string |
| 28 | */ |
| 29 | public function get_path() { |
| 30 | return self::get_path_regex(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get the path of this rest route. |
| 35 | * |
| 36 | * @return string |
| 37 | */ |
| 38 | public static function get_path_regex() { |
| 39 | return '/cart/items/(?P<key>[\w-]{32})'; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get method arguments for this REST route. |
| 44 | * |
| 45 | * @return array An array of endpoints. |
| 46 | */ |
| 47 | public function get_args() { |
| 48 | return [ |
| 49 | 'args' => [ |
| 50 | 'key' => [ |
| 51 | 'description' => __( 'Unique identifier for the item within the cart.', 'woocommerce' ), |
| 52 | 'type' => 'string', |
| 53 | ], |
| 54 | ], |
| 55 | [ |
| 56 | 'methods' => \WP_REST_Server::READABLE, |
| 57 | 'callback' => [ $this, 'get_response' ], |
| 58 | 'permission_callback' => '__return_true', |
| 59 | 'args' => [ |
| 60 | 'context' => $this->get_context_param( [ 'default' => 'view' ] ), |
| 61 | ], |
| 62 | ], |
| 63 | [ |
| 64 | 'methods' => \WP_REST_Server::EDITABLE, |
| 65 | 'callback' => array( $this, 'get_response' ), |
| 66 | 'permission_callback' => '__return_true', |
| 67 | 'args' => $this->schema->get_endpoint_args_for_item_schema( \WP_REST_Server::EDITABLE ), |
| 68 | ], |
| 69 | [ |
| 70 | 'methods' => \WP_REST_Server::DELETABLE, |
| 71 | 'callback' => [ $this, 'get_response' ], |
| 72 | 'permission_callback' => '__return_true', |
| 73 | ], |
| 74 | 'schema' => [ $this->schema, 'get_public_item_schema' ], |
| 75 | 'allow_batch' => [ 'v1' => true ], |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get a single cart items. |
| 81 | * |
| 82 | * @throws RouteException On error. |
| 83 | * @param \WP_REST_Request $request Request object. |
| 84 | * @return \WP_REST_Response |
| 85 | */ |
| 86 | protected function get_route_response( \WP_REST_Request $request ) { |
| 87 | $cart_item = $this->cart_controller->get_cart_item( $request['key'] ); |
| 88 | |
| 89 | if ( empty( $cart_item ) ) { |
| 90 | throw new RouteException( 'woocommerce_rest_cart_invalid_key', __( 'Cart item does not exist.', 'woocommerce' ), 409 ); |
| 91 | } |
| 92 | |
| 93 | $data = $this->prepare_item_for_response( $cart_item, $request ); |
| 94 | $response = rest_ensure_response( $data ); |
| 95 | |
| 96 | return $response; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Update a single cart item. |
| 101 | * |
| 102 | * @throws RouteException On error. |
| 103 | * @param \WP_REST_Request $request Request object. |
| 104 | * @return \WP_REST_Response |
| 105 | */ |
| 106 | protected function get_route_update_response( \WP_REST_Request $request ) { |
| 107 | $cart = $this->cart_controller->get_cart_instance(); |
| 108 | |
| 109 | if ( isset( $request['quantity'] ) ) { |
| 110 | $this->cart_controller->set_cart_item_quantity( $request['key'], $request['quantity'] ); |
| 111 | } |
| 112 | |
| 113 | return rest_ensure_response( $this->prepare_item_for_response( $this->cart_controller->get_cart_item( $request['key'] ), $request ) ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Delete a single cart item. |
| 118 | * |
| 119 | * @throws RouteException On error. |
| 120 | * @param \WP_REST_Request $request Request object. |
| 121 | * @return \WP_REST_Response |
| 122 | */ |
| 123 | protected function get_route_delete_response( \WP_REST_Request $request ) { |
| 124 | $cart = $this->cart_controller->get_cart_instance(); |
| 125 | $cart_item = $this->cart_controller->get_cart_item( $request['key'] ); |
| 126 | |
| 127 | if ( empty( $cart_item ) ) { |
| 128 | throw new RouteException( 'woocommerce_rest_cart_invalid_key', __( 'Cart item does not exist.', 'woocommerce' ), 409 ); |
| 129 | } |
| 130 | |
| 131 | $cart->remove_cart_item( $request['key'] ); |
| 132 | |
| 133 | return new \WP_REST_Response( null, 204 ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Prepare links for the request. |
| 138 | * |
| 139 | * @param array $cart_item Object to prepare. |
| 140 | * @param \WP_REST_Request $request Request object. |
| 141 | * @return array |
| 142 | */ |
| 143 | protected function prepare_links( $cart_item, $request ) { |
| 144 | $base = $this->get_namespace() . $this->get_path(); |
| 145 | $links = array( |
| 146 | 'self' => array( |
| 147 | 'href' => rest_url( trailingslashit( $base ) . $cart_item['key'] ), |
| 148 | ), |
| 149 | 'collection' => array( |
| 150 | 'href' => rest_url( $base ), |
| 151 | ), |
| 152 | ); |
| 153 | return $links; |
| 154 | } |
| 155 | } |
| 156 |