Aleš Sýkora wrote

WooCommerce category clustered by child taxonomies in WordPress

6. 2. 2021 in

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

  }
}
    ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from category
Aleš Sýkora wrote
Comments Shortcode for Content Templates
11. 11. 2019 in
Display comments section whetever you want.
Read More
Aleš Sýkora wrote
ACF Masonry Left to right ordered Gallery in Oxygen Builder /w Colcade
Would you like to use ACF masonry gallery with left to right flow of images? Good, let’s do that! But at first, why not to use standard Gallery module in Oxygen Builder? The problem with Oxygen Masonry gallery The main problem is ordering of images. Standard masonry layout is made by Flexbox columns. So Your […]
Read More
Aleš Sýkora wrote
Child posts menu for custom post type parent item
1. 2. 2021 in
This code will help you display menu of childs on current post. For example: Product 1 = parent has those child pages: – Product usage– How to– Disclaimer– Calculator When you put this child items menu in your template, it will show all childs of product 1 on all child pages and also on product […]
Read More
magnifiercross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram