How to get Categories from product id in single page

I am trying to retrieve Related Categories from product id.
Suppose I have categories like bellow.

iPhone (Parent Category)
  -iPhone 3 (sub)
  -iPhone 4 (sub)
  -iPhone 5 (sub)
  -iPhone 6 (sub)

if a Product : iPhone 4 Charger. (in iPhone 4 sub category)

Read More

How can I retrieve all above categories by the product id (iPhone 4 Charger)
as a list?

I think Step may be == (get product id) –> (get category) –> (get parent category) –> (get category list of this parent category)

I am trying below code

<?php 
$parent = get_category_parents( $cat, true, ' &raquo; ' ); 
echo $product->get_categories( ', ', '<span>' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); 
?>

Related posts

1 comment

  1. if you are using woocommerce please try this:

    <?php 
    $term_list = wp_get_post_terms($id_product,'product_cat',array('fields'=>'ids'));
    $cat_id = (int)$term_list[0];
    echo get_term_link ($cat_id, 'product_cat');
    
    ?>
    

Comments are closed.