33
44use Automattic \WooCommerce \Blocks \StoreApi \Routes \RouteException ;
55use Automattic \WooCommerce \Blocks \StoreApi \Utilities \NoticeHandler ;
6- use Automattic \WooCommerce \Blocks \Utils \ArrayUtils ;
76use Automattic \WooCommerce \Checkout \Helpers \ReserveStock ;
8- use WP_Error ;
97
108/**
119 * Woo Cart Controller class.
@@ -215,130 +213,17 @@ public function validate_add_to_cart( \WC_Product $product, $request ) {
215213 do_action ( 'wooocommerce_store_api_validate_add_to_cart ' , $ product , $ request );
216214 }
217215
218- /**
219- * Generates the error message for out of stock products and adds product names to it.
220- *
221- * @param string $singular The message to use when only one product is in the list.
222- * @param string $plural The message to use when more than one product is in the list.
223- * @param array $items The list of cart items whose names should be inserted into the message.
224- * @returns string The translated and correctly pluralised message.
225- */
226- private function add_product_names_to_message ( $ singular , $ plural , $ items ) {
227- $ product_names = wc_list_pluck ( $ items , 'getProductName ' );
228- $ message = ( count ( $ items ) > 1 ) ? $ plural : $ singular ;
229- return sprintf (
230- $ message ,
231- ArrayUtils::natural_language_join ( $ product_names , true )
232- );
233- }
234-
235216 /**
236217 * Validate all items in the cart and check for errors.
237218 *
238- * @throws InvalidStockLevelsInCartException Exception if invalid data is detected due to insufficient stock levels .
219+ * @throws RouteException Exception if invalid data is detected.
239220 */
240221 public function validate_cart_items () {
241222 $ cart = $ this ->get_cart_instance ();
242223 $ cart_items = $ this ->get_cart_items ();
243224
244- $ out_of_stock_products = [];
245- $ too_many_in_cart_products = [];
246- $ partial_out_of_stock_products = [];
247- $ not_purchasable_products = [];
248-
249225 foreach ( $ cart_items as $ cart_item_key => $ cart_item ) {
250- try {
251- $ this ->validate_cart_item ( $ cart_item );
252- } catch ( TooManyInCartException $ error ) {
253- $ too_many_in_cart_products [] = $ error ;
254- } catch ( NotPurchasableException $ error ) {
255- $ not_purchasable_products [] = $ error ;
256- } catch ( PartialOutOfStockException $ error ) {
257- $ partial_out_of_stock_products [] = $ error ;
258- } catch ( OutOfStockException $ error ) {
259- $ out_of_stock_products [] = $ error ;
260- }
261- }
262-
263- $ error = new WP_Error ();
264-
265- if ( count ( $ out_of_stock_products ) > 0 ) {
266- // translators: %s: product names.
267- $ singular_error = __ (
268- '%s is out of stock and cannot be purchased. It has been removed from your cart. ' ,
269- 'woo-gutenberg-products-block '
270- );
271- // translators: %s: product names.
272- $ plural_error = __ (
273- '%s are out of stock and cannot be purchased. They have been removed from your cart. ' ,
274- 'woo-gutenberg-products-block '
275- );
276-
277- $ error ->add (
278- 409 ,
279- $ this ->add_product_names_to_message ( $ singular_error , $ plural_error , $ out_of_stock_products )
280- );
281- }
282-
283- if ( count ( $ not_purchasable_products ) > 0 ) {
284- // translators: %s: product names.
285- $ singular_error = __ (
286- '%s cannot be purchased. It has been removed from your cart. ' ,
287- 'woo-gutenberg-products-block '
288- );
289- // translators: %s: product names.
290- $ plural_error = __ (
291- '%s cannot be purchased. They have been removed from your cart. ' ,
292- 'woo-gutenberg-products-block '
293- );
294-
295- $ error ->add (
296- 409 ,
297- $ this ->add_product_names_to_message ( $ singular_error , $ plural_error , $ not_purchasable_products )
298- );
299- }
300-
301- if ( count ( $ too_many_in_cart_products ) > 0 ) {
302- // translators: %s: product names.
303- $ singular_error = __ (
304- 'There are too many %s in the cart. Only 1 can be purchased. The quantity in your cart has been reduced. ' ,
305- 'woo-gutenberg-products-block '
306- );
307- // translators: %s: product names.
308- $ plural_error = __ (
309- 'There are too many %s in the cart. Only 1 of each can be purchased. The quantities in your cart have been reduced. ' ,
310- 'woo-gutenberg-products-block '
311- );
312-
313- $ error ->add (
314- 409 ,
315- $ this ->add_product_names_to_message ( $ singular_error , $ plural_error , $ too_many_in_cart_products )
316- );
317- }
318-
319- if ( count ( $ partial_out_of_stock_products ) > 0 ) {
320- // translators: %s: product names.
321- $ singular_error = __ (
322- 'There is not enough %s in stock. The quantity in your cart has been reduced. ' ,
323- 'woo-gutenberg-products-block '
324- );
325- // translators: %s: product names.
326- $ plural_error = __ (
327- 'There are not enough %s in stock. The quantities in your cart have been reduced. ' ,
328- 'woo-gutenberg-products-block '
329- );
330-
331- $ error ->add (
332- 409 ,
333- $ this ->add_product_names_to_message ( $ singular_error , $ plural_error , $ partial_out_of_stock_products )
334- );
335- }
336-
337- if ( $ error ->has_errors () ) {
338- throw new InvalidStockLevelsInCartException (
339- 'woocommerce_stock_availability_error ' ,
340- $ error
341- );
226+ $ this ->validate_cart_item ( $ cart_item );
342227 }
343228
344229 // Before running the woocommerce_check_cart_items hook, unhook validation from the core cart.
@@ -359,11 +244,8 @@ public function validate_cart_items() {
359244 /**
360245 * Validates an existing cart item and returns any errors.
361246 *
362- * @throws TooManyInCartException Exception if more than one product that can only be purchased individually is in
363- * the cart.
364- * @throws PartialOutOfStockException Exception if an item has a quantity greater than what is available in stock.
365- * @throws OutOfStockException Exception thrown when an item is entirely out of stock.
366- * @throws NotPurchasableException Exception thrown when an item is not purchasable.
247+ * @throws RouteException Exception if invalid data is detected.
248+ *
367249 * @param array $cart_item Cart item array.
368250 */
369251 public function validate_cart_item ( $ cart_item ) {
@@ -374,25 +256,30 @@ public function validate_cart_item( $cart_item ) {
374256 }
375257
376258 if ( ! $ product ->is_purchasable () ) {
377- throw new NotPurchasableException (
378- 'woocommerce_rest_cart_product_not_purchasable ' ,
379- $ product ->get_name ()
380- );
259+ $ this ->throw_default_product_exception ( $ product );
381260 }
382261
383262 if ( $ product ->is_sold_individually () && $ cart_item ['quantity ' ] > 1 ) {
384- WC ()->cart ->set_quantity ( $ cart_item ['key ' ], 1 , false );
385- throw new TooManyInCartException (
263+ throw new RouteException (
386264 'woocommerce_rest_cart_product_sold_individually ' ,
387- $ product ->get_name ()
265+ sprintf (
266+ /* translators: %s: product name */
267+ __ ( 'There are too many "%s" in the cart. Only 1 can be purchased. ' , 'woo-gutenberg-products-block ' ),
268+ $ product ->get_name ()
269+ ),
270+ 400
388271 );
389272 }
390273
391274 if ( ! $ product ->is_in_stock () ) {
392- WC ()->cart ->remove_cart_item ( $ cart_item ['key ' ] );
393- throw new OutOfStockException (
275+ throw new RouteException (
394276 'woocommerce_rest_cart_product_no_stock ' ,
395- $ product ->get_name ()
277+ sprintf (
278+ /* translators: %s: product name */
279+ __ ( '"%s" is out of stock and cannot be purchased. ' , 'woo-gutenberg-products-block ' ),
280+ $ product ->get_name ()
281+ ),
282+ 400
396283 );
397284 }
398285
@@ -401,11 +288,20 @@ public function validate_cart_item( $cart_item ) {
401288 $ qty_in_cart = $ this ->get_product_quantity_in_cart ( $ product );
402289
403290 if ( $ qty_remaining < $ qty_in_cart ) {
404-
405- WC ()->cart ->set_quantity ( $ cart_item ['key ' ], $ qty_remaining , false );
406- throw new PartialOutOfStockException (
407- 'woocommerce_rest_cart_product_partially_no_stock ' ,
408- $ product ->get_name ()
291+ throw new RouteException (
292+ 'woocommerce_rest_cart_product_no_stock ' ,
293+ sprintf (
294+ /* translators: 1: quantity in stock, 2: product name */
295+ _n (
296+ 'There is only %1$s unit of "%2$s" in stock. ' ,
297+ 'There are only %1$s units of "%2$s" in stock. ' ,
298+ $ qty_remaining ,
299+ 'woo-gutenberg-products-block '
300+ ),
301+ wc_format_stock_quantity_for_display ( $ qty_remaining , $ product ),
302+ $ product ->get_name ()
303+ ),
304+ 400
409305 );
410306 }
411307 }
@@ -447,9 +343,7 @@ public function get_cart_item_errors() {
447343 try {
448344 $ this ->validate_cart_item ( $ cart_item );
449345 } catch ( RouteException $ error ) {
450- $ errors [] = new WP_Error ( $ error ->getErrorCode (), $ error ->getMessage () );
451- } catch ( StockAvailabilityException $ error ) {
452- $ errors [] = new WP_Error ( $ error ->getErrorCode (), $ error ->getMessage () );
346+ $ errors [] = new \WP_Error ( $ error ->getErrorCode (), $ error ->getMessage () );
453347 }
454348 }
455349
0 commit comments