Automattic\WooCommerce\StoreApi\Utilities
QuantityLimits::validate_cart_item_quantity
Check that a given quantity is valid according to any limits in place.
Метод класса: QuantityLimits{}
Хуки из метода
Возвращает
\WP_Error|true.
Использование
$QuantityLimits = new QuantityLimits(); $QuantityLimits->validate_cart_item_quantity( $quantity, $cart_item );
- $quantity(int|float) (обязательный)
- Quantity to validate.
- $cart_item(массив) (обязательный)
- Cart item.
Код QuantityLimits::validate_cart_item_quantity() QuantityLimits::validate cart item quantity WC 11.2.2
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'] ) );
}
/**
* Filters the validation result for a cart item quantity being updated via the Store API.
*
* Return a \WP_Error to reject the new quantity; the Store API sends its code and message in a 400
* response. Throwing a RouteException works too. Any other return value, including false, is
* ignored and the quantity is accepted. Notices added with wc_add_notice() are not read here.
* Core validation failures (min, max, multiple_of, read-only), and cart items whose data key is
* not a WC_Product, return early and never reach this filter.
*
* This does not run when a product is first added to the cart; use the
* woocommerce_store_api_validate_add_to_cart action for that. When an already-in-cart item is
* topped up, $quantity is the new total while $cart_item['quantity'] is still the pre-existing
* quantity.
*
* @since 11.2.0
*
* @param true $valid Always true; core validation failures bypass this filter.
* @param int|float $quantity The new quantity, already normalized through wc_stock_amount().
* @param \WC_Product $product The product object.
* @param array $cart_item Cart item.
* @return \WP_Error|true
*/
$valid = apply_filters( 'woocommerce_store_api_cart_item_quantity_validation', true, $quantity, $product, $cart_item );
// Like the other filters in this class, unexpected return values fall back to the original value.
return is_wp_error( $valid ) ? $valid : true;
}