key_bearer
key_bearer

Reputation: 25

Woocommerce Separate Product Loops for Each Category

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?

Upvotes: 2

Views: 4269

Answers (2)

Wakar Ahmad Khan
Wakar Ahmad Khan

Reputation: 517

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();
}

	}
 }

Upvotes: 0

Amin Abdolrezapoor
Amin Abdolrezapoor

Reputation: 1847

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; ?>

Upvotes: 2

Related Questions