i am trying to insert products from an array.
The product is successfully created and the values are inserted normally except the sub-category.
The issue here is that the subcategory is registered as top level (no parent) or it’s not registered at all.
Anyone have any idea on what i am missing here ?
Outside from function
$parent_cat_a= term_exists($myproduct['12'], 'category');
$parent_cat_a_id= $parent_term_a['term_id'];
eisagwghproion($myproductssimple);
Inside function
$product_to_create = array(
'post_content' => '',
'post_status' => "publish",
'post_title' => $myproduct['6'],
'post_status' => 'publish',
'post_date' => $myproduct['22'],
'post_parent' => '',
'post_type' => "product",
);
$post_id= wp_insert_post($product_to_create, true);
if (!$post_id) //
{
return false;
}
update_post_meta($post_id, '_price', $myproduct['17']);
update_post_meta($post_id, '_regular_price', $myproduct['17']);
update_post_meta($post_id, '_sku', $myproduct['2'] );
wp_set_object_terms($post_id, 'simple', 'product_type');
if(strlen($myproduct['13']) > 1){
$parent_cat_a = term_exists($myproduct['13'], 'product_cat');
if($parent_cat_a !== null) //εαν υπαρχει η υπο κατηγορια
{
wp_set_object_terms($post_id, $myproduct['13'] , 'product_cat');
}
else //εαν δεν υπάρχει η υπο κατηγορια
{
$parent_cat_a_id = $parent_cat_a['term_id'];
wp_insert_term(
$myproduct['13'], // the term
'product_cat', // the taxonomy
array(
'slug' => $myproduct['13'],
'parent'=> $parent_cat_a_id
)
);
wp_set_object_terms($post_id, $myproduct['13'] , 'product_cat');
}
}
else{wp_set_object_terms($post_id, $myproduct['12'] , 'product_cat');}