You appear to be a bot. Output may be restricted
Description
Based on core set_quantity
method, but validates if an item is sold individually first.
Usage
CartController::set_cart_item_quantity( $item_id, $quantity );
Parameters
- $item_id
- ( string ) required – Cart item id.
- $quantity
- ( integer ) optional default: 1 – Cart quantity.
Returns
void
Source
File name: woocommerce/packages/woocommerce-blocks/src/StoreApi/Utilities/CartController.php
Lines:
1 to 27 of 27
public function set_cart_item_quantity( $item_id, $quantity = 1 ) { $cart_item = $this->get_cart_item( $item_id ); if ( empty( $cart_item ) ) { throw new RouteException( 'woocommerce_rest_cart_invalid_key', __( 'Cart item does not exist.', 'woocommerce' ), 404 ); } $product = $cart_item['data']; if ( ! $product instanceof \WC_Product ) { throw new RouteException( 'woocommerce_rest_cart_invalid_product', __( 'Cart item is invalid.', 'woocommerce' ), 404 ); } if ( $product->is_sold_individually() && $quantity > 1 ) { throw new RouteException( 'woocommerce_rest_cart_product_sold_individually', sprintf( /* translators: %s: product name */ __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product->get_name() ), 400 ); } $cart = $this->get_cart_instance(); $cart->set_quantity( $item_id, $quantity ); }