Want to make sure a coupon only applies to the products you choose? With a simple WooCommerce snippet, you can restrict coupon usage to selected product categories and give customers a clearer, more relevant discount.
This guide walks you through WooCommerce coupon restriction by product category, so you can decide which categories qualify for a coupon without relying on extra plugins. Add the snippet to your store, adjust it to match your category choices, and keep your promotions focused.
WooCommerce Coupon Restriction by Product Category: How It Works
Imagine launching a discount for one product line without cutting prices across your entire store. A category rule gives you that control, but it helps to understand what the rule checks before you add a snippet or choose the categories.
In a typical setup, the code checks the products in the customer’s cart against the categories you specify. The exact result depends on how the snippet is written: it may allow the coupon when at least one eligible product is in the cart, or require a stricter match. That distinction affects what shoppers can discount.
The same approach can support different promotion goals, from clearing seasonal stock to rewarding interest in a particular collection. Here are a few situations where category rules can make a coupon more focused.
When category-based coupon rules are useful
Category restrictions are handy when a store-wide discount would be too broad or expensive. Instead of creating a separate coupon for every product, you can target a group of related items and keep the promotion aligned with your margin or campaign.
- Seasonal promotions: Offer a discount on summer accessories while leaving new arrivals at regular prices.
- Stock clearance: Encourage sales of items in an end-of-season or overstock category.
- Category-specific campaigns: Share a coupon with customers interested in a particular collection, such as home décor or skincare.
One detail worth checking is whether the coupon’s discount applies only to qualifying products or whether an eligible item simply makes the coupon valid for the order. Those are different outcomes: a cart might contain both sale-category products and full-price items. Make sure the snippet’s logic matches the promotion you intend to run, and test a mixed-category cart before publishing the coupon.
Also remember that products can belong to more than one category, so a qualifying item may match your rule through any assigned category. WooCommerce’s coupon management documentation explains the built-in coupon settings; a snippet can add category-specific behavior beyond those options.
Add a PHP Snippet to Restrict Coupons to Selected Categories
Once you’ve decided which items should qualify, the next step is turning that rule into a check WooCommerce can apply to each product in the cart. The snippet below targets one coupon and leaves your other coupons’ existing rules alone.
Customize the WooCommerce coupon restriction by product category code
Use each category’s slug—the short, URL-friendly version of its name—and enter the coupon code you want to restrict. This filter checks category eligibility for individual products, so non-matching items won’t receive the coupon discount.
addfilter( 'woocommercecouponisvalidforproduct', 'limitcoupontoselectedcategories', 10, 4 );
function limitcoupontoselectedcategories( $valid, $product, $coupon, $values ) {
$targetcoupon = 'spring15';
$allowedcategories = array( 'clothing', 'accessories' );
// Leave other coupons unchanged.
if ( wcstrtolower( $coupon->getcode() ) !== $targetcoupon ) {
return $valid;
}
// Check the parent product when the cart item is a variation.
$productid = $product->istype( 'variation' )
? $product->getparentid()
: $product->getid();
if ( ! hasterm( $allowedcategories, 'productcat', $productid ) ) {
return false;
}
// Preserve any other coupon or product restrictions.
return $valid;
}Replace spring15 with your coupon code and update the category slugs in $allowed_categories. You can find a slug in the product category list in your WordPress dashboard. If a product belongs to more than one category, it qualifies when any assigned category matches one of the slugs in the array.
Add the code using a snippets plugin or your child theme’s functions.php file. When using a snippets plugin, follow its instructions about whether to include the opening PHP tag; the example above does not include one. The WooCommerce coupon restriction by product category in this example applies to a single named coupon, while other coupon codes retain their existing behavior.
Before sharing the coupon, test a cart with both eligible and ineligible products. Check that the discount appears only on qualifying items and that the coupon is rejected when the cart contains no eligible products. Keeping return $valid; in the snippet also preserves any built-in coupon restrictions you’ve already configured.
Test and Maintain Your Coupon Rule
A rule that looks right in the code still needs to behave well in a shopper’s cart. A few deliberate checks now can catch edge cases before customers encounter them at checkout.
Start with a fresh cart and apply the coupon in different product combinations. The following checks help confirm both which products receive the discount and when the coupon should be rejected.
Verify the restriction across eligible and ineligible cart scenarios
Test the coupon with more than one cart setup, including products assigned to multiple categories and variations of a qualifying product. If your store has sale prices, other coupons, or minimum-spend rules, include those conditions too; they can affect the final result.
- Eligible product only: Confirm the coupon applies to a product in an allowed category.
- Ineligible product only: Check that the coupon is rejected when no cart item matches.
- Mixed cart: Add eligible and ineligible items, then verify the discount appears only on qualifying products.
- Variation: Test a variation of a qualifying product, especially if your category assignments are made on the parent product.
- Other restrictions: Confirm existing coupon limits, such as expiry dates or minimum spend, still work as expected.
Run these checks from the cart through checkout, and try again after removing an eligible item. WooCommerce may refresh totals as cart contents change, so confirm the discount updates rather than relying on a single initial result. For a useful reference while checking built-in settings, consult WooCommerce’s coupon management documentation.
If a result differs from what you expect, check the coupon code and category slugs first, then review whether the product or its parent variation has the intended category assignment. Also test with other custom snippets temporarily disabled if you suspect two rules are affecting the same coupon.
Once the checks pass, keep a brief record of the coupon code, allowed categories, and test results. When categories or promotional terms change, repeat the mixed-cart test before relaunching the offer. This small maintenance habit helps keep the WooCommerce coupon restriction by product category aligned with your campaign and avoids surprises at checkout.
Keep WooCommerce Coupons Focused on the Right Products
Restricting a coupon to selected product categories helps you run more targeted promotions while protecting discounts on items that aren’t part of the offer. With a small PHP snippet, you can control which products qualify and preserve the behavior of your other coupons.
Before launching, double-check the coupon code and category slugs, then test eligible products, variations, and mixed carts to make sure the discount works as intended. Revisit the rule whenever your categories or promotion details change.
With a clear rule and a quick checkout test, you can offer shoppers relevant savings with greater confidence.