0

Working on a category page - taxonomy-product_cat.php - I want to list all the subcategories of that category page, with all its products. I would think it is simple enough but I cant get it to work.

I can list all categories of the shop with all subcategories and all products - So for example bags would list all products of the whole shop.

Or I can list the subcategories of just the category page - but without the products listed.

How can I list just the subcategories of the category page with their products?

The code I am using that works great, but loops all the categories in the shop, and not just the active category is:

$args = array('taxonomy' => 'product_cat' ); $all_categories = get_categories( $args );

foreach ($all_categories as $cat) {
    $category_id = $cat->term_id;
    $args2 = array('taxonomy' => $taxonomy,'parent' => $category_id,'hierarchical' => $hierarchical, 'orderby' => $orderby, 'order' => $order,'hide_empty' => $empty); $categories = get_categories( $args2 );


$sub_cats = get_categories( $args2 );
            if($sub_cats) {
                foreach($sub_cats as $sub_category) {
                    echo "<h2>".$sub_category->cat_name."</h2>";

                    $args = array( 'post_type' => 'product','product_cat' => $sub_category->slug, 'orderby' => $orderby, 'order' => $order);
                            $loop = new WP_Query( $args );   
                            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>


                            <!--HTML HERE-->


                            <?php endwhile;  wp_reset_query();  }}} ?>
1

The code below works for me in archive-product.php not sure if it may also work in taxonomy-product_cat.php

EDIT I've added some lines of code to exclude posts previously outputted, since a post can belong to several categories and that's why you can find always the same posts

    if(is_a(get_queried_object(),'WP_term')){
    $subs=get_terms('product_cat',array('parent'=>get_queried_object()->term_id));
    //var_dump($subs);
    $to_excude=[];
    foreach($subs as $sub){
        $args = array(
            'post_type' => 'product',
            'post_status'=>'publish',
            'post__not_in' =>$to_excude,
            'tax_query' => array(
                    array(
                            'taxonomy' => 'product_cat',
                            'field'    => 'term_id',
                            'terms'    => $sub->slug
                    ),
            ),
    );
    $query = new WP_Query( $args );
    if($query->have_posts()):
        ?>
        <h4><?php echo $sub->name?></h4>
        <?php
        while($query->have_posts()):
            //
            $to_excude[]=get_the_ID();
            $query->the_post();
            the_title();
            // etc
        endwhile;
    endif;
    wp_reset_query();   
    }

}
| improve this answer | |
  • out of the box taxonomy-product_cat.php just calls archive-product.php – Jon Jan 20 at 19:43
  • I dumped it into my page, and it looks good, but the products under the sub categories are not the correct products. And All subcategories are showing the same products. But I will see what I can do with the code - hopefully it is a good start. Thanks – Jon Jan 20 at 19:49
  • Sorry I cant get this to work. The best I can do is display different products not the sub category products. Maybe the products is calling the wrong parent? How would I change that? – Jon Jan 21 at 13:14
0

I cobbled this code together from different ideas online, and seems to work:

<?php foreach ( $product_categories as $product_category ) {
    echo '<h2><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></h2>';      
    $args = array(
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $product_category->slug
            )
        ),
        'post_type' => 'product',            
        'orderby' => 'name',
        'order'   => 'ASC', 
        'posts_per_page' => -1 

    );      

    $products = new WP_Query( $args );      
    while ( $products->have_posts() ) {
        $products->the_post();    ?>

        //HTML CODE HERE
 <div>  <?php the_content(); ?> </div>
 <?php  }  } ?>

However I am having some issues ordering the outputted products - if anyone has any ideas. Changing the 2nd args 'order' => 'DESC' or adding 'meta_key' => 'size', 'orderby' => 'meta_value_num' orderby` etc does not seem to affect the product order listing under the sun category titles.

| improve this answer | |
  • You are currently sorting by post slug, are they not sorted like that? What do you mean by "I am having some issues ordering the outputted products"? – nmr Jan 28 at 18:10
  • @nmr When I change for example to 'order' => 'DESC', nothing changes in the output. Or if I change orderby to 'meta_key' => 'size', 'orderby' => 'meta_value_num' which works on other loops nothing changes. Output being the products listed under the subcategory heading. How could I sort by SKU, or a meta key, or even order desc? – Jon Jan 28 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.