Woocommerce PHP – Product Categories by Product ID

In this tutorial you will learn how to check if a product is assigned to a tag, a category or a custom taxonomy. You can check if a product is inside a “loop”, for example the shop page, or you can also check if a product of a category is in the cart or you can do the check inside an order or an order email.

 

The WordPress Function has_term()

The same function is always used for this type of check, the has_term() function. This function contains three parameters:

  • The name of the term (category, tab, custom term), the term ID, a slug or array of them.
  • The taxonomy name (default or custom).
  • If you do not want to use the “current post”, also specify the post ID or the post object.

Define a term you want to check, for example “rock”, specify a taxonomy name, for example “music” and finally select the post ID.

if ( has_term( 'rock', 'music', 3 ) ) {
// do something if post 3 belongs to Rock Music
}
In the other examples you can see where it can be useful to use these checks and the has_term() function. In Woocommerce there are already custom taxonomies: “product_cat” for product categories and “product_tag” for product tags, these are needed in the examples.

 

Checks on the Single Product Page

Check for category

This is the simplest case. If you want to customize the Single Product Page, there is a current product and then you don’t need the third parameter of the has_term() function.

In this example, a banner should be displayed if the product belongs to the “computers” category. If the product is not assigned to this category, nothing happens.

add_action( 'woocommerce_before_single_product', 'print_banner_if_product_belongs_to_category_computers' );

function print_banner_if_product_belongs_to_category_computers() {
if ( has_term( 'computers', 'product_cat' ) ) {
echo '<img src="banner.png">';
}
}

Check for tag

If you want to check for a tag, you only need to change the second parameter of the has_term() function:

add_action( 'woocommerce_before_single_product', 'print_banner_if_product_belongs_to_tag_black' );

function print_banner_if_product_belongs_to_tag_black() {
if ( has_term( 'black', 'product_tag' ) ) {
echo '<img src="banner2.png">';
}
}

Checks on the Shop Page

Check for category

We are on the Shop Page, this is called the “loop”. On the Shop Page there are multiple products, so the current product cannot be used, instead the ID of the current page is used. This is done by “calculating” the ID based on the position within the “loop”.

In the example, a message is written under each product that is assigned to the category “tv”. The first thing to do is to find out the “loop product ID”, as this must be done for each product in the loop, and then the has_term() condition can be used.

add_action( 'woocommerce_after_shop_loop_item', 'print_message_if_product_belongs_to_category_tv' );

function print_message_if_product_belongs_to_category_tv() {
global $product;
$product_id = $product->get_id();
if ( has_term( 'tv', 'product_cat', $product_id ) ) {
echo 'A message about tvs';
}
}

Check for tag

To check for a tag you, again, need to only change the second parameter of the has_term() function:

add_action( 'woocommerce_after_shop_loop_item', 'print_message_if_product_belongs_to_tag_dark' );

function print_message_if_product_belongs_to_tag_dark() {
global $product;
$product_id = $product->get_id();
if ( has_term( 'dark', 'product_tag', $product_id ) ) {
echo 'A message about dark products';
}
}

Check if a Product ID belongs to a Product Category

If you are not on the Single Product Page, there is no current product to work with and therefore no global $product object. Fortunately, the has_term() function also accepts the post ID, so you can do the check outside the Single Product Page.

In this example, a text should be displayed in the footer if product ID 10 belongs to the category “lamps”:

add_action( 'wp_footer', 'print_message_if_product_ID_belongs_to_category_lamps' );

function print_message_if_product_ID_belongs_to_category_lamps() {
if ( has_term( 'lamps', 'product_cat', 10 ) ) {
echo '<p>Product ID 10 belongs to Lamps</p>'; 
}
}

Conclusion

As you have seen in this post, it is easy to check products by category, tag or custom taxonomy. Hopefully the Woocommerce PHP – Product Categories by Product ID post has helped you. You can read more Arrow Design posts here:

 

Read another Woocommerce post : ‘Woocommerce Product Attribute Hooks

 

Arrow Design, based in Dublin, Ireland, provides quality website design services in Dublin and beyond at affordable prices. If you would like help with implementing the above code, or any wordpress website development project, contact us. We love website design and it shows! We provide custom wordpress plugin development, website design training and lots more.

We do it all, so you don’t have to!

Related Posts

Woocommerce Account Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Account Pages, like the Login/Register page, the Downloads page or the Orders page.

Woocommerce PHP – Product Categories by Product ID

Woocommerce Single Product Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Single Product Page. This should help you to quickly and easily find the hook positions on the page.

Woocommerce Product Attribute Hooks

Woocommerce Product Attribute Hooks

In our Product Attribute Hooks post you will learn how to get the product sku, description, short description, regular price, shipping class and so on.

…We do more, so you can do less 🙂

 

Leave a message and we will be straight back to you!

Pin It on Pinterest

Share This