在通过代码插入新产品后,我手动插入了该属性:
$attributes = Array(
"type_product1" => $data1,
"type_product2" => $data2,
"type_product3" => $data3,
"type_product4" => $data4,
"type_product5" => $data5,
"type_product6" => $data5
);
foreach ($attributes as $key => $value) {
$term_taxonomy_ids = wp_set_object_terms( $product_id, $value, \'pa_\'.$key, true );
$product_attributes = Array (
\'pa_\'.$key => Array(
\'name\' => \'pa_\'.$key, // set attribute name
\'value\' => $value, // set attribute value
\'is_visible\' => 1,
\'is_variation\' => \'1\',
\'is_taxonomy\' => 1
)
);
}
//Add as post meta
update_post_meta($post_ID, \'_product_attributes\', $product_attributes);
之后,如果我进入products->attribute,我会看到属性名称的单个项的计数增加,但如果我进入name attributes(custom product attribute)下的单个产品,我将只看到最后一个属性(type\\u product6),该属性的值设置为其他属性不显示。。。。奇怪的是,如果我转到属性页,单击单个属性项的计数,它会显示具有此属性集的产品列表!
我怎样才能修复它?
SO网友:walle
the solution is:
foreach ($attributes as $attr => $value) {
$attr = \'pa_\'.$attr;
wp_set_object_terms($post_ID, $value, $attr);
$thedata[sanitize_title($attr)] = Array(
\'name\' => wc_clean($attr),
\'value\' => $value,
\'postion\' => \'0\',
\'is_visible\' => \'1\',
\'is_variation\' => \'1\',
\'is_taxonomy\' => \'1\'
);
}
update_post_meta($post_ID, \'_product_attributes\', $thedata);