3

I am trying to exclude some product variations that have specific product attributes from coupon discounts.

In my case I am targeting product variations that have a product attribute "finish" set to "Classic Frame" or "Box Frame" term.

I tried to use "Exclude variations with 2 specific attribute terms from coupon usage in Woocommerce" answer code and it does indeed make the coupon invalid when a product variation has a product attribute "finish" set to "Classic Frame" or "Box Frame" term.

But it also makes the coupon invalid for any other items added to the cart.

So for example if the cart has 3 items in it:

  • Item 1: has the attribute "Classic Frame" ==> Coupon is Not Valid for this Item
  • Item 2: has the attribute "Box Frame" ==> Coupon is Not Valid for this Item
  • Item 3: has the attribute "Loose" ==> Coupon should be VALID for this Item

So what I would like is that the coupon discount is only applied to other items.

Any help is really appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
MitchellK
  • 2,322
  • 1
  • 16
  • 25

2 Answers2

3

The purpose of "Exclude variations with 2 specific attribute terms from coupon usage in Woocommerce" answer code is to make a coupon invalid if there is any product variation that has 'Classic Frame' or 'Box Frame' term set for 'Finish' product attribute (for variations).

Yes it makes the coupon invalid, even if there is other items without it, and it's the right behavior.

Why?

  • Because (global cart) coupons with a "Percentage discount" or a "Fixed cart discount" don't allow to be applied only to some cart items meaning some product variations and not to others, when using $is_valid property.

  • Same thing if you use the "exclude product IDS" coupon property, as it doesn't handle product variation IDs.

Maybe a commercial plugin exist that extends coupon possibilities, allowing to exclude product variations IDS (adding a usable filter hook) to that extended feature.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks Loic argghh, variations are a nightmare but I have no choice. I need to figure something out. Can offer customers 25% off of only certain variations for next 6 days, already lost today. – MitchellK Sep 12 '19 at 17:17
  • 2
    Actually I think I can make this work. I hope. I'll just have to pop-up a message telling them to remove any product from their cart that are not on the store wide sale of X variation. – MitchellK Sep 12 '19 at 17:34
  • 1
    Yes that can be a way… The thing is that WooCommerce coupons don't handle to exclude product variation IDS from discounts. That should be the needed feature. – LoicTheAztec Sep 12 '19 at 17:46
0

You can use this filter

function filter_woocommerce_coupon_get_discount_amount( $discounting_amount, $price_to_discount , $cart_item, $single, $coupon ) {    
    // On backorder
    if ( $cart_item['data']->get_attribute('attribute') !== 'attribute_value' ) {
        $discounting_amount = 0;
    }
    return $discounting_amount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );