Woocommerce sub category loop

I am working on a listing of subcategories for Woocommerce and I am stuck,

The code for the product loop

Read More
    <?php
$args = array ( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 12, 'product_cat' => '2', 'orderby' => 'date','order' => 'DESC' );

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

                        <li>    

                             <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><h3 ><?php the_title(); ?></h3></a>

                        </li>
            <?php endwhile; ?>
            <?php wp_reset_query(); ?>

But I can not find a decent way to simply get a listing of the sub-product-categories instead of the products.

Thanks in advance

Related posts

1 comment

  1. By ID:

    function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) {
    $args = array(
       'hierarchical' => 1,
       'show_option_none' => '',
       'hide_empty' => 0,
       'parent' => $parent_cat_ID,
       'taxonomy' => 'product_cat'
    );
    $subcats = get_categories($args);
    echo '<ul class="wooc_sclist">';
      foreach ($subcats as $sc) {
        $link = get_term_link( $sc->slug, $sc->taxonomy );
          echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
      }
      echo '</ul>';
     }
    

    By Name:

    function woocommerce_subcats_from_parentcat_by_NAME($parent_cat_NAME) {
    $IDbyNAME = get_term_by('name', $parent_cat_NAME, 'product_cat');
    $product_cat_ID = $IDbyNAME->term_id;
    $args = array(
       'hierarchical' => 1,
       'show_option_none' => '',
       'hide_empty' => 0,
       'parent' => $product_cat_ID,
       'taxonomy' => 'product_cat'
    );
    $subcats = get_categories($args);
    echo '<ul class="wooc_sclist">';
      foreach ($subcats as $sc) {
        $link = get_term_link( $sc->slug, $sc->taxonomy );
          echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
      }
    echo '</ul>';
    }
    

    Source / Inspiration

Comments are closed.