Automattic\WooCommerce\StoreApi\Utilities

CartController::validate_cart()publicWC 1.0

Validate cart and check for errors.

Method of the class: CartController{}

Return

null. Nothing (null).

Usage

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

CartController::validate_cart() code WC 8.7.0

public function validate_cart() {
	$this->validate_cart_items();
	$this->validate_cart_coupons();

	$cart        = $this->get_cart_instance();
	$cart_errors = new WP_Error();

	/**
	 * Fires an action to validate the cart.
	 *
	 * Functions hooking into this should add custom errors using the provided WP_Error instance.
	 *
	 * @since 7.2.0
	 *
	 * @example See docs/examples/validate-cart.md
	 *
	 * @param \WP_Error $errors  WP_Error object.
	 * @param \WC_Cart  $cart    Cart object.
	 */
	do_action( 'woocommerce_store_api_cart_errors', $cart_errors, $cart );

	if ( $cart_errors->has_errors() ) {
		throw new InvalidCartException(
			'woocommerce_cart_error',
			$cart_errors,
			409
		);
	}

	// 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 );

	/**
	 * Fires when cart items are being validated.
	 *
	 * 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 wp errors instead.
	 *
	 * @since 7.2.0
	 *
	 * @deprecated
	 * @internal Matches action name in WooCommerce core.
	 */
	do_action( 'woocommerce_check_cart_items' );

	$cart_errors = NoticeHandler::convert_notices_to_wp_errors( 'woocommerce_rest_cart_item_error' );

	if ( $cart_errors->has_errors() ) {
		throw new InvalidCartException(
			'woocommerce_cart_error',
			$cart_errors,
			409
		);
	}
}