Create WooCommerce Product Category Programmatically

To create a taxonomy term programmatically you can use wp_insert_term function.

<?php wp_insert_term( $term, $taxonomy, $args = array() ); ?>

It has 3 params:

$term (int|string) (required) The term to add or update. Default: None

$taxonomy (string) (required) The taxonomy to which to add the term.
Default: None

$args (array|string) (optional) Change the values of the inserted term
Default: None

WooCommerce categories are stored as terms in product_cat taxonomy, so if you want to create some new category, you can use this code:

wp_insert_term( 'My New Category', 'product_cat', array(
    'description' => 'Description for category', // optional
    'parent' => 0, // optional
    'slug' => 'my-new-category' // optional
) );

Leave a Comment