2

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.

  • What exactly are you trying to do? What is your question? – Krzysiek Dróżdż Jul 17 '18 at 20:50
2

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
) );
  • Thank you! Was stuck on this for hours. I greatly appreciate the help. – Tyler Jul 18 '18 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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