Automattic\WooCommerce\StoreApi\Utilities

CartController::set_cart_item_quantity()publicWC 1.0

Based on core set_quantity method, but validates if an item is sold individually first and enforces any limits in place.

{} Это метод класса: CartController{}

Хуков нет.

Возвращает

null. Ничего.

Использование

$CartController = new CartController();
$CartController->set_cart_item_quantity( $item_id, $quantity );
$item_id(строка) (обязательный)
Cart item id.
$quantity(int)
Cart quantity.
По умолчанию: 1

Код CartController::set_cart_item_quantity() WC 6.8.0

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 );
	}

	$quantity_validation = ( new QuantityLimits() )->validate_cart_item_quantity( $quantity, $cart_item );

	if ( is_wp_error( $quantity_validation ) ) {
		throw new RouteException( $quantity_validation->get_error_code(), $quantity_validation->get_error_message(), 400 );
	}

	$cart = $this->get_cart_instance();
	$cart->set_quantity( $item_id, $quantity );
}