'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 1:[1]
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!
Solution 2:[2]
As Seb Toombs said you must use woocommerce_coupon_is_valid_for_product filter to check product validation by tag. If the validation returns you an error you must change the $product with $product->get_id()
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->get_id());
return $valid;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Lefteris Harissopoulos |
