T
T
TopdiR2013-12-29 14:24:28
PHP
TopdiR, 2013-12-29 14:24:28

How to display a dropdown menu with subcategories on a WooCommerce category page?

Hello dear experts!
There is a task on the WooCommerce category page to display a drop-down menu with subcategories of this category. Any solution with code or a plugin will do.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Zelenin, 2013-12-29
@zelenin

codex.wordpress.org/Function_Reference/get_terms

M
Mad-cote, 2016-10-01
@Mad-cote

Well, maybe my solution will be a bit off topic, but if you need to display your subcategories in each div of the category, then the solution is very simple:
On the wc-template-functions.php page

function woocommerce_subcategory_list($category) {

    $product_categories = get_categories( apply_filters( 'woocommerce_product_subcategories_args', array(
        'child_of'       => $category->term_id ,
        'menu_order'   => 'ASC',
        'hide_empty'   => 0,
        'hierarchical' => 1,
        'taxonomy'     => 'product_cat',
        'pad_counts'   => 1
    ) ) );


    echo "<ul>";
    foreach($product_categories as $product) {
        echo "<li>$product->name</li>";
    }
    echo "</ul>";
}

On the wc-template-hooks.php page
And then in the right place (for example, in content-product_cat.php) we insert:
do_action( 'woocommerce_sub_list', $category);

T
TopdiR, 2013-12-29
@TopdiR

@zelenin , thanks for the tip.
I found a code on GitHub that displays almost what I need:

<?php

add_shortcode( 'product_categories_dropdown', 'woo_product_categories_dropdown' );
 
function woo_product_categories_dropdown( $atts ) {
 
  extract(shortcode_atts(array(
    'count'         => '1',
    'hierarchical'  => '1',
    'orderby' 	    => ''
    ), $atts));
  
  ob_start();
  
  $c = $count;
  $h = $hierarchical;
  $o = ( isset( $orderby ) && $orderby != '' ) ? $orderby : 'order';
    
  // Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
  woocommerce_product_dropdown_categories( $c, $h, 0, $o );
 
  ?>
  <script type='text/javascript'>
  /* <![CDATA[ */
    var product_cat_dropdown = document.getElementById("dropdown_product_cat");
    function onProductCatChange() {
      if ( product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value !=='' ) {
        location.href = "<?php echo home_url(); ?>/?product_cat="+product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value;
      }
    }
    product_cat_dropdown.onchange = onProductCatChange;
  /* ]]> */
  </script>
  <?php
  
  return ob_get_clean();

It outputs all WooCommerce store categories with subcategories in the select. If you can help me modify it so that only subcategories of the current category are displayed in the select, I will be very grateful to you.

T
TopdiR, 2014-01-05
@TopdiR

@zelenin , applying and modifying the code from the example for the get_terms function, I get a list of all categories, not child ones. Problem with child_of argument? Please point out my mistake.

<?php
$args = array( 'child_of' => $current_term->term_id, 'orderby' => 'name', 'order' => 'ASC' );
$terms = get_terms( 'product_cat', $args );
$count = count($terms);
 if($count > 0){
     echo "<ul>";
     foreach ($terms as $term) {
       echo "<li>".$term->name."</li>";

     }
     echo "</ul>";
 }
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question