How to Get Current Category ID WooCommerce

How to Get Current Category ID WooCommerceIn this post I want to show you how get current category id WooCommerce using a simple get current category ID WooCommerce code snippet that you can use in your custom plugin or WooCommerce theme development.

I want to show you with a step by step example of how you can get the category ID in WooCommerce and repurpose it in your code to build custom functionality.

Why Get Current Category ID WooCommerce?

In most cases you may want to get current category ID in WooCommerce when you want to modify the functionality of how the product is added to cart or checkout process.

Example to Get Current Category ID WooCommerce

For example, you can modify the checkout page functionality by adding custom WooCommerce redirect after payment as I explained in detailed in the post WooCommerce Redirect after Checkout: How to Set It Up.

This get current category ID WooCommerce code snippet I will share in this quick guide can be used along with the WooCommerce redirect after checkout code to create a good experience for the user to be redirected to a unique sales page after completing the payment for a specific product category.

Get Current Category ID WooCommerce

Get Current Category ID WooCommerce

Suppose we wish to get the category ID of the category shown on the image above. We can simply get the category ID using a WordPress core method – get_queried_object() .

It helps to start by understanding how this function works so that you can implement the code in your project with full knowledge of how it works!

Get Current Category ID WooCommerce Using  – get_queried_object() 

This is a simple function that just returns the object that has been currently queried. So if you are in the category it will return the category object.

When you get the category object, you can go ahead and get the ID from the object.

So we can save the return value of this function in a variable as follows:

$current_category_object = get_queried_object();

To illustrate using our example of the category image I shared above, we can add this to a header hook to display the category object and then pluck the category ID.

Get Current Category WooCommerce Object Display on Header

To display this in the header of the customized Storefront theme that I use for my WooCommerce development tutorials, we need to add it to the header hook.

The following is the full code snippet with a print_r to display the WooCommerce category object in the header of the WooCommerce site.

As you can see on the image below the category object is displayed and we have the term_id which is our category ID in the first index.  We will later access it and display it or use it in the code any other way we wish:

Get Current Category ID WooCommerce

The following is the code snippet we have used to display the WooCommerce category object in the WordPress header.

[php]

// Get Current Category Object

add_action(‘wp_head’, ‘get_current_category_object’);

function get_current_category_object(){

$current_category_object = get_queried_object();

print(‘<pre>’);

print_r($current_category_object);

print(‘</pre>’)

}

[/php]

Get Current Category ID WooCommerce Step by Step (2 Steps)

To get the current category ID in WooCommerce, you need to understand that we can use the WordPress function that gets us the queried object.

The following are the steps to get the current category ID in WooCommerce.

  1. Save the category object in a variable – $current_category_object = get_queried_object();
  2. Access the term_id property of the object in the first step and you now have your category ID.

For illustration, the complete code is as follows  :

[php]

// Get Current Category ID WooCommerce

add_action(‘wp_head’, ‘get_current_category_id’);

function get_current_category_id(){

$current_category_object = get_queried_object();

echo $current_category_object ->term_id;

}

[/php]

When you add this code you can see the ID of the category you visit displayed at the top of the page as shown on the image below:

Get Current Category ID WooCommerce

Conclusion

In this post, I have shared the get current category id WooCommerce code snippet and also illustrated how it works.

The take from this tutorial should be the using of the get_queried_object()  WordPress function to get the WooCommerce category object and then access the term_id which is a property of the category object and is equal to the category ID.

Similar Articles

  1. How to Find WordPress Category ID in 3 Easy & Quick Options