WooCommerce provides built-in options to restrict coupons to specific products or categories. However, there is no native “global exclude” setting to ensure a particular product can never be discounted by any coupon, regardless of the coupon’s individual settings.
This is particularly useful for products sold at cost or with very slim margins, where any further discount would result in a loss. Fortunately, WooCommerce provides the woocommerce_coupon_is_valid_for_product filter, which allows us to programmatically exclude specific products from coupon eligibility across your entire store.
Implementation PHP Code
The implementation is straightforward. Within the callback for the woocommerce_coupon_is_valid_for_product filter, we simply check the ID of the product being evaluated. If it matches the ID of the product we want to exclude, we return false to invalidate the coupon for that specific item.
add_filter('woocommerce_coupon_is_valid_for_product', function ($valid, $product, $coupon, $values)
{
// Replace '12345' with the ID of the product you want to exclude from all discounts.
if (12345 == $product->get_id()) {
$valid = false;
}
return $valid;
}, 9999, 4);
Beyond excluding individual product IDs, this method can be extended to restrict coupons based on product categories, product types, or even specific custom metadata. This gives you granular control over your store’s discounting strategy, ensuring that your most valuable or low-margin items remain at their intended price points.
