• Resolved Aleksa Stevic

    (@aleshin99)


    Hi everybody,

    I have an issue with getting product_cat terms.
    I am extending WC_Integration class in order to create a small settings page in admin.

    I simply want to output a select with all product categories as options where the value is ID of the term and text (between <option> and </option>) is term name.

    I was unable to find woocommerce function which returns the array of product categories terms, so I used built-in WordPress function:

    
    $product_cats = get_terms([ 'taxonomy' => 'product_cat' ]);
    
    $product_cats_options = [];
    foreach ($product_cats as $product_cat) {
        $product_cats_options[$product_cat->term_id] = $product_cat->name;
    }
    
    $this->form_fields = [
        'club_price_product_cats'   => [
                    'title'         => __('Club Products', 'woocommerce'),
                    'type'          => 'select',
                    'description'   => __('Which product categories adopt the price from above.', 'woocommerce'),
                    'options'       => $product_cats_options
                ]
    ];
    
    

    The code above is in init_form_fields() function in my class.

    The problem is get_terms() returns WP_Error object with message ‘Invalid taxonomy’. So I did var_dump(get_taxonomies()) and it doesn’t return product_cat in the list.

    Some people pointed on the internet that product_cat isn’t there because it’s not registered until ‘init’ hook and my code runs before ‘init’ hook. But I don’t know how to solve the problem. I don’t call init_form_fields() myself, Woocoomerce does.

    Can anybody help me with this?

    Thanks.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Invalid taxonomy ‘product_cat’ in wc integration’ is closed to new replies.