Fix - Missing validation rule for product cat exclusion coupons (validate_excluded_product_categories)

Fixes #10058
This commit is contained in:
Mike Jolley 2016-01-12 09:49:46 +00:00
parent b5146f18fa
commit e15c123510
1 changed files with 26 additions and 0 deletions

View File

@ -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();