How to Get Current Product Category Name in WooCommerce

How to Get Current Product Category Name in WooCommerceIf you want to get current product category name from the product in WooCommerce, you can use the $wp_query object like you would for any other WordPress post. In this tutorial, I will demonstrate how to get current product category from the product.

WooCommerce Get Product Category

Suppose we have this product shown in the image below and we want to know the category name of this product.

How to Get Current Product Category Name in WooCommerce

As you can see from the image above, we already know the category is ‘Cool products’.

We need to get this current product category in the code as we would get in the ordinary WordPress post using this code:

$wp_query->get_queried_object()->term_id;

WP Function Get the Terms

You can also use get_the_terms() which is a  WordPress native function that retrieves the terms of a taxonomy that is associated with a specific post.

The general expression of this function is as follows:

get_the_terms( int|WP_Post $post, string $taxonomy )

The following are the parameter of this function and their respective descriptions:

Parameter Description
$post This is the post ID or the object that we will use to access the specific product post so that we get the respective category for the product. This can be an integer the POST ID.
$taxonomy This is the taxonomy we want to retrieve using this function. This can either be a category or tag in a WooCommerce product. These are the examples for category and tags;

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

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

Get Current Product Category Name in WooCommerce

The first step is declaring the global post object then use the function above to get current product category.

To illustrate I will add the action hook to the head and add the code to the callback function to illustrate by displaying the category in the head. Add this code to the functions.php

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;

}

When you add this code you should see the display of the product category as shown on the image below:

How to Get Current Product Category Name in WooCommerce

You can verify with the other products on your site to see if the current product category is displayed as shown again in another product like below:

WooCommerce Get Product Category

Conclusion

In this post, we have highlighted how you can use the get_the_terms() WordPress function to get current product category in WooCommerce. This is the simplest way you can use to get the product category and display it in your WordPress theme or plugin functions.

Similar Articles

Comments are closed.