以下是我如何以编程方式向woocommerce
中的产品添加属性:
if($item->POIOTHTA != null && !empty($item->POIOTHTA)){
wp_set_object_terms( $post_id, $item->POIOTHTA, 'pa_poiotita', true );
$att_quality = Array('pa_poiotita' =>Array(
'name'=>'pa_poiotita',
'value'=>$item->POIOTHTA,
'is_visible' => '1',
'is_taxonomy' => '1'
));
}
if($item->MHKOS != null && !empty($item->MHKOS)){
wp_set_object_terms( $post_id, $item->MHKOS, 'pa_mikos', true );
$att_length = Array('pa_mikos' =>Array(
'name'=>'pa_mikos',
'value'=>$item->MHKOS,
'is_visible' => '1',
'is_taxonomy' => '1'
));
}
if($item->PAXOS != null && !empty($item->PAXOS)){
wp_set_object_terms( $post_id, $item->PAXOS, 'pa_paxos', true );
$att_thickness = Array('pa_paxos' =>Array(
'name'=>'pa_paxos',
'value'=>$item->PAXOS,
'is_visible' => '1',
'is_taxonomy' => '1'
));
}
if($item->DIAMETROS != null && !empty($item->DIAMETROS)){
wp_set_object_terms( $post_id, $item->DIAMETROS, 'pa_diametros', true );
$att_diameter = Array('pa_diametros' =>Array(
'name'=>'pa_diametros',
'value'=>$item->DIAMETROS,
'is_visible' => '1',
'is_taxonomy' => '1'
));
}
update_post_meta( $post_id, '_product_attributes', array_merge((array)$att_quality, (array)$att_length, (array)$att_thickness, (array)$att_diameter));
问题是,如果在另一个更新中更改了属性,则不会替换现有的属性,而是添加新的属性。
如何删除不再使用的属性,或在插入新属性之前将其全部删除?
一开始我试着调用这个函数,但是没有解决问题:delete_post_meta($post_id, '_product_attributes');
发布于 2021-05-13 23:12:01
您当前已设置为追加而不是覆盖,如wordpres developer docs中所述
wp_set_post_terms( int $post_id, string|array $tags = '', string $taxonomy = 'post_tag', bool $append = false )
如下所示,通过将append设置为false来更改代码
if($item->POIOTHTA != null && !empty($item->POIOTHTA)){
wp_set_object_terms( $post_id, $item->POIOTHTA, 'pa_poiotita', false);
$att_quality = Array('pa_poiotita' =>Array(
'name'=>'pa_poiotita',
'value'=>$item->POIOTHTA,
'is_visible' => '1',
'is_taxonomy' => '1'
));
}
if($item->MHKOS != null && !empty($item->MHKOS)){
wp_set_object_terms( $post_id, $item->MHKOS, 'pa_mikos', false);
$att_length = Array('pa_mikos' =>Array(
'name'=>'pa_mikos',
'value'=>$item->MHKOS,
'is_visible' => '1',
'is_taxonomy' => '1'
));
}
if($item->PAXOS != null && !empty($item->PAXOS)){
wp_set_object_terms( $post_id, $item->PAXOS, 'pa_paxos', false);
$att_thickness = Array('pa_paxos' =>Array(
'name'=>'pa_paxos',
'value'=>$item->PAXOS,
'is_visible' => '1',
'is_taxonomy' => '1'
));
}
if($item->DIAMETROS != null && !empty($item->DIAMETROS)){
wp_set_object_terms( $post_id, $item->DIAMETROS, 'pa_diametros', false);
$att_diameter = Array('pa_diametros' =>Array(
'name'=>'pa_diametros',
'value'=>$item->DIAMETROS,
'is_visible' => '1',
'is_taxonomy' => '1'
));
}
update_post_meta( $post_id, '_product_attributes', array_merge((array)$att_quality, (array)$att_length, (array)$att_thickness, (array)$att_diameter));
https://stackoverflow.com/questions/67520421
复制相似问题