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
Shortcode to display word count or reading time
1. 7. 2019 in
This shortcode "word-count" will display a count of words for whatever content is passed, and can optionally display an estimated reading time.
Read More
Aleš Sýkora wrote
ACF Repeater Post Object in oxygen code block
2. 10. 2020 in ,
You are using gutenberg and ACF and toolset and need to add related posts in slider, which you choose at the post you are editing? Also you need to put them in the content, exactly where you want? Create ACF repeater with Post Object field inside Create Oxygen Block for Gutenberg Add code block and […]
Read More
Aleš Sýkora wrote
Redirect non logged users to login page
Situation: Website with pages restricted only to logged in users and we do not use toolset content templates but Oxygen Builder. We are hiding the content to the non-logged in users with Oxygen conditionals but we would like to redirect user to the login page instead of displaying login form on every restricted page. Code […]
Read More
magnifiercross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram