Search code examples
wordpresswoocommerce

How to get the category name from an id product in woocommerce


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?


Solution

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