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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / StoreApi / Routes / V1 / ShopperListItems.php
woocommerce / src / StoreApi / Routes / V1 Last commit date
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