A
A
Aidar2018-04-13 08:58:52
WordPress
Aidar, 2018-04-13 08:58:52

How to make subcategories in woocomerce so that only products of these subcategories are under them?

Hello! The question is that I need to display subcategories with products on the category page.
The bottom line is that now woocomerce offers categories at the top, and then all products in a row. I need to somehow separate the belonging of goods to a specific subcategory. Type:
Category 1
- Subcategory 1
-- Commodity
-- Commodity
-- Commodity
- Subcategory 2
-- Commodity
-- Commodity
-- Commodity
Etc. And now it's offered like this:
Category 1
- Subcategory 1
- Subcategory 2
-- Commodity
-- Commodity
-- Commodity
-- Commodity
-- Commodity
-- Commodity

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2018-04-14
@azerphoenix

Hello!
Check out this code:

<!-- BEGIN -->
      <?php $post_type = 'product';
      $taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
      foreach( $taxonomies as $taxonomy ) :
          $terms = get_terms( $taxonomy );
          foreach( $terms as $term ) : ?>
      <div class="resources_column">
            <h4 class="resources_h4"><?php echo $term->name; ?></h4>
              <?php $args = array(
                      'post_type' => $post_type,
                      'posts_per_page' => -1,  //show all posts
                      'tax_query' => array(
                          array(
                              'taxonomy' => $taxonomy,
                              'field' => 'slug',
                              'terms' => $term->slug,) ) );
              $posts = new WP_Query($args);
              if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
                         <?php the_title();?>
              <?php endwhile; endif; ?>
      </div>
          <?php endforeach; ?>
      <?php endforeach; ?>
      <!-- END -->

Here the products will be displayed in columns and each under its own category. Seems like it should work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question