0
votes

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)

    • Direktåtkomst Förrådslänga(SubCategory)
      • 7 kvm (product)
      • 6 kvm (product)
    • Entréplan (SubCategory)
      • 1 kbm (product)
      • 1.5 kvm (product)
  • Nacka (Parent Category)

    • Sample(SubCategory)
      • aa (product)
      • bbb (product)

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

2
Can you give a link to your website? What is the expected output?Hareesh Sivasubramanian

2 Answers

0
votes

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
                }
            }
        }   
    }
}
0
votes

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>';
}