Create WooCommerce Product Category Programmatically

I am having trouble creating a woocommerce product category programmatically. I plan to automate the creation of product categories based on the creation of a new term in a specified custom taxonomy. Any help is greatly appreciated, Thank you.

1 Answer
1

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