skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search