Fix - add validate_excluded_items function to validate product coupons before applying them

This commit is contained in:
Mike Jolley 2016-06-27 15:02:32 +01:00
parent 8a46831eb1
commit 0ed58784fd
1 changed files with 21 additions and 0 deletions

View File

@ -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 ) ) {