Justin
Justin

Reputation: 73

WooCommerce products ordered by categories

I have WooCommerce attribute archive pages where I need to show the products sorted by category. Like so:

Category A

Category B

etc.

What the result of my current custom loop is now:

Category A

Category B

etc.

So it is basically showing all products within the attribute term within every category, even if the product is not even in this category. I think I am overlooking something, but I can't figure out what?

<?php
/**
 * Show products sorted by category
 */

$taxonomy       = 'product_cat';
$orderby        = 'menu_order';
$order          = 'ASC';
$show_count     = 1; // 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          = 1; // 1 for hide, 0 for show

$args = array(
    'taxonomy'      => $taxonomy,
    'orderby'       => $orderby,
    'order'         => $order,
    '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) :

    $term_link = get_term_link( $cat );

    if( $cat->category_parent == 0 ) :

        $category_id    = $cat->term_id;
        $thumbnail_id   = get_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image          = wp_get_attachment_url( $thumbnail_id );

        echo '<div id="' . $cat->slug . '" class="category ' . $cat->slug . '"><h2 class="category-title"><a href="'. esc_url( $term_link ) .'">'.$cat->name.'</a></h2>';

            $loop = new WP_Query( $args );

            while ( have_posts() ) : the_post();

                global $product;

                wc_get_template_part( 'content', 'product' );

            endwhile; // end of the loop. ?>

            </div><!-- end .category -->

            <?php

            wp_reset_query();

        endif;

    endforeach;

    wp_reset_query();

    ?>

Upvotes: 1

Views: 4072

Answers (2)

Ankur Bhadania
Ankur Bhadania

Reputation: 4148

Argument for get_categories() and WP_Query() are different.

Argument for get product categories wise.

$args_for_product = array(
            'tax_query' => array(array('taxonomy' =>$taxonomy,'field' => 'slug','terms' =>$cat->slug)),
            'orderby'       => $orderby,
            'order'         => $order,
);

used tax_query for pass the categories in product query. other argument (like show_count,pad_counts,hierarchical,title_li,hide_empty are not pass in WP_Query) not need. put above code in category loop

also change in while loop have_post() to $loop->have_post() and the_post() to $loop->the_post()

Complete code

<?php
/**
 * Show products sorted by category
 */

$taxonomy       = 'product_cat';
$orderby        = 'menu_order';
$order          = 'ASC';
$show_count     = 1; // 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          = 1; // 1 for hide, 0 for show

$args = array(
    'taxonomy'      => $taxonomy,
    'orderby'       => $orderby,
    'order'         => $order,
    '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) :

    $term_link = get_term_link( $cat );

    if( $cat->category_parent == 0 ) :

        $category_id    = $cat->term_id;
        $thumbnail_id   = get_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image          = wp_get_attachment_url( $thumbnail_id );

        echo '<div id="' . $cat->slug . '" class="category ' . $cat->slug . '"><h2 class="category-title"><a href="'. esc_url( $term_link ) .'">'.$cat->name.'</a></h2>';

        $args_for_product = array('type'=> 'product',
            'post_status'   => 'publish',
            'tax_query' => array(array('taxonomy' =>$taxonomy,'field' => 'slug','terms' =>$cat->slug)),
            'orderby'       => $orderby,
            'order'         => $order,
        );
            $loop = new WP_Query( $args_for_product );

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

                global $product;

                wc_get_template_part( 'content', 'product' );

            endwhile; // end of the loop. ?>

            </div><!-- end .category -->

            <?php

            wp_reset_query();

        endif;

    endforeach;

    wp_reset_query();

    ?>

Upvotes: 1

Shital Marakana
Shital Marakana

Reputation: 2887

add below code for fetch category wise product

    <?php
/**
 * Show products sorted by category
 */

$taxonomy       = 'product_cat';
$orderby        = 'menu_order';
$order          = 'ASC';
$show_count     = 1; // 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          = 1; // 1 for hide, 0 for show


$args = array(
    'taxonomy'      => $taxonomy,
    'orderby'       => $orderby,
    'order'         => $order,
    '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) :

    $term_link = get_term_link( $cat );

    if( $cat->category_parent == 0 ) :

        $category_id    = $cat->term_id;
        $thumbnail_id   = get_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image          = wp_get_attachment_url( $thumbnail_id );

        echo '<div id="' . $cat->slug . '" class="category ' . $cat->slug . '"><h2 class="category-title"><a href="'. esc_url( $term_link ) .'">'.$cat->name.'</a></h2>';

            $product_args = array(
                'type'          => 'product',
                'post_status'   => 'publish',
                'orderby'       => 'name',
                'order'         => 'DESC',
                'pad_counts'    => false,
                'hierarchical'  => 1,
                'hide_empty'    => 0,
                'tax_query'                => array(
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'id',
                        'terms' => $cat->term_id
                    )
                ),
            );

            $loop = new WP_Query( $product_args );

            while ( have_posts() ) : the_post();

                global $product;

                wc_get_template_part( 'content', 'product' );

            endwhile; // end of the loop. ?>

            </div><!-- end .category -->

            <?php

            wp_reset_query();

        endif;

    endforeach;

    wp_reset_query();

 ?>

Upvotes: 1

Related Questions