Automattic\WooCommerce\StoreApi\Utilities
QuantityLimits::validate_cart_item_quantity
Check that a given quantity is valid according to any limits in place.
Method of the class: QuantityLimits{}
No Hooks.
Returns
\WP_Error|true.
Usage
$QuantityLimits = new QuantityLimits(); $QuantityLimits->validate_cart_item_quantity( $quantity, $cart_item );
- $quantity(int|float) (required)
- Quantity to validate.
- $cart_item(array) (required)
- Cart item.
QuantityLimits::validate_cart_item_quantity() QuantityLimits::validate cart item quantity code WC 10.4.3
public function validate_cart_item_quantity( $quantity, $cart_item ) {
$limits = $this->get_cart_item_quantity_limits( $cart_item );
$product = $cart_item['data'] ?? false;
$quantity = wc_stock_amount( $quantity );
if ( ! $product instanceof \WC_Product ) {
return true;
}
if ( ! $limits['editable'] && $quantity > $limits['maximum'] ) {
/* translators: 1: product name */
return new \WP_Error( 'readonly_quantity', sprintf( __( 'The quantity of "%1$s" cannot be changed', 'woocommerce' ), $product->get_name() ) );
}
if ( $quantity < $limits['minimum'] ) {
/* translators: 1: product name 2: minimum quantity */
return new \WP_Error( 'invalid_quantity', sprintf( __( 'The minimum quantity of "%1$s" allowed in the cart is %2$s', 'woocommerce' ), $product->get_name(), $limits['minimum'] ) );
}
if ( $quantity > $limits['maximum'] ) {
/* translators: 1: product name 2: maximum quantity */
return new \WP_Error( 'invalid_quantity', sprintf( __( 'The maximum quantity of "%1$s" allowed in the cart is %2$s', 'woocommerce' ), $product->get_name(), $limits['maximum'] ) );
}
if ( ! $this->is_multiple_of( $quantity, NumberUtil::normalize( $limits['multiple_of'] ) ) ) {
/* translators: 1: product name 2: multiple of */
return new \WP_Error( 'invalid_quantity', sprintf( __( 'The quantity of "%1$s" must be a multiple of %2$s', 'woocommerce' ), $product->get_name(), $limits['multiple_of'] ) );
}
return true;
}