How to group woocommerce products by category in WordPress

Multi tool use
Multi tool use


How to group woocommerce products by category in WordPress



I have at least 4 Parent Categories and each parent category has a sub-category.



The categories in WordPress looks like this:



Lidingö (Parent Category)



Nacka (Parent Category)



I want to query the products in WordPress and they should be grouped by categories.



This is my current code:


<?php

$args = array(
'post_type' => 'product',
array(
'taxonomy' => 'product_cat'
),
'posts_per_page' => 6,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {

while ( $loop->have_posts() ) : $loop->the_post();

echo woocommerce_template_single_title();

endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();

?>



I think the code above is not correct because the same products are being displayed for every pagination.



enter image description here



Do you have any idea what is the correct query to group all woocommerce products by category? Thanks





Can you give a link to your website? What is the expected output?
– Hareesh Sivasubramanian
Jul 2 at 3:23




2 Answers
2



First you need to overwrite this woocommerce template to your current theme as you have to use custom loop to get products by category.



Just copy that template of woocommerce to your current_theme->create folder name (Woocommerce) -> Paste template to that folder.



Use Below Code:


$parent_terms= get_terms( array( 'taxonomy' => 'product_cat', 'parent' => 0 ) );
if($parent_terms= )
{
foreach( $parent_terms as $parent_term )
{
$child_terms = get_terms( array( 'taxonomy' => 'product_cat', 'parent' => $parent_term->term_id ) );
if($child_terms)
{
foreach( $child_terms as $child_term )
{
$product_args=
array(
'posts_per_page' => 50,
'post_type' => 'my_custom_type',
'posts_per_page' => 6,
'cat' => $child_term->term_id
);
$product_query = new WP_Query( $product_args );
while($product_query->have_posts()) : $product_query->the_post();
{
Here you will get product detail so you can display product details as you wanted
}
}
}
}
}



Your query is asking for all products that belong to the product category taxonomy - that is, all of them.



You need to narrow the search by adding the category you wish to search to the arguments. For example, if you wanted to search via the category slug, you would use:


$args = array(
'post_type' => 'product',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'category-slug1'
),
'posts_per_page' => 6,
);



To loop through all the categories on one page, you'll want something like this:


$my_categories = get_terms( 'TERM_NAME_HERE' );
$my_categories_count = count( $my_categories );

if ( $my_categories_count > 0 && is_array( $my_categories ) ) {
echo '<div class="wrap">';
foreach ( $my_categories as $single_cat ) { ?>
<h2><?php echo $single_cat->name; ?></h2>

<?php
$cat_posts_args = array(
'post_type' => 'product',
'order' => 'ASC',
'orderby' => 'date',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $single_cat->term_id,
'include_children' => false
)
)
);
$cat_posts = new WP_Query( $cat_posts_args );
if ( $cat_posts->have_posts() ) :
echo '<p>';
while ( $cat_posts->have_posts() ) : $cat_posts->the_post(); ?>

<a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span>: <?php echo get_the_excerpt(); ?></a><br>

<?php endwhile;
echo '</p>';
else :
if ( !$parent ) echo '<p>No products found.</p>';
endif;
wp_reset_postdata();
} // end foreach
echo '</div>';
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

V,wqDR,uTHpTl2hsu,1YephJ78GVeB 2IsV5w,xD njJhBW81vcNQxBM6Kw I2kAmsn8
HEBWfjXPiwO1,OgOPRk4ql7clVf2JPix7vYE cVqtRy1zlE

Popular posts from this blog

Rothschild family

Cinema of Italy