WooCommerce category clustered by child taxonomies in WordPress

Author: Aleš Sýkora, 6. 2. 2021

If you need to display your parent product category sorted by child taxonomies in clusters - for each child taxonomy it owns row - for example:

Breakfasts Category Archive - Parent category

  • Vegetarian - child category
    • Product 1
    • Product 2
  • Vegan - child category
    • Product 1
    • Product 2
  • Meat lovers - child category
    • Product 1
    • Product 2

You can edit and use my code for it. It can be used in Oxygen custom code block. It simply get all children terms and then for each children term use wp query loop to display name and price of product (you can add more fields easily by your custom code).

Add it to your Oxygen Custom Code block or to your archive template.

<?php
$term = get_queried_object(); // Get current archive page
$children = get_terms($term->taxonomy, array(
  'parent'    => $term->term_id,
  'hide_empty' => false
));

if ($children) {
  foreach ($children as $subcat) {
    echo '<h2><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . // Display child taxonomy name
      $subcat->name . ' (' . $subcat->count . ')' . '</a></h2>' . $subcat->description; // Display child taxonomy item count
    $kategorie = $subcat->slug; // Set child category slug for each query of products
    $args = array(
      'post_type' => 'product',
      'product_cat' => $kategorie, 
    );
    $loop = new WP_Query($args);
    if ($loop->have_posts()) {
      while ($loop->have_posts()) : $loop->the_post(); ?>
        <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); //Display product title ?></a></h4>
        <?php $product = wc_get_product(get_the_ID()); // get the WC_Product Object ?>
        <p><?php echo $product->get_price_html(); // Display product Price ?></p>
        <?php endwhile; ?><?php
                        } else {
                          echo __('No products found');
                        }
                        wp_reset_postdata(); // Reset Query
                          ?>
        <!--/.products-->
    <?php

  }
}
    ?>