4

I am creating a e-commerce website using woo commerce. I am struggling with a code that how to display the category name of the product using category id in woocommerce?.

In wordpress its easy to display the category name by id as get_the_category_by_id(3). So that it display the name of the id 3.

By the same way how to display the category name by id in woocommerce?

user2644743
  • 215
  • 3
  • 9
  • 18

3 Answers3

17

Try this function to get product category name

function get_product_category_by_id( $category_id ) {
    $term = get_term_by( 'id', $category_id, 'product_cat', 'ARRAY_A' );
    return $term['name'];
}
$product_category = get_product_category_by_id( $your_category_id );

Hope this will be useful.

Ratnakar - StoreApps
  • 4,261
  • 23
  • 17
  • wow super. working greatly. Thank you very much Ratnakar.. Thank you so much. I was struggling this for more than 2 days. Thank you once again.. – user2644743 Apr 04 '14 at 04:33
8

you can use something like this

$product_cats = wp_get_post_terms( your_id, 'product_cat' );

Please, let me know whether it helps.

print_r($product_cats);
Yogesh Pawar
  • 336
  • 2
  • 17
1

You can use wp_get_post_terms to get category id of any product like this :

$term_list = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));

I am just fetching id's of categories assigned to the particular product, You can fetch name also.

Hope this will be useful.

Amritosh pandey
  • 404
  • 4
  • 11