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
ShopperListItems.php
254 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\StoreApi\Routes\V1; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\ShopperLists\ShopperList; |
| 7 | use Automattic\WooCommerce\Internal\ShopperLists\ShopperListItem; |
| 8 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 9 | |
| 10 | /** |
| 11 | * GET / POST on /shopper-lists/{slug}/items. |
| 12 | * |
| 13 | * GET returns the items in a list. |
| 14 | * POST saves an item to the list either from an existing cart line or from direct item payload fields. |
| 15 | */ |
| 16 | class ShopperListItems extends AbstractRoute { |
| 17 | // Stopgap CSRF guard, replaced once the upstream trait lands on trunk. |
| 18 | use ShopperListsNonceCheck; |
| 19 | |
| 20 | /** |
| 21 | * Route identifier. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | const IDENTIFIER = 'shopper-list-items'; |
| 26 | |
| 27 | /** |
| 28 | * Schema identifier this route uses. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | const SCHEMA_TYPE = 'shopper-list-item'; |
| 33 | |
| 34 | /** |
| 35 | * Get the path of this REST route. |
| 36 | * |
| 37 | * @return string |
| 38 | */ |
| 39 | public function get_path() { |
| 40 | return self::get_path_regex(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the path regex for this REST route. |
| 45 | * |
| 46 | * @return string |
| 47 | */ |
| 48 | public static function get_path_regex() { |
| 49 | return '/shopper-lists/(?P<slug>[a-z0-9-]+)/items'; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get method arguments for this REST route. |
| 54 | * |
| 55 | * @return array |
| 56 | */ |
| 57 | public function get_args() { |
| 58 | return array( |
| 59 | 'args' => array( |
| 60 | 'slug' => array( |
| 61 | 'description' => __( 'Stable slug for the list.', 'woocommerce' ), |
| 62 | 'type' => 'string', |
| 63 | ), |
| 64 | ), |
| 65 | array( |
| 66 | 'methods' => \WP_REST_Server::READABLE, |
| 67 | 'callback' => array( $this, 'get_response' ), |
| 68 | 'permission_callback' => function () { |
| 69 | return is_user_logged_in(); |
| 70 | }, |
| 71 | 'args' => array( |
| 72 | 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
| 73 | ), |
| 74 | ), |
| 75 | array( |
| 76 | 'methods' => \WP_REST_Server::CREATABLE, |
| 77 | 'callback' => array( $this, 'get_response' ), |
| 78 | 'permission_callback' => function () { |
| 79 | return is_user_logged_in(); |
| 80 | }, |
| 81 | 'args' => array( |
| 82 | 'cart_item_key' => array( |
| 83 | 'description' => __( 'Existing cart item key to copy into the list.', 'woocommerce' ), |
| 84 | 'type' => 'string', |
| 85 | ), |
| 86 | 'product_id' => array( |
| 87 | 'description' => __( 'Product or variation ID to save. Required when cart_item_key is not supplied.', 'woocommerce' ), |
| 88 | 'type' => 'integer', |
| 89 | ), |
| 90 | 'variation' => array( |
| 91 | 'description' => __( 'Chosen attributes (for variations).', 'woocommerce' ), |
| 92 | 'type' => 'array', |
| 93 | 'context' => array( 'view', 'edit' ), |
| 94 | 'items' => array( |
| 95 | 'type' => 'object', |
| 96 | 'properties' => array( |
| 97 | 'attribute' => array( |
| 98 | 'description' => __( 'Variation attribute name.', 'woocommerce' ), |
| 99 | 'type' => 'string', |
| 100 | 'context' => array( 'view', 'edit' ), |
| 101 | ), |
| 102 | 'value' => array( |
| 103 | 'description' => __( 'Variation attribute value.', 'woocommerce' ), |
| 104 | 'type' => 'string', |
| 105 | 'context' => array( 'view', 'edit' ), |
| 106 | ), |
| 107 | ), |
| 108 | ), |
| 109 | ), |
| 110 | 'quantity' => array( |
| 111 | 'description' => __( 'Quantity for the saved item.', 'woocommerce' ), |
| 112 | 'type' => 'integer', |
| 113 | 'default' => 1, |
| 114 | ), |
| 115 | ), |
| 116 | ), |
| 117 | 'schema' => array( $this->schema, 'get_public_item_schema' ), |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Return the items in the requested list. |
| 123 | * |
| 124 | * @throws RouteException When the list doesn't exist. |
| 125 | * |
| 126 | * @param \WP_REST_Request $request Request object. |
| 127 | * |
| 128 | * @phpstan-param \WP_REST_Request<array<string, mixed>> $request |
| 129 | * |
| 130 | * @return \WP_REST_Response |
| 131 | */ |
| 132 | protected function get_route_response( \WP_REST_Request $request ) { |
| 133 | $list = ShopperList::get_by_slug( (string) $request['slug'] ); |
| 134 | if ( ! $list ) { |
| 135 | throw new RouteException( 'woocommerce_rest_shopper_list_not_found', esc_html__( 'Your saved list isn\'t available right now.', 'woocommerce' ), 404 ); |
| 136 | } |
| 137 | |
| 138 | $items = array_values( $list->get_items() ); |
| 139 | $this->prime_product_caches_for_items( $items ); |
| 140 | |
| 141 | $response = array(); |
| 142 | foreach ( $items as $item ) { |
| 143 | $response[] = $this->prepare_response_for_collection( $this->prepare_item_for_response( $item, $request ) ); |
| 144 | } |
| 145 | |
| 146 | return rest_ensure_response( $response ); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Add an item to the requested list from cart_item_key or direct product payload fields. |
| 151 | * |
| 152 | * @throws RouteException On validation failure. |
| 153 | * |
| 154 | * @param \WP_REST_Request $request Request object. |
| 155 | * |
| 156 | * @phpstan-param \WP_REST_Request<array<string, mixed>> $request |
| 157 | * |
| 158 | * @return \WP_REST_Response |
| 159 | */ |
| 160 | protected function get_route_post_response( \WP_REST_Request $request ) { |
| 161 | $list = ShopperList::get_by_slug( (string) $request['slug'] ); |
| 162 | |
| 163 | if ( ! $list ) { |
| 164 | throw new RouteException( 'woocommerce_rest_shopper_list_not_found', esc_html__( 'Your saved list isn\'t available right now.', 'woocommerce' ), 404 ); |
| 165 | } |
| 166 | |
| 167 | [ $lookup_id, $variation, $quantity ] = $this->resolve_item_payload( $request ); |
| 168 | |
| 169 | try { |
| 170 | $item = ShopperListItem::from_product( $lookup_id, $variation, $quantity ); |
| 171 | } catch ( \InvalidArgumentException $e ) { |
| 172 | throw new RouteException( 'woocommerce_rest_shopper_list_invalid_variation', esc_html( $e->getMessage() ), 400 ); |
| 173 | } |
| 174 | if ( ! $item ) { |
| 175 | throw new RouteException( 'woocommerce_rest_shopper_list_unknown_product', esc_html__( 'No product exists for the supplied item.', 'woocommerce' ), 404 ); |
| 176 | } |
| 177 | |
| 178 | $list->add_item( $item ); |
| 179 | $list->save(); |
| 180 | |
| 181 | $saved = $list->find_item( $item->get_key() ) ?? $item; |
| 182 | return new \WP_REST_Response( $this->schema->get_item_response( $saved ), 201 ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Resolve the POST input into a uniform payload (product lookup id, variation, quantity). |
| 187 | * |
| 188 | * Accepts either an existing cart_item_key, or direct product_id/variation_id/variation. |
| 189 | * |
| 190 | * @throws RouteException When neither a cart_item_key nor a product_id is supplied, or the cart_item_key is unknown. |
| 191 | * |
| 192 | * @param \WP_REST_Request $request Request object. |
| 193 | * |
| 194 | * @phpstan-param \WP_REST_Request<array<string, mixed>> $request |
| 195 | * |
| 196 | * @return array{0:int,1:array,2:int} `[ lookup_id, variation, quantity ]`. |
| 197 | */ |
| 198 | private function resolve_item_payload( \WP_REST_Request $request ): array { |
| 199 | $cart_item_key = (string) $request->get_param( 'cart_item_key' ); |
| 200 | |
| 201 | if ( $cart_item_key ) { |
| 202 | if ( ! did_action( 'woocommerce_load_cart_from_session' ) || ! wc()->cart ) { |
| 203 | wc_load_cart(); |
| 204 | } |
| 205 | |
| 206 | $cart_contents = wc()->cart->get_cart(); |
| 207 | if ( empty( $cart_contents[ $cart_item_key ] ) ) { |
| 208 | throw new RouteException( 'woocommerce_rest_shopper_list_invalid_cart_item_key', esc_html__( 'No cart item exists for the supplied key.', 'woocommerce' ), 404 ); |
| 209 | } |
| 210 | |
| 211 | $line = $cart_contents[ $cart_item_key ]; |
| 212 | $product_id = absint( $line['product_id'] ?? 0 ); |
| 213 | $variation_id = absint( $line['variation_id'] ?? 0 ); |
| 214 | $variation_attrs = isset( $line['variation'] ) && is_array( $line['variation'] ) ? $line['variation'] : array(); |
| 215 | |
| 216 | return array( |
| 217 | $variation_id ? $variation_id : $product_id, |
| 218 | $variation_attrs, |
| 219 | absint( $line['quantity'] ?? 1 ), |
| 220 | ); |
| 221 | }//end if |
| 222 | |
| 223 | $product_id = absint( $request->get_param( 'product_id' ) ); |
| 224 | if ( ! $product_id ) { |
| 225 | throw new RouteException( 'woocommerce_rest_shopper_list_missing_item_input', esc_html__( 'Provide cart_item_key or product_id.', 'woocommerce' ), 400 ); |
| 226 | } |
| 227 | |
| 228 | $variation = wp_list_pluck( (array) $request->get_param( 'variation' ), 'value', 'attribute' ); |
| 229 | |
| 230 | return array( |
| 231 | $product_id, |
| 232 | (array) array_combine( |
| 233 | array_map( 'wc_variation_attribute_name', array_keys( $variation ) ), |
| 234 | array_values( $variation ) |
| 235 | ), |
| 236 | absint( $request->get_param( 'quantity' ) ), |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Prime post caches before the per-item product lookup loop in the schema. |
| 242 | * |
| 243 | * @param ShopperListItem[] $items Items. |
| 244 | */ |
| 245 | private function prime_product_caches_for_items( array $items ): void { |
| 246 | $ids = array_map( |
| 247 | fn( $item ) => $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id(), |
| 248 | $items |
| 249 | ); |
| 250 | |
| 251 | _prime_post_caches( array_unique( $ids ) ); |
| 252 | } |
| 253 | } |
| 254 |