1

Hello (sorry for bad english) i want know 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 replace it with woocommerce category to get woocommerce catogory please help me asap

0

1 Answer 1

3

It's not working because you didn't pass the Woocoomerce's Product category taxonomy.

By default get_categories() function will returns all terms data of default category taxonomy, if you did't mention specific taxonomy slug inside the arguments list. So, To retrieve the terms data of any specific taxonomy, We must have to pass the slug of that taxonomy.

Woocommerce uses product_cat custom taxonomy to store all Product's categories.

function gaga_lite_category_lists( ) {

     $categories = get_categories(

        array(
            'hide_empty' =>  0,
            //'exclude'  =>  1,
            'taxonomy'   =>  'product_cat' // mention taxonomy here. 
        )
     );


     $category_lists = array();

     $category_lists[0] = __( 'Select Category' , 'gaga-lite' );

     foreach( $categories as $category ){

          $category_lists[$category->term_id] = $category->name;
     } 

     return $category_lists;

}

Function reference get_categories

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.