Automattic\WooCommerce\StoreApi\Utilities

CartController::validate_cart_items()publicWC 1.0

Validate all items in the cart and check for errors.

Method of the class: CartController{}

No Hooks.

Return

null. Nothing (null).

Usage

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

CartController::validate_cart_items() code WC 8.6.1

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

	$errors                        = [];
	$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 ( RouteException $error ) {
			$errors[] = new WP_Error( $error->getErrorCode(), $error->getMessage(), $error->getAdditionalData() );
		} 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;
		}
	}

	if ( count( $errors ) > 0 ) {

		$error = new WP_Error();
		foreach ( $errors as $wp_error ) {
			$error->merge_from( $wp_error );
		}

		throw new InvalidCartException(
			'woocommerce_cart_error',
			$error,
			409
		);
	}

	$error = $this->stock_exceptions_to_wp_errors( $too_many_in_cart_products, $not_purchasable_products, $partial_out_of_stock_products, $out_of_stock_products );
	if ( $error->has_errors() ) {

		throw new InvalidCartException(
			'woocommerce_stock_availability_error',
			$error,
			409
		);
	}
}