Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display woocommerce category list

How can I get the category list in woocommerce? With this code, I get wordpress category list:

function gaga_lite_category_lists(){
    $categories = get_categories(
        array(
            'hide_empty' => 0,
            'exclude' => 1
        )
    );


$category_lists = array();
$category_lists[0] = __('Select Category', 'gaga-lite');
foreach($categories as $category) :
    $category_lists[$category->term_id] = $category->name;
endforeach;
return $category_lists;

}

I want to replace it with woocommerce category.

like image 732
Abraham Avatar asked Feb 22 '26 19:02

Abraham


1 Answers

WooCommerce Product Category are treated as product_cat taxonomy

Here is the code.

function gaga_lite_category_lists()
{
    $category_lists = array();
    $category_lists[0] = __('Select Category', 'gaga-lite');
    $args = array(
        'taxonomy' => 'product_cat',
        'orderby' => 'name',
        'hierarchical' => 0, // 1 for yes, 0 for no  
        'hide_empty' => 0,
        'exclude' => 1 //list of product_cat id that you want to exclude (string/array).
    );
    $all_categories = get_categories($args);
    foreach ($all_categories as $cat)
    {
        if ($cat->category_parent == 0)
        {
            $category_lists[$cat->term_id] = $cat->name;
            //get_term_link($cat->slug, 'product_cat')
        }
    }
    return $category_lists;
}
like image 189
Raunak Gupta Avatar answered Feb 27 '26 02:02

Raunak Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!