title: WP_insert_term creating duplicate categories during WooCommerce product import
body: <p>I've been racking my brain for a few days on this issue and can't figure out why my code to import is creating duplicate product categories.</p>
  <p><strong>Problem:</strong> I am pulling in about 30,000 products from the third party API in JSON format it is then is inserted into a custom database, from which I pull each product's data and format for importing into WooCommerce, these processes are working fine the problem comes when I am inserting the category terms. When categories are inserted using wp_insert_term some categories are duplicated but have a different URL slug.</p>
  <p>ie...</p>
  <pre><code>— Ranges, Cooktops &amp; Ovens
  — — Range Hoods &amp; Downdraft Ventilation (slug: range-hoods-downdraft-ventilation ) 
  — — Range Hoods &amp; Downdraft Ventilation (slug: range-hoods-downdraft-ventilation-ranges-cooktops-ovens)
  
  The child category gets duplicated but with a different slug.
  
  </code></pre>
  <p><strong>This is the Category array I'm looping through to create the hierarchical category paths:</strong></p>
  <p><code>&quot;categoryPath&quot;:[{&quot;id&quot;:&quot;cat00000&quot;,&quot;name&quot;:&quot;Company Name&quot;},{&quot;id&quot;:&quot;abcat0900000&quot;,&quot;name&quot;:&quot;Appliances&quot;},{&quot;id&quot;:&quot;abcat0904000&quot;,&quot;name&quot;:&quot;Ranges, Cooktops &amp; Ovens&quot;},{&quot;id&quot;:&quot;abcat0904002&quot;,&quot;name&quot;:&quot;Range Hoods &amp; Downdraft Ventilation&quot;}]</code></p>
  <p><strong>Here is my single product import code the relevant category portion in at the bottom starting with $categoryPath, just wanted to give some context of what Im doing:</strong></p>
  <pre><code>$result= $wpdb-&gt;get_var( &quot;SELECT `results_array`
                                               FROM $table_name 
                                               WHERE `arr_serialize2` = $textProductSku  
                                               AND  `update_type` = 'create' 
                                          &quot;);
  
              $final_product_formate_arrays = json_decode($result, true);
  
             
  
              $post = array(
                  'post_title' =&gt; $final_product_formate_arrays['parent_or_simple_product']['title'],
                  'post_content' =&gt; $final_product_formate_arrays['parent_or_simple_product']['longDescription'],
                  'post_excerpt' =&gt; isset( $final_product_formate_arrays['parent_or_simple_product']['shortDescription']) ?  $final_product_formate_arrays['parent_or_simple_product']['shortDescription'] : '' ,
                  'post_status' =&gt; &quot;publish&quot;,
                  'post_parent' =&gt; '',
                  'post_type' =&gt; &quot;product&quot;,
              );
              $post_id = wp_insert_post( $post );
              if (!$post_id) 
              {
                  return false;
              }
              update_post_meta($post_id,'_complete_product_data',json_encode($final_product_formate_arrays));
              update_post_meta($post_id, '_sku', $final_product_formate_arrays['parent_or_simple_product']['sku']);
              update_post_meta( $post_id, '_visibility', 'visible' ); 
              
              if($final_product_formate_arrays['parent_or_simple_product']['inStoreAvailability'] &gt; 0) 
              {
              
                  update_post_meta( $post_id, '_manage_stock', &quot;yes&quot; );
                  update_post_meta( $post_id, '_stock', $final_product_formate_arrays['parent_or_simple_product']['stockQuantity'] );
                  update_post_meta( $post_id, '_stock_status', 'instock');
  
              } elseif($final_product_formate_arrays['is_variation']){
                  
                  update_post_meta( $post_id, '_manage_stock', &quot;no&quot; );
                  update_post_meta( $post_id, '_stock_status', 'instock');
                  
              } else  {
  
  
                  update_post_meta( $post_id, '_manage_stock', &quot;yes&quot; );
                  update_post_meta( $post_id, '_stock_status', 'outofstock');
                  update_post_meta( $post_id, '_stock', 0 );
              }
  
              update_post_meta( $post_id, '_regular_price', $final_product_formate_arrays['parent_or_simple_product']['regularPrice'] );
              update_post_meta( $post_id, '_sale_price', $final_product_formate_arrays['parent_or_simple_product']['salePrice'] );
              update_post_meta( $post_id, '_price', $final_product_formate_arrays['parent_or_simple_product']['salePrice'] );
              update_post_meta( $post_id, '_weight', $final_product_formate_arrays['parent_or_simple_product']['weight'] );
              update_post_meta( $post_id, '_length', $final_product_formate_arrays['parent_or_simple_product']['depth'] );
              update_post_meta( $post_id, '_width', $final_product_formate_arrays['parent_or_simple_product']['width'] );
              update_post_meta( $post_id, '_height', $final_product_formate_arrays['parent_or_simple_product']['height'] );
              update_post_meta( $post_id, '_wc_average_rating', $final_product_formate_arrays['parent_or_simple_product']['reviewAverage'] );
              
  
           
  
                  **$categoryPath = $final_product_formate_arrays['parent_or_simple_product']['categoryPath'];
                  if(!empty($categoryPath))
                  {
                      $parent_id='';
                      
                      
                      foreach ($categoryPath as $key =&gt; $value) {
                          
                          if($key!=0) {
                              $new_str = str_replace( array( ',', '&amp;' ), '-', $value['name'] );
                              $new_str = sanitize_title( $new_str );
                              $parent_term = term_exists( $parent_id, 'product_cat' );  // array is returned if taxonomy is given
                              if( isset( $parent_term )) {
                              $parent_id = $parent_term['term_id'];  // get numeric term id
                              
                              } else{
                                  $parent_id = 0;
                              }
                                                    
                                      $term = wp_insert_term( $value['name'], 'product_cat', [
                                          'description'=&gt; $value['name'],
                                          'parent' =&gt; intval( $parent_id ),
                                          'slug' =&gt;  $new_str,
                                      ]
                                  );
                          
                                                
                         if( is_wp_error( $term ) &amp;&amp; isset( $term-&gt;error_data['term_exists'] ) )
                          {
                              $term_id = isset( $term-&gt;error_data['term_exists'] ) ?  $term-&gt;error_data['term_exists'] :'';
                              $term_error = $term-&gt;get_error_message();
                              $term_error .=&quot;\n&quot;;
                              error_log($term_error, 3, $pluginlog);
                          } else if ( !is_wp_error( $term ) ) {
                              $new_term_id = isset( $term['term_id']) ? $term['term_id'] : '';
                              $term_id = $new_term_id;
                          }
                          $term_id = intval($term_id);
                         
                                
                          $parent_id = !empty($term_id)?$term_id:'';
                                         
                        wp_set_post_terms($post_id, $term_id, 'product_cat');**
  
                     
                      // wp_set_object_terms($post_id, $term_id, 'product_cat');
                      } else {
  
                          continue;
                      }
  
                      
                    }
  
                  }
  
  </code></pre>