S
S
Sergey Burduzha2019-11-20 19:37:46
WooCommerce
Sergey Burduzha, 2019-11-20 19:37:46

How to display products from categories and subcategories in woocommerce?

Good evening.
There are categories and subcategories for both products.
5dd56af145dff708725417.jpeg
You need to display the name of the category and if it has subcategories, then display them and then the products from this subcategory.
5 products from each category.
It should look like this
Category
Product Product Product
Category
Subcategory
Product Product Product
Subcategory
Product Product Product
Category
Product Product Product
Here's the code I wrote, but I can't put the subcategories in the 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 = 1;

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

<?php foreach ($all_categories as $cat): ?>
    <?php if ($cat->category_parent == 0): ?>
        <h2 class="products-section__title"><?php echo $cat->name; ?></h2>
        <div class="bs-products-loops">
                                  <?php
                                  $products = new WP_Query([
                                  'post_type' => 'product',
                                  'posts_per_page' => 5,
                                  'product_cat' => $cat->slug
                                  ]);
                                  ?>
                                  
                                  <?php if ($products->have_posts()): ?>
                                  
                                  <?php while ($products->have_posts()): ?><?php $products->the_post(); ?>
                                  
                                  <?php do_action('woocommerce_product_loop_start'); ?>
                                  
                                  <?php wc_get_template_part('content', 'product'); ?>
                                  
                                  <?php do_action('woocommerce_product_loop_end'); ?>
                                  
                                  <?php endwhile; ?><?php wp_reset_postdata(); ?>
                                  
                                  <?php endif; ?>
                                  </div>
    <?php else: ?>
    <?php $category_parent = $cat->category_parent; ?>
        <?php
        $args2 = array(
            'taxonomy' => $taxonomy,
            'child_of' => 0,
            'parent' => $category_parent,
            'orderby' => $orderby,
            'show_count' => $show_count,
            'pad_counts' => $pad_counts,
            'hierarchical' => $hierarchical,
            'title_li' => $title,
            'hide_empty' => $empty
        );
        $sub_cats = get_categories($args2); ?>
        <?php if ($sub_cats): ?>
            <?php foreach ($sub_cats as $sub_category): ?>
                <h3><?php echo $sub_category->name; ?></h3>
                <div class="bs-products-loops">
                    <?php
                    $products = new WP_Query([
                        'post_type' => 'product',
                        'posts_per_page' => 5,
                        'product_cat' => $sub_category->slug
                    ]);
                    ?>

                    <?php if ($products->have_posts()): ?>

                        <?php while ($products->have_posts()): ?><?php $products->the_post(); ?>
                                                          
                                                          <?php do_action('woocommerce_product_loop_start'); ?>
                                                          
                                                          <?php wc_get_template_part('content', 'product'); ?>
                                                          
                                                          <?php do_action('woocommerce_product_loop_end'); ?>
                                                          
                                                          <?php endwhile; ?><?php wp_reset_postdata(); ?>

                    <?php else: ?><?php endif; ?>
                </div>
            <?php endforeach; ?>

        <?php endif; ?>
    <?php endif; ?>
<?php endforeach; ?>

Tell me, where am I wrong?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question