T
T
TuMko2019-02-22 09:12:45
Taxonomy
TuMko, 2019-02-22 09:12:45

How to display a list of categories and their subcategories of Woocommerce products?

It is necessary to display on the page of the Woocommerce store a list of categories of the form:
Category 1
- Subcategory 1 of category 1
- Subcategory 2 of category 1
Category 2
- Subcategory 1 of category 2
- Subcategory 2 of category 2
When you select the desired category, the following page of the form opens:
Subcategory 1
- Subcategory 1 of the subcategory 1
- Subcategory 2 subcategories 1
Subcategory 2
- Subcategory 1 subcategories 2
- Subcategory 2 subcategories
2 2, 3... nesting levels, but only one nesting level should be displayed.

$parentid = get_queried_object_id();
  $args = array(
      'parent' => $parentid,
      'hide_empty' => false
  );
  $terms = get_terms( 'product_cat', $args );
  if ( $terms ) {   
      echo '<div class="content-container__catalog-list"><ul class="catalog-list">';
          foreach ( $terms as $term ) {              
              echo '<li class="catalog-list-block">';                        
                  //woocommerce_subcategory_thumbnail( $term ); 
                    echo '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . ' catalog-list-block__title">';
                        echo $term->name;
                    echo '</a>';                                                        
              echo '</li>';                                                        
      }
      echo '</ul></div>';
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kulikov, 2019-07-30
@it_proger29

<?php
      $args = array(
        'taxonomy' => 'product_cat',
        'hide_empty' => false,
      );
      $product_categories = get_terms( $args );

      $count = count($product_categories);

      if ( $count > 0 ){
        foreach ( $product_categories as $product_category ) {
            $thumbnail_id = get_woocommerce_term_meta( $product_category->term_id, 'thumbnail_id', true );
            echo '<div class="col-md-6 col-sm-12 col-xs-12">';
            echo '<article class="type-post">';
            echo '<div class="entry-cover" style="width: 388px; height: 295px;">';
            echo  '<a href="' . get_term_link( $product_category ) . '"><img style="background-image: url('.  wp_get_attachment_url( $thumbnail_id ) .')!important;background-size: 100%;background-repeat: no-repeat;background-size: cover; width: 100%; height: 100%;" /></a>';
            echo  '</div>';
            echo  '<div class="entry-block">';
            echo  '<div class="entry-title">';
            echo  '<a href="' . get_term_link( $product_category ) . '" title="' . $product_category->name . '"><h3>' . $product_category->name . '</h3></a>';
            echo  '</div>';
            echo  '<hr>';
            echo  '<div class="entry-content">';
            echo  '<p>' . $product_category->description . '</p>';
            echo  '</div>';
            echo  '</div>';
            echo  '</article>';
            echo  '</div>';
            

        }
      }
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question