How to get category name from product id in woocommerce

I am building an e-commerce site using woo commerce. I'm struggling with code, how to display product category name using category id in woocommerce?

In wordpress it is easy to show the category name by id like get_the_category_by_id(3). So that it displays the name id 3.

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

0
source share
2 answers

Try this feature to get the name of a product category.

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 helpful.

+9
source

you can use something like this

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

Please let me know if this helps.

print_r($product_cats);
+7

All Articles