Exclude a product from a WooCommerce coupon so it never goes on sale!
WooCommerce allows us to set which or which category of products a particular coupon is allowed to be used on, but there is no way to set it to not allow a particular product to be used.
Join We have a product that is already sold at cost, if we then allow coupons for this item, for this item, we are definitely losing money.WooCommerce has a woocommerce_coupon_is_valid_for_product Filters can help us exclude a product from a coupon so that it never goes on sale.
The PHP code that implements this functionality
The PHP code that implements this functionality is simple, and is found in the woocommerce_coupon_is_valid_for_product In the callback of the filter, we determine the product ID that needs to be excluded, and the product ID provided by this filter, if it is the same, we can just return false directly.
add_filter('woocommerce_coupon_is_valid_for_product', function ($valid, $product, $coupon, $values)
{
// 这里的12345就是我们不想让打折的产品
if (12345 == $product->get_id()) {
$valid = false;
}
return $valid;
}, 9999, 4);Based on the approach in this paper, in addition to restricting the use of coupons to a certain ID, we can also restrict the use of coupons to a certain category, a certain product type, with a certain metadata.