We’ve studied “conditional logic” extensively over the previous Business Bloomer posts. In today’s spin-off, I want to clarify how you can check if a given product belongs to a category, tag or custom taxonomy because this is one of the most used conditional logic scenarios.
It’s important to know that a product can be inside the “loop” (e.g. the shop page or a list of products), alternatively you may be on the single product page or have the product unique ID, or even you can check if a product category is inside the Cart. Finally, you can even run the product category check within an Order or an Order Email.
Either way, the function is always the same. You simply need to understand how to use it. Enjoy!
Meet the has_term() WordPress function
WordPress comes to the rescue with the has_term() function, which accepts 3 parameters:
- The term (category, tab, custom term) name, term ID, slug or array of them
- The taxonomy name (default or custom)
- The post ID or post object, in case you don’t want to use the “current post”
It basically goes like this: you define a term you want to check against the post object e.g. “Rock”, you specify the default or custom taxonomy name (in the Jazz example it could be you’re using a “Music” custom taxonomy) and finally you may specify the post ID:
if ( has_term( 'rock', 'music', 26 ) ) {
// do something if post 26 belongs to Rock Music
}
Easy peasy. Now it’s time to apply this to WooCommerce and see where it could come in handy. WooCommerce comes already with custom taxonomies: “product_cat” for product categories and “product_tag” for product tags. That’s all we need to use.
WooCommerce: check if current product belongs to product category @ Single Product Page
This is the easiest case scenario. You’re customizing the single product page and want to show a specific banner in case the current product belongs to a certain category.
The good thing is that we are on the single product page and therefore we know the “current post” exists and we don’t need the third has_term() parameter.
I’ve picked the woocommerce_before_single_product hook to display the banner above the single product template, but feel free to chose any other single product page hooks. Code goes like this:
add_action( 'woocommerce_before_single_product', 'tutoraspire_print_banner_if_product_belongs_to_category_tables' );
function tutoraspire_print_banner_if_product_belongs_to_category_tables() {
if ( has_term( 'tables', 'product_cat' ) ) {
echo '';
}
}
Only if the single product we’re looking at belongs to the “tables” category, the image will display. Otherwise, nothing will happen.
WooCommerce: check if current product belongs to product tag @ Single Product Page
Similarly, you can check the current product against a given product_tag:
add_action( 'woocommerce_before_single_product', 'tutoraspire_print_banner_if_product_belongs_to_tag_red' );
function tutoraspire_print_banner_if_product_belongs_to_tag_red() {
if ( has_term( 'red', 'product_tag' ) ) {
echo '';
}
}
WooCommerce: check if current product belongs to product category @ Shop Page
Let’s complicate things.
We’re now in the Shop page, or in a product list section generated by a shortcode. That’s called the “loop”. In this case, you have multiple products in the same page, so we can’t really use the same method as above as the “current post” won’t be a product ID – instead it will be the current page ID.
We therefore need to “calculate” the ID based on the position we’re inside the “loop”. That’s easier coded than said actually.
So, let’s try to print a message under EVERY product in the Shop page that belong to a given category e.g. “Chairs”. I’ve used the woocommerce_after_shop_loop_item but feel free to pick any other shop hooks from my visual hook guide.
As you can see inside the snippet, I first need to find out the “loop product ID” because this needs to run for each product in the loop, and after that I can go back using the has_term() conditional:
add_action( 'woocommerce_after_shop_loop_item', 'tutoraspire_print_message_if_product_belongs_to_category_chairs' );
function tutoraspire_print_message_if_product_belongs_to_category_chairs() {
global $product;
$product_id = $product->get_id();
if ( has_term( 'chairs', 'product_cat', $product_id ) ) {
echo 'A message about chairs';
}
}
WooCommerce: check if current product belongs to product tag @ Shop Page
No comment.
add_action( 'woocommerce_after_shop_loop_item', 'tutoraspire_print_message_if_product_belongs_to_tag_yellow' );
function tutoraspire_print_message_if_product_belongs_to_tag_yellow() {
global $product;
$product_id = $product->get_id();
if ( has_term( 'yellow', 'product_tag', $product_id ) ) {
echo 'A message about yellow products';
}
}
WooCommerce: check if a product ID belongs to product category
Let’s say you are outside the loop and not on the single product page. That means you have no “current product” to deal with, no global $product you can call, and you’re stuck.
Thankfully, the has_term() function accepts the post ID as third parameter so you can run your conditional check even on the Homepage, inside a shortcode, inside a WooCommerce order email, and so on.
You simply need to specify the ID you desire.
In this case study I wish to display something in the footer if product ID 59 belongs to a certain category. It could be for a site-wide advertising banner for example
add_action( 'wp_footer', 'tutoraspire_print_message_if_product_ID_belongs_to_category_lamps' );
function tutoraspire_print_message_if_product_ID_belongs_to_category_lamps() {
if ( has_term( 'lamps', 'product_cat', 59 ) ) {
echo 'Product ID 59 belongs to Lamps
';
}
}