WC_Discounts::validate_coupon_excluded_items()protectedWC 3.2.0

All exclusion rules must pass at the same time for a product coupon to be valid.

Method of the class: WC_Discounts{}

No Hooks.

Return

true|false.

Usage

// protected - for code of main (parent) or child class
$result = $this->validate_coupon_excluded_items( $coupon );
$coupon(WC_Coupon) (required)
Coupon data.

Changelog

Since 3.2.0 Introduced.

WC_Discounts::validate_coupon_excluded_items() code WC 8.7.0

protected function validate_coupon_excluded_items( $coupon ) {
	$items = $this->get_items_to_validate();
	if ( ! empty( $items ) && $coupon->is_type( wc_get_product_coupon_types() ) ) {
		$valid = false;

		foreach ( $items as $item ) {
			if ( $item->product && $coupon->is_valid_for_product( $item->product, $item->object ) ) {
				$valid = true;
				break;
			}
		}

		if ( ! $valid ) {
			throw new Exception( __( 'Sorry, this coupon is not applicable to selected products.', 'woocommerce' ), 109 );
		}
	}

	return true;
}