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.
WooCommerce Product Category are treated as
product_cattaxonomy
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With