How to add categories to products in woocommerce programatically?

I have studies the database intensively and know which all tables are updated when a category is added to a product. Is there any inbuilt woocommerce function to add categories? It updates 2 or 3 tables but want to leave it to woocommerce.

Thanks for your help.

Related posts

1 comment

  1. I think the function you are looking for is called wp_insert_term() in WordPress core.
    Woocommerce uses WordPress-taxonomy called product_cat

    So if you want to add some categories for example phones:

    function wpse_insert_term()
    {
        wp_insert_term(
          'phones',
          'product_cat', // the taxonomy
          array(
            'description'=> 'A yummy phone.',
            'slug' => 'phone',
          )
        );
    }
    add_action('init', 'wpse_insert_term');
    

Comments are closed.