From e15c1235108ca08984a82c6c5121b752ccd96a2c Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 12 Jan 2016 09:49:46 +0000 Subject: [PATCH] Fix - Missing validation rule for product cat exclusion coupons (validate_excluded_product_categories) Fixes #10058 --- includes/class-wc-coupon.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/includes/class-wc-coupon.php b/includes/class-wc-coupon.php index cbf949dedd8..9452387a177 100644 --- a/includes/class-wc-coupon.php +++ b/includes/class-wc-coupon.php @@ -444,6 +444,7 @@ class WC_Coupon { foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $product_cats = wc_get_product_cat_ids( $cart_item['product_id'] ); + // If we find an item with a cat in our allowed cat list, the coupon is valid if ( sizeof( array_intersect( $product_cats, $this->product_categories ) ) > 0 ) { $valid_for_cart = true; } @@ -455,6 +456,30 @@ class WC_Coupon { } } + /** + * Ensure coupon is valid for product categories in the cart is valid or throw exception. + * + * @throws Exception + */ + private function validate_excluded_product_categories() { + if ( sizeof( $this->exclude_product_categories ) > 0 ) { + $valid_for_cart = false; + if ( ! WC()->cart->is_empty() ) { + foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { + $product_cats = wc_get_product_cat_ids( $cart_item['product_id'] ); + + // If we find an item with a cat NOT in our disallowed cat list, the coupon is valid + if ( empty( $product_cats ) || sizeof( array_diff( $product_cats, $this->exclude_product_categories ) ) > 0 ) { + $valid_for_cart = true; + } + } + } + if ( ! $valid_for_cart ) { + throw new Exception( self::E_WC_COUPON_NOT_APPLICABLE ); + } + } + } + /** * Ensure coupon is valid for sale items in the cart is valid or throw exception. * @@ -581,6 +606,7 @@ class WC_Coupon { $this->validate_maximum_amount(); $this->validate_product_ids(); $this->validate_product_categories(); + $this->validate_excluded_product_categories(); $this->validate_sale_items(); $this->validate_cart_excluded_items();