WordPress at Your Fingertips
function is not described

Automattic\WooCommerce\Blocks\StoreApi\Utilities

CartController::validate_cart_items() public WC 1.0

Validate all items in the cart and check for errors.

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

Hooks from the method
Return

null. Nothing.

Usage

$CartController = new CartController();
$CartController->validate_cart_items();

Code of CartController::validate_cart_items() WC 5.6.0

public function validate_cart_items() {
	$cart       = $this->get_cart_instance();
	$cart_items = $this->get_cart_items();

	$out_of_stock_products         = [];
	$too_many_in_cart_products     = [];
	$partial_out_of_stock_products = [];
	$not_purchasable_products      = [];

	foreach ( $cart_items as $cart_item_key => $cart_item ) {
		try {
			$this->validate_cart_item( $cart_item );
		} catch ( TooManyInCartException $error ) {
			$too_many_in_cart_products[] = $error;
		} catch ( NotPurchasableException $error ) {
			$not_purchasable_products[] = $error;
		} catch ( PartialOutOfStockException $error ) {
			$partial_out_of_stock_products[] = $error;
		} catch ( OutOfStockException $error ) {
			$out_of_stock_products[] = $error;
		}
	}

	$error = new WP_Error();

	if ( count( $out_of_stock_products ) > 0 ) {
		$singular_error = $this->get_error_message_for_stock_exception_type( 'out_of_stock', 'singular' );
		$plural_error   = $this->get_error_message_for_stock_exception_type( 'out_of_stock', 'plural' );

		$error->add(
			409,
			$this->add_product_names_to_message( $singular_error, $plural_error, $out_of_stock_products )
		);
	}

	if ( count( $not_purchasable_products ) > 0 ) {
		$singular_error = $this->get_error_message_for_stock_exception_type( 'not_purchasable', 'singular' );
		$plural_error   = $this->get_error_message_for_stock_exception_type( 'not_purchasable', 'plural' );

		$error->add(
			409,
			$this->add_product_names_to_message( $singular_error, $plural_error, $not_purchasable_products )
		);
	}

	if ( count( $too_many_in_cart_products ) > 0 ) {
		$singular_error = $this->get_error_message_for_stock_exception_type( 'too_many_in_cart', 'singular' );
		$plural_error   = $this->get_error_message_for_stock_exception_type( 'too_many_in_cart', 'plural' );

		$error->add(
			409,
			$this->add_product_names_to_message( $singular_error, $plural_error, $too_many_in_cart_products )
		);
	}

	if ( count( $partial_out_of_stock_products ) > 0 ) {
		$singular_error = $this->get_error_message_for_stock_exception_type( 'partial_out_of_stock', 'singular' );
		$plural_error   = $this->get_error_message_for_stock_exception_type( 'partial_out_of_stock', 'plural' );

		$error->add(
			409,
			$this->add_product_names_to_message( $singular_error, $plural_error, $partial_out_of_stock_products )
		);
	}

	if ( $error->has_errors() ) {
		throw new InvalidStockLevelsInCartException(
			'woocommerce_stock_availability_error',
			$error
		);
	}

	// Before running the woocommerce_check_cart_items hook, unhook validation from the core cart.
	remove_action( 'woocommerce_check_cart_items', array( $cart, 'check_cart_items' ), 1 );
	remove_action( 'woocommerce_check_cart_items', array( $cart, 'check_cart_coupons' ), 1 );

	/**
	 * Hook: woocommerce_check_cart_items
	 *
	 * Allow 3rd parties to validate cart items. 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.
	 */
	do_action( 'woocommerce_check_cart_items' );
	NoticeHandler::convert_notices_to_exceptions( 'woocommerce_rest_cart_item_error' );
}