I want to loop through Woocommerce Product Catogories and show them in dropdown

I want to loop through Woocommerce Product Catogories and show them in dropdown. I have tried almost every piece of code available on internet but seems that they are not working for me. I’m very new in wordpress. Can anyone help me out? Currently i’m using this code but it’s not returning anything.

<?php

$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0;      // 1 for yes, 0 for no
$pad_counts = 0;      // 1 for yes, 0 for no
$hierarchical = 1;      // 1 for yes, 0 for no  
$title = '';
$empty = 0;

$args = array(
    'taxonomy' => $taxonomy,
    'orderby' => $orderby,
    'show_count' => $show_count,
    'pad_counts' => $pad_counts,
    'hierarchical' => $hierarchical,
    'title_li' => $title,
    'hide_empty' => $empty
);
$all_categories = get_categories($args);
foreach ($all_categories as $cat) {
    if ($cat->category_parent == 0) {
        $category_id = $cat->term_id;
        echo '<br /><a href="' . get_term_link($cat->slug, 'product_cat') . '">' . $cat->name . '</a>';
        ?>
        <?php

        $args2 = array(
            'taxonomy' => $taxonomy,
            'child_of' => 0,
            'parent' => $category_id,
            'orderby' => $orderby,
            'show_count' => $show_count,
            'pad_counts' => $pad_counts,
            'hierarchical' => $hierarchical,
            'title_li' => $title,
            'hide_empty' => $empty
        );
        $sub_cats = get_categories($args2);
        if ($sub_cats) {
            foreach ($sub_cats as $sub_category) {
                echo $sub_category->name;
            }
        }
    }
}
?>

Related posts

1 comment

  1. You want to show all category under parent
    For Example-
    Category 1
    – Subcategory 1
    – Subcategory 2
    Category 2
    – Subcategory 3
    – Subcategory 4
    Try this code.
    We are change parent=0 in get all category.

    $taxonomy = 'product_cat';
    $orderby = 'name';
    $show_count = 0;      // 1 for yes, 0 for no
    $pad_counts = 0;      // 1 for yes, 0 for no
    $hierarchical = 1;      // 1 for yes, 0 for no  
    $title = '';
    $empty = 0;
    
    $args = array(
        'taxonomy' => $taxonomy,
        'orderby' => $orderby,
        'show_count' => $show_count,
        'pad_counts' => $pad_counts,
        'hierarchical' => $hierarchical,
        'title_li' => $title,
        'hide_empty' => $empty,
        'parent' => 0,
    );
    $all_categories = get_categories($args);
    foreach ($all_categories as $cat) {
        if ($cat->category_parent == 0) {
            $category_id = $cat->term_id;
            echo '<br /><a href="' . get_term_link($cat->slug, 'product_cat') . '">' . $cat->name . '</a>';
            ?>
            <?php
    
            $args2 = array(
                'taxonomy' => $taxonomy,
                'child_of' => 0,
                'parent' => $category_id,
                'orderby' => $orderby,
                'show_count' => $show_count,
                'pad_counts' => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li' => $title,
                'hide_empty' => $empty
            );
            $sub_cats = get_categories($args2);
            if ($sub_cats) {
                foreach ($sub_cats as $sub_category) {
                    echo $sub_category->name;
                }
            }
        }
    }
    ?>
    

Comments are closed.