How to Get Current WooCommerce Product Category

WooCommerce Get Current Product Category

In this post, I share with you WooCommerce get current product category code snippet and explain how you can use this code to get the current product category.

If you are a WooCommerce developer or just customizing WooCommerce as a regular user, the ability to get current product categories from the WooCommerce database can help you create a variety of display options for your products.

You can also get the current product category when you want to create specific WooCommerce customer experience features like the WooCommerce redirect after checkout based on the current category.

I demonstrated this in the previous tutorial on how to get WooCommerce current product category name.

WooCommerce Get Current Product Category

First, I presume you know your way around WooCommerce development and you are just looking for a quick code snippet to get the current product category.

If you are a complete beginner, you may want to check out most of the more than 1000 posts I have shared before on WooCommerce development here – WooCommerce tutorials.

Now let us focus on the basics of getting the current WooCommerce product category.

First, you need to understand that WooCommerce products are just custom post types – from a WordPress development perspective.

So you can query them and the product categories in the same manner we build queries for ordinary WordPress posts.

WooCommerce products are Custom Post Types with Categories as one of their Taxonomies.

Since we now understand how the WooCommerce categories work under the hood, we will use the get_the_terms() WordPress core function to get the current WooCommerce product category from the database.

But first, let us have a quick overview of how the get_the_terms() WordPress function works!

 

WordPress Function to Get Current Category – get_the_terms ()

This function is designed to simply get us the terms of a taxonomy that we want and to get what we want we need to give this function a parameter.

The way to get what we want from this function is to give it the ID of the post from which we want the categories.

So in a quick summary, this function takes in two parameters the ID or the object and the taxonomy name.

Before we get lost, let me share a quick summary table followed by a simple example to help you get a glimpse of the way this function works!

Parameter Description
$post This is required for this function to work and it simply the post ID or the object for which we want to get the data –  for this case, it would be the ID of the current post since we want to get the WooCommerce CURRENT PRODUCT category
$taxonomy This is also required for this function to work and it’s the taxonomy name

 

WooCommerce Get Current Product Category

WooCommerce Get Current Product CategorySuppose we now want to get the current product category as you can see in the image above; we have that for the example.

So we can get the current product ID using the WordPress global post object  $post. This will return the ID along with the other post data

$post (WP_Post) The post object for the current post. The object described in Class Reference/Wp_posts.

Once we have the ID we can now pass the ID to the function get_the_terms() along with the taxonomy and we will successfully get the current product category.

The taxonomy for the WooCommerce product category is – product_cat so we can now pass the current product ID along with the product_cat  to get all the categories associated with the current product. So we can do this with this code:

global $post;

$terms = get_the_terms( $post->ID, 'product_cat' );

$nterms = get_the_terms( $post->ID, 'product_tag'  );

foreach ($terms  as $term  ) {

$product_cat_id = $term->term_id;

$product_cat_name = $term->name;

break;

}

 

WooCommerce Get Current Product Category Step by Step (3 Main Steps)

So these are the steps we have taken to get the current product category in WooCommerce :

  1. First, get the current product ID
  2. Pass the product ID along with the product_cat taxonomy to the get_the_terms() function as the two parameters.
  3. Step 2 returns an array with all the details. We use a for each loop to loop through the data and get the category name or any other detail we want of the current product category.

You can now go ahead and print or use the category in any other logic as I pointed out in the example WooCommerce redirect after checkout based on category.

I have added this code to a hook to the head of my example site and you can see from the image below that the category is successfully displayed:

WooCommerce Get Current Product Category

The full code snippet that I added to the customized Storefront theme to display the category on the WordPress header as shown on the image above is as follows:

add_action('wp_head', 'get_current_product_category');

function get_current_product_category(){
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
$nterms = get_the_terms( $post->ID, 'product_tag'  );
foreach ($terms  as $term  ) {
$product_cat_id = $term->term_id;
$product_cat_name = $term->name;
break;
}

echo $product_cat_name;

}

Conclusion

In this post, we have highlighted the step-by-step way to get the current product WooCommerce category.

As you have seen in the example above, you simply need to know the current product ID and you can use the default WordPress function to get the terms and loop through to object the current WooCommerce category.

Similar Articles