Automattic\WooCommerce\StoreApi\Utilities

CartController::validate_add_to_cart()publicWC 1.0

Validate all items in the cart and check for errors.

Method of the class: CartController{}

Return

null. Nothing (null).

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.

CartController::validate_add_to_cart() code WC 8.6.1

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

	/**
	 * Filters if an item being added to the cart passed validation checks.
	 *
	 * 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.
	 *
	 * @since 7.2.0
	 *
	 * @deprecated
	 * @param boolean $passed_validation True if the item passed validation.
	 * @param integer $product_id Product ID being validated.
	 * @param integer $quantity Quantity added to the cart.
	 * @param integer $variation_id Variation ID being added to the cart.
	 * @param array $variation Variation data.
	 * @return boolean
	 */
	$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 );
	}

	/**
	 * Fires during validation when adding an item to the cart via the Store API.
	 *
	 * @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.
	 * @deprecated 7.1.0 Use woocommerce_store_api_validate_add_to_cart instead.
	 */
	wc_do_deprecated_action(
		'wooocommerce_store_api_validate_add_to_cart',
		array(
			$product,
			$request,
		),
		'7.1.0',
		'woocommerce_store_api_validate_add_to_cart',
		'This action was deprecated in WooCommerce Blocks version 7.1.0. Please use woocommerce_store_api_validate_add_to_cart instead.'
	);

	/**
	 * Fires during validation when adding an item to the cart via the Store API.
	 *
	 * Fire action to validate add to cart. Functions hooking into this should throw an \Exception to prevent
	 * add to cart from happening.
	 *
	 * @since 7.1.0
	 *
	 * @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( 'woocommerce_store_api_validate_add_to_cart', $product, $request );
}