A
A
almenovr2021-08-03 10:28:35
WordPress
almenovr, 2021-08-03 10:28:35

How to make division into categories and subcategories in WooCommerce?

It is necessary to make a division of categories on the WooCommerce site, a division like on https://www.tools.by/
Maybe there is some kind of plugin or something like that. Split so that there is no web of goods on the main page.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-08-03
@artzolin

Such a list of categories is quite simple to make. I practically don't use plugins, but I can show you the code on the basis of which you can implement this
. so that they have a common key

$taxonomy = 'category';
$args = [
    'taxonomy' => $taxonomy, // название таксономии с WP 4.5
    'hide_empty' => false,
];

if ( $terms = get_terms( $args ) ) {
  $output = array();
  foreach ( $terms as $key => $term ) {
    if ( $term->parent == 0 ) {
      $output[$term->term_id]['parent']['title'] = $term->name;
      $output[$term->term_id]['parent']['link'] = get_term_link( $term->term_id, $taxonomy );
    } else {
      $output[$term->parent]['children'][$key]['title'] = $term->name;
      $output[$term->parent]['children'][$key]['link'] = get_term_link( $term->term_id, $taxonomy );
    }
  }
}

2. At the output, we get arrays of this type into the $output variable
6108f82f21835125924035.png
3. Now we just have to display all the values ​​in the form we need
if ( isset( $output ) && is_array( $output ) && !empty( $output ) ) {

  foreach ( $output as $key => $items ) {
    echo '<h2 class="title"><a href="' . $items['parent']['link'] . '" class="title-link">' . $items['parent']['title'] . '</a></h2>';
    if ( isset( $items['children'] ) ) {
      echo '<ul class="list">';
      foreach ( $items['children'] as $key => $item ) {
        echo '<li class="list-item"><a href="' . $item['link'] . '" class="list-link">' . $item['title'] . '</a></li>';
      }
      echo '</ul>';
    }
  } // end foreach

} // end if

PS. I found in the wp_options table the category_children option, which already has parent → child relationships, so you can use it and simplify the code above

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question