php - woocommerce 的问题设置子类别
问题描述
我正在尝试从数组中插入产品。
产品创建成功,除子类别外,值正常插入。
这里的问题是子类别注册为顶级(没有父级)或者根本没有注册。
任何人都知道我在这里缺少什么:(?
$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_cata = term_exists($myproduct['13'], 'product_cat');
if($parent_cata !== null) //
{
wp_set_object_terms($post_id, $myproduct['13'] , 'product_cat');
}
else
{
$parent_term_a_id = $parent_cata['term_id'];
wp_insert_term(
$myproduct['13'],
'product_cat',
array(
'slug' => 'sub-cat',
'parent'=> $parent_term_a_id
)
);
wp_set_object_terms($post_id, $myproduct['13'] , 'product_cat');
}
}
else{wp_set_object_terms($post_id, $myproduct['12'] , 'product_cat');}
解决方案
推荐阅读
- go - Golang中的TCP固定大小消息成帧方法
- batch-file - [已解决]我写了一个程序放在start文件夹下消失了但是还能运行
- python - Pandas Dataframe 旋转:从列值创建新列
- yocto - 如何本地编译 openssl - yocto 中的自定义版本?
- datetime - 如何更改 Google 表单响应中的日期格式?
- python - 在 Altair 中停用工具提示
- arrays - 如何在 numpy 数组中将值逐渐减小到零?
- ios - Tabbar iPad App to Sidebar macOS Catalyst app
- javascript - openUI5中的身份验证我怎么能做到这一点?
- android - 从 savedInstanceState 恢复时 TableLayout 不显示