Support » Plugin: WooCommerce » Woocommerce: sorting products by their category in a foreach loop

  • Resolved adzetko

    (@adzetko)


    I’m doing a custom product-archive.php as my main shop page, so, in my child theme I have this custom product loop:

    
    woocommerce_product_loop_start();
    if ( wc_get_loop_prop( 'total' ) ) {
    
        $categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
        foreach ( $categories as $category ) {
    
            $title_value = (mb_strlen($category->parent) + 1);
    
            print_r("<h$title_value>$category->name</h$title_value>");
    
            while ( have_posts() ) {
                the_post();
    
                /**
                 * Hook: woocommerce_shop_loop.
                 */
                do_action( 'woocommerce_shop_loop' );
    
                wc_get_template_part( 'content', 'product' );
            }
        }
    }
    woocommerce_product_loop_end();
    

    The problem is that actually, it displays all the products available on the shop, for each category, because I don’t know how to tell the product-fetching query to only show products from the actual category. How can I tell it to do so?

    • This topic was modified 1 year, 2 months ago by adzetko.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support thup90

    (@thup90)

    Hi there,

    You can use the conditional tag is_product_category() to get only products from a category: https://docs.woocommerce.com/document/conditional-tags/#section-5

    Hari Shanker R

    (@harishanker)

    Automattic Happiness Engineer

    Hi @adzetko

    Just wanted to give you some additional inputs on your question.

    I looked at the code you have shared.

    I can see that you have trie to get a category list with:

    
     $categories = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );
        foreach ( $categories as $category ) {
    

    However, the code to display the posts is as follows:

    
      while ( have_posts() ) {
                the_post();
    

    I can see that you got this from here: https://github.com/woocommerce/woocommerce/blob/0e2ed1f408cae6989a508d6f23b233c20ae31aa3/templates/archive-product.php#L61-L62

    This returns all products when used in a custom template. In order to display the category list, you will need to call a new WP_Query() with the $category variable.

    This explains how you can generate a WP_Query() for a category variable: https://stackoverflow.com/a/47650240

    You can use this WP_Query to generate products instead of using: while ( have_posts() ) { the_post(); alone (which generates all products).

    For example, if you use the variable $loop to generate a WP_Query() that has the products belonging to the category (as mentioned in stackoverflow article I shared above), you could use the following code to list the posts belonging to that category alone:

    
            if ( $loop->have_posts() ) {
                while ( $loop->have_posts() ) : $loop->the_post();
                    wc_get_template_part( 'content', 'product' );
                endwhile;
            } else {
                echo __( 'No products found' );
            }
    

    I hope that makes sense.

    Let us know if you have other questions. We’re happy to help!

    Plugin Support Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Howdy!

    We haven’t heard back from you in a while, so I’m going to go ahead and mark this thread as resolved. If you have any other questions please start a new thread.

    Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce: sorting products by their category in a foreach loop’ is closed to new replies.