How to Check If a WooCommerce Product Belongs to a Category or Tag

How to Check If a WooCommerce Product Belongs to a Category or Tag
On this page
  1. Why This Check Comes Up So Often
  2. Step 1: Meet has_term()
  3. Step 2: Check on the Single Product Page
  4. Troubleshooting
  5. Frequently Asked Questions
  6. Conclusion

You can check if a WooCommerce product belongs to a category or tag using WordPress’s built-in has_term() function, passing the term, the taxonomy (product_cat or product_tag), and optionally a specific product ID.

Why This Check Comes Up So Often

  • Conditional banners, badges, or pricing rules frequently depend on a product’s category or tag
  • The same check works whether you’re in the shop loop, on a single product page, or working with a cart item
  • It’s a foundational building block for a huge range of custom WooCommerce logic

Step 1: Meet has_term()

has_term() accepts a term (name, ID, slug, or array), a taxonomy, and an optional post ID — omit the post ID when checking the current post in the loop or on a single product page.

Step 2: Check on the Single Product Page

PHP
/** Show a banner above the single product page if it belongs to the "tables" category */
add_action( 'woocommerce_before_single_product', 'show_banner_for_tables_category' );

function show_banner_for_tables_category() {
    if ( has_term( 'tables', 'product_cat' ) ) {
        echo '<img src="/wp-content/uploads/tables-banner.png" alt="Tables Collection">';
    }
}
Check if a WooCommerce product belongs to a category or tag: the page before adding the code
Before
Check if a WooCommerce product belongs to a category or tag: the page after adding the code
After

Checking a Specific Product by ID

PHP
if ( has_term( 'clearance', 'product_tag', 4820 ) ) {
    // Product ID 4820 is tagged "clearance"
}

Checking Inside the Cart

PHP
foreach ( WC()->cart->get_cart() as $cart_item ) {
    if ( has_term( 'fragile', 'product_tag', $cart_item['product_id'] ) ) {
        // At least one fragile-tagged item is in the cart
    }
}

Troubleshooting

The check always returns false even though the product clearly has the term

Confirm the taxonomy name is correct — product_cat for categories and product_tag for tags, not the human-readable label shown in wp-admin.

It works on the single product page but not inside a loop

Without a post ID, has_term() relies on the global post context — inside a custom loop, pass the specific product ID explicitly rather than relying on the current post.

Frequently Asked Questions

Can I check multiple categories at once?

Yes, pass an array of terms as the first argument — has_term() returns true if the product matches any one of them.

Does this work with custom product taxonomies too?

Yes, any registered taxonomy works, not just the two built-in WooCommerce ones — pass its exact registered taxonomy slug.

Is has_term() faster than querying the database directly?

Yes, it uses WordPress’s cached term relationships rather than a fresh database query, making it efficient to call repeatedly.

This same has_term() pattern is worth keeping in your personal snippet library, since checking whether a product belongs to a category or tag comes up constantly across banners, pricing rules, and shipping logic throughout a typical WooCommerce build.

Conclusion

Once you know how to check if a WooCommerce product belongs to a category or tag with has_term(), most conditional-display logic in WooCommerce becomes a one-line check you can reuse anywhere.

See also How to Get Variation IDs by Product Category or Tag in WooCommerce. Then read How to Show the Product Category on the WooCommerce Shop Page.

Prefer no code? All-in-One Utilities Woo Add-on adds over 50 WooCommerce features, so you can switch them on without code.

Leave a Reply

Schedule a call