Get WooCommerce product categories from WordPress

I am trying to get the product categories from WooCommerce through a function in my WordPress theme

    function get_me_list_of($atts, $content = null)
    {   
        $args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => $atts[0]);

        $loop = new WP_Query( $args );

        echo '<h1 class="upp">Style '.$atts[0].'</h1>';
        echo "<ul class='mylisting'>";
        while ( $loop->have_posts() ) : $loop->the_post(); 
        global $product; 

        echo '<li><a href="'.get_permalink().'">'.get_the_post_thumbnail($loop->post->ID, 'thumbnail').'</a></li>';
     echo '<li><a href="'.get_permalink().'">'.$loop->post->post_title.'</a></li>';

echo '<li><a href="">'.get_categories().'</a></li>';
        endwhile; 

        echo "</ul>";

        wp_reset_query(); 


    }

    ?>

The above code returns some products, but the product categories.

When I included echo '<li><a href="">'.get_categories().'</a></li>'; in the code above it returns as an array. How do I fix this?

How do i change this to get the product categories from WooCommerce?

Solutions

Improving Suman.hassan95's answer by adding a link to subcategory as well. Replace the following code:

$sub_cats = get_categories( $args2 );
    if($sub_cats) {
        foreach($sub_cats as $sub_category) {
            echo  $sub_category->name ;
        }

    }

with:

$sub_cats = get_categories( $args2 );
            if($sub_cats) {
                foreach($sub_cats as $sub_category) {
                    echo  '<br/><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
                }
            }

or if you also wish a counter for each subcategory, replace with this:

$sub_cats = get_categories( $args2 );
            if($sub_cats) {
                foreach($sub_cats as $sub_category) {
                    echo  '<br/><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
                    echo apply_filters( 'woocommerce_subcategory_count_html', ' <span class="cat-count">' . $sub_category->count . '</span>', $category );
                }
            }
<?php

  $taxonomy     = 'product_cat';
  $orderby      = 'name';  
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 1;      // 1 for yes, 0 for no  
  $title        = '';  
  $empty        = 0;

  $args = array(
         'taxonomy'     => $taxonomy,
         'orderby'      => $orderby,
         'show_count'   => $show_count,
         'pad_counts'   => $pad_counts,
         'hierarchical' => $hierarchical,
         'title_li'     => $title,
         'hide_empty'   => $empty
  );
 $all_categories = get_categories( $args );
 foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;       
        echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';

        $args2 = array(
                'taxonomy'     => $taxonomy,
                'child_of'     => 0,
                'parent'       => $category_id,
                'orderby'      => $orderby,
                'show_count'   => $show_count,
                'pad_counts'   => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li'     => $title,
                'hide_empty'   => $empty
        );
        $sub_cats = get_categories( $args2 );
        if($sub_cats) {
            foreach($sub_cats as $sub_category) {
                echo  $sub_category->name ;
            }   
        }
    }       
}
?>

This will list all the top level categories and subcategories under them hierarchically. do not use the inner query if you just want to display the top level categories. Style it as you like.

You could also use wp_list_categories();

wp_list_categories( array('taxonomy' => 'product_cat', 'title_li'  => '') );

Similar questions

How do I list categories and the common categories for posts beneath those categories?
I have four categories: These categories contain posts that are also categorized So basically there's a bunch of posts that are categorized (2013, Cat A), (2013, Cat B), (2012, Cat A), (2012, Cat C), etc Without having to add Cat A, Cat B and Cat C as four sets of child categories under 2013, 2012, 2011 and 2010 is there a way that I can programmat...
Show parent categories and sub categories but not grand children categories
I'm struggling with displaying sub categories limited to only direct children of the parent on category page... I hope someone can help me with that :) I have working code - when the current category is selected, it displays all its subcategories... but also including grand children and so on... How to display only direct children of current parent...
Display Categories, Sub-categories, and Sub-sub-categories on separate pages
I am building a FAQ section in our mobile site which I would like to have the following hierarchy: Category > Sub-category > Sub-sub-category > Article I would like the category list and each sub-category list to display on its own separate page, rather than use an accordion-style UI (which you find in most FAQ plug-ins). I'm told by my developer t...
How can I echo the categories, sub categories and then posts within the sub categories all on one page?
I amusing wordpress and have setup a custom post type (called products & custom taxonomy called product_category) and I want to do the follow I will have a page called catalog and want to display all the categories/sub categories and products under the sub categories on one page. However am unsure on the code to to it. Get all categories and ec...
List categories / child categories / grandchild categories / post titles
I found this code: This lists all categories and posts from a category. But i want it from all categories. But when i fill in 'child_of' => 2 it lists everything, but it does not get formatted well. grandchilds have the same hierarchical status as the children. what i want for example: so: all cats should be able to handle posts, and if there ar...
How can I add below of Woocommerce out of stock product text the Woocommerce product price on a single product
I have a single woocommerce product page where only availability "Out of Stock" message is shown. How can I show a formatted price with currency below that text? I think I need to insert the following but I dont know how This is happening to all simple products. As settings, stock management is disabled. I don't know why price is not displayed. I t...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.