Skip to content
Advertisement

Display content on WooCommerce single products based on product categories when they have child categories

I am trying to display the test div below on any product page that is in the category cat1title or cat2title. Using the hook below, which I thought was correct, I should be seeing this div on the page but I am not seeing it at all. The page displays just fine but no div. No errors in the console either. According to docs, this should still be valid with the latest version of Woo. What am I missing?

JavaScript

Advertisement

Answer

The defined product categories need to be set on the products to work with has_term() conditional function…

So this will not work for parent product categories which child subcategories are set for a product…

To check for parent product categories too you could use this custom conditional function:

JavaScript

So in your code:

JavaScript

Code goes in functions.php file of the active child theme (or active theme).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement