WordPress at Your Fingertips
function is not described

Automattic\WooCommerce\Blocks\StoreApi\Utilities

CartController::validate_add_to_cart() public WC 1.0

Validate all items in the cart and check for errors.

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

Return

null. Nothing.

Usage

$CartController = new CartController();
$CartController->validate_add_to_cart( $product, $request );
$product(WC_Product) (required)
Product object associated with the cart item.
$request(array) (required)
Add to cart request params.

Code of CartController::validate_add_to_cart() WC 5.8.0

public function validate_add_to_cart( \WC_Product $product, $request ) {
	if ( ! $product->is_purchasable() ) {
		$this->throw_default_product_exception( $product );
	}

	if ( ! $product->is_in_stock() ) {
		throw new RouteException(
			'woocommerce_rest_cart_product_no_stock',
			sprintf(
				/* translators: %s: product name */
				__( 'You cannot add "%s" to the cart because the product is out of stock.', 'woocommerce' ),
				$product->get_name()
			),
			400
		);
	}

	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 + $request['quantity'] ) {
			throw new RouteException(
				'woocommerce_rest_cart_product_no_stock',
				sprintf(
					/* translators: 1: product name 2: quantity in stock */
					__( 'You cannot add that amount of &quot;%1$s&quot; to the cart because there is not enough stock (%2$s remaining).', 'woocommerce' ),
					$product->get_name(),
					wc_format_stock_quantity_for_display( $qty_remaining, $product )
				),
				400
			);
		}
	}

	/**
	 * Hook: woocommerce_add_to_cart_validation (legacy).
	 *
	 * Allow 3rd parties to validate if an item can be added to the cart. This is a legacy hook from Woo core.
	 * This filter will be deprecated because it encourages usage of wc_add_notice. For the API we need to capture
	 * notices and convert to exceptions instead.
	 */
	$passed_validation = apply_filters(
		'woocommerce_add_to_cart_validation',
		true,
		$this->get_product_id( $product ),
		$request['quantity'],
		$this->get_variation_id( $product ),
		$request['variation']
	);

	if ( ! $passed_validation ) {
		// Validation did not pass - see if an error notice was thrown.
		NoticeHandler::convert_notices_to_exceptions( 'woocommerce_rest_add_to_cart_error' );

		// If no notice was thrown, throw the default notice instead.
		$this->throw_default_product_exception( $product );
	}

	/**
	 * Fire action to validate add to cart. Functions hooking into this should throw an \Exception to prevent
	 * add to cart from occuring.
	 *
	 * @param \WC_Product $product Product object being added to the cart.
	 * @param array       $request Add to cart request params including id, quantity, and variation attributes.
	 */
	do_action( 'wooocommerce_store_api_validate_add_to_cart', $product, $request );
}