Search code examples
phpwordpresswoocommercecoupon

Is there a hook or plugin to exclude certain product-tags from a woocommerce coupon?


I need to exclude a product tag from a woocommerce coupon.

I've tried to search Google for an applicable plugin, but came up empty.

Ideally, a solution would be a plugin that adds a custom field to the coupon post type. However, I'll settle for hard coding through a hook/filter.


Solution

  • There is a filter you can use to accomplish this woocommerce_coupon_is_valid_for_product.

    It accepts 4 parameters, valid, the product, the coupon and values. It's called from class-wc-coupon.php around line 860 in version 3 or so. It should return a boolean (true/false).

    Note that the code below is incomplete and untested, and is for example purposes only.

    add_filter('woocommerce_coupon_is_valid_for_product', 'exclude_product_from_coupon_by_tag', 12, 4);
    function exclude_product_from_coupon_by_tag($valid, $product, $coupon, $values ){
        
        //Check if product has tag/s
        $valid = has_term('INSERT_TERM_HERE', 'product_tag', $product);
    
        return $valid;
    }
    

    Hope that helps!