N
N
Ninja Mate2017-03-21 23:32:25
WordPress
Ninja Mate, 2017-03-21 23:32:25

Why does this error occur when creating a subcategory for woocommerce products (creates twice)?

I have a function that creates categories for products.
when I try to use it to create subcategories and set parent, the product is created as the main product and gives an error that a similar product already exists.

function test(){

    $sub_cat_id = wp_insert_term(
        'TEST', // the term
        'product_cat',
        array(
            'parent' => 7331
        )
    );

    echo "------------------------------------" . "</br>";

    if ( is_wp_error( $sub_cat_id ) ) {
        echo "Error: " . $sub_cat_id->get_error_message() . "</br>";
        echo "------------------------------------" . "</br>";
    }

    return null;
}

------------------------------------
Error: A term with the name provided already exists with this parent.
------------------------------------

In this case, the product is created. How to win this?
Update:
The script is run twice. I run it as a plugin through the admin panel.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Klepko, 2017-03-22
@gaz12

wp_insert_term(
  'TEST',
  'product_cat',
  array(
  'description'=> 'test description',
  'slug' => 'mytest',
  'parent'=> 7331
  )
);

Try like this. Most likely somewhere such a term already exists. I would search the database for matches.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question