WordPress at Your Fingertips
function is not described

Automattic\WooCommerce\Blocks\StoreApi\Utilities

CartController::validate_cart_item() public WC 1.0

Validates an existing cart item and returns any errors.

{} It's a method of the class: CartController{}

Return

null. Nothing.

Usage

$CartController = new CartController();
$CartController->validate_cart_item( $cart_item );
$cart_item(array) (required)
Cart item array.

Code of CartController::validate_cart_item() WC 5.8.0

public function validate_cart_item( $cart_item ) {
	$product = $cart_item['data'];

	if ( ! $product instanceof \WC_Product ) {
		return;
	}

	if ( ! $product->is_purchasable() ) {
		throw new NotPurchasableException(
			'woocommerce_rest_cart_product_not_purchasable',
			$product->get_name()
		);
	}

	if ( $product->is_sold_individually() && $cart_item['quantity'] > 1 ) {
		throw new TooManyInCartException(
			'woocommerce_rest_cart_product_sold_individually',
			$product->get_name()
		);
	}

	if ( ! $product->is_in_stock() ) {
		throw new OutOfStockException(
			'woocommerce_rest_cart_product_no_stock',
			$product->get_name()
		);
	}

	if ( $product->managing_stock() && ! $product->backorders_allowed() ) {
		$qty_remaining = $this->get_remaining_stock_for_product( $product );
		$qty_in_cart   = $this->get_product_quantity_in_cart( $product );

		if ( $qty_remaining < $qty_in_cart ) {
			throw new PartialOutOfStockException(
				'woocommerce_rest_cart_product_partially_no_stock',
				$product->get_name()
			);
		}
	}

	/**
	 * Fire action to validate add to cart. Functions hooking into this should throw an \Exception to prevent
	 * add to cart from occurring.
	 *
	 * @param \WC_Product $product Product object being added to the cart.
	 * @param array       $cart_item Cart item array.
	 */
	do_action( 'wooocommerce_store_api_validate_cart_item', $product, $cart_item );
}