我正在尝试插入阵列中的产品
产品已成功创建,除子类别外,其他值均正常插入
这里的问题是,子类别已注册为顶级(无父级)或根本未注册。有人知道我在这里遗漏了什么吗
外部自功能
$parent_cat_a= term_exists($myproduct[\'12\'], \'category\');
$parent_cat_a_id= $parent_term_a[\'term_id\'];
eisagwghproion($myproductssimple);
内部功能
$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\');}