2
votes

On the main shop page (archive-product.php) on my Woocommerce shop, I want to be able to display all the products but separate them by categories. So I would need to be able to create a loop for each product category.

For a visual reference, this is what I'm trying to achieve: for reference

Each gray block represents a new category and will loop through the products in that category.

Is there a way to achieve this?

2
Do you also need pagination or not?Amin
No pagination needed as the shop page will display all products.key_bearer

2 Answers

2
votes

Well as you mentioned in the comment, if you don't need any pagination, to list all products leading by their category you can first loop through the categories using get_terms() function and get whatever information you need on each iteration ( e.g: category name ), and then create one custom query per category and show the query's products, something like this will get you what you're trying to do:

<?php
foreach( get_terms( array( 'taxonomy' => 'product_cat' ) ) as $category ) :
    $products_loop = new WP_Query( array(
        'post_type' => 'product',

        'showposts' => -1,

        'tax_query' => array_merge( array(
            'relation'  => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'terms'    => array( $category->term_id ),
                'field'   => 'term_id'
            )
        ), WC()->query->get_tax_query() ),

        'meta_query' => array_merge( array(

            // You can optionally add extra meta queries here

        ), WC()->query->get_meta_query() )
    ) );

?>
    <h2 class="category-title"><?php echo $category->name; ?></h2>

    <?php
    while ( $products_loop->have_posts() ) {
        $products_loop->the_post();
        /**
         * woocommerce_shop_loop hook.
         *
         * @hooked WC_Structured_Data::generate_product_data() - 10
         */
        do_action( 'woocommerce_shop_loop' );
        wc_get_template_part( 'content', 'product' );
    }
    wp_reset_postdata(); ?>
<?php endforeach; ?>
0
votes

Try this code on your page template. It will get result for Woocommerce Separate Product Loops for Each Category.

$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>';
		
		
	//get product
		$args = array(
  'post_type'      => 'product', 
	'product_cat' => $cat->name,
  'posts_per_page' => $count,
  'paged'          => $paged,
);
$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) { 
    $query->the_post();
        ?>
		<span class="title"><h2>  <?php the_title(); ?> </h2></span>
       	<?php
    }
    wp_reset_postdata();
}

	}
 }