From 0ed58784fdf258d98e3ba407c15b85919d8115d1 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 27 Jun 2016 15:02:32 +0100 Subject: [PATCH] Fix - add validate_excluded_items function to validate product coupons before applying them --- includes/class-wc-coupon.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/includes/class-wc-coupon.php b/includes/class-wc-coupon.php index 752054f2620..c8f0ca97c7b 100644 --- a/includes/class-wc-coupon.php +++ b/includes/class-wc-coupon.php @@ -482,6 +482,26 @@ class WC_Coupon { } } + /** + * All exclusion rules must pass at the same time for a product coupon to be valid. + */ + private function validate_excluded_items() { + if ( ! WC()->cart->is_empty() ) { + $valid = false; + + foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { + if ( $this->is_valid_for_product( $cart_item['data'], $cart_item ) ) { + $valid = true; + break; + } + } + + if ( ! $valid ) { + throw new Exception( self::E_WC_COUPON_NOT_APPLICABLE ); + } + } + } + /** * Cart discounts cannot be added if non-eligble product is found in cart. */ @@ -582,6 +602,7 @@ class WC_Coupon { $this->validate_product_ids(); $this->validate_product_categories(); $this->validate_sale_items(); + $this->validate_excluded_items(); $this->validate_cart_excluded_items(); if ( ! apply_filters( 'woocommerce_coupon_is_valid', true, $this ) ) {