0

How to get current category id in Woocommerce?

How to get current category id in a template or theme functions file while using Woocommerce. In fact not only using Woocommerce but this method can be used to get the category object and subsequently the ID in normal category page.
[code language=”php”]
global $wp_query;
// get the query object
$cat_obj = $wp_query->get_queried_object();

print_r($cat_obj);

if($cat_obj) {
$category_name = $cat_obj->name;
$category_desc = $cat_obj->description;
$category_ID = $cat_obj->term_id;
}[/code]
Output:
[code language=”php”]
stdClass Object
(
[term_id] => 19
[name] => Dairy Rations
[slug] => dairy
[term_group] => 0
[term_taxonomy_id] => 19
[taxonomy] => product_cat
[description] => Being a small boutique style mill we are able to custom manufacture a ration to your specific needs and requirements.
[parent] => 0
[count] => 3
)[/code]

Another Code

[code language=”css”]
global $wp_query;
$category_name = $wp_query->query_vars[‘product_cat’];

if( $category_name ) {
$category_object = get_term_by(‘name’, $category_name, ‘product_cat’);
$category_id = $category_object->term_id;
}

[/code]

Sandeep Kumar

Sandeep kumar is a web developer and a professinal blogger. He works for latest technologies and write articles what he learn. In ICodeHub he share web languages tutorials and other programming tips and tricks. if you need some help or have some suggestion then mail him at sandeep92pal@gmail.com without any hesitation. He believes in learning and sharing.