Automattic\WooCommerce\StoreApi\Utilities

CartController::validate_cart_coupon()protectedWC 1.0

Validates an existing cart coupon and returns any errors.

Method of the class: CartController{}

No Hooks.

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->validate_cart_coupon( $coupon );
$coupon(\WC_Coupon) (required)
Coupon object applied to the cart.

CartController::validate_cart_coupon() code WC 9.3.1

protected function validate_cart_coupon( \WC_Coupon $coupon ) {
	if ( ! $coupon->is_valid() ) {
		$cart = $this->get_cart_instance();
		$cart->remove_coupon( $coupon->get_code() );
		$cart->calculate_totals();
		throw new RouteException(
			'woocommerce_rest_cart_coupon_error',
			sprintf(
				/* translators: %1$s coupon code, %2$s reason. */
				__( 'The "%1$s" coupon has been removed from your cart: %2$s', 'woocommerce' ),
				$coupon->get_code(),
				wp_strip_all_tags( $coupon->get_error_message() )
			),
			409
		);
	}
}