Automattic\WooCommerce\StoreApi\Utilities

QuantityLimits::validate_cart_item_quantity()publicWC 1.0

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) (обязательный)
Quantity to validate.
$cart_item(\WC_Product|массив) (обязательный)
Cart item.

Код QuantityLimits::validate_cart_item_quantity() WC 6.8.0

public function validate_cart_item_quantity( $quantity, $cart_item ) {
	$limits = $this->get_cart_item_quantity_limits( $cart_item );

	if ( ! $limits['editable'] ) {
		return new \WP_Error(
			'readonly_quantity',
			__( 'This item is already in the cart and its quantity cannot be edited', 'woocommerce' )
		);
	}

	if ( $quantity < $limits['minimum'] ) {
		return new \WP_Error(
			'invalid_quantity',
			sprintf(
				// Translators: %s amount.
				__( 'The minimum quantity that can be added to the cart is %s', 'woocommerce' ),
				$limits['minimum']
			)
		);
	}

	if ( $quantity > $limits['maximum'] ) {
		return new \WP_Error(
			'invalid_quantity',
			sprintf(
				// Translators: %s amount.
				__( 'The maximum quantity that can be added to the cart is %s', 'woocommerce' ),
				$limits['maximum']
			)
		);
	}

	if ( $quantity % $limits['multiple_of'] ) {
		return new \WP_Error(
			'invalid_quantity',
			sprintf(
				// Translators: %s amount.
				__( 'The quantity added to the cart must be a multiple of %s', 'woocommerce' ),
				$limits['multiple_of']
			)
		);
	}

	return true;
}