我需要先通过PHP以编程方式插入WooCommerce自定义属性,然后再插入其他属性。
我在这里找到了一个非常相似的问题-WooCommerce: Add a product attribute to the existing ones in a product,并使用了以下代码:
$taxonomy = 'pa_format';
$clean_keywords = array('Online');
$term_taxonomy_ids = wp_set_object_terms( $product_id, $clean_keywords, $taxonomy, true );
// Get existing attributes
$product_attributes = get_post_meta( $product_id, '_product_attributes', true);
// get the count of existing attributes to set the "position" in the array
$count = count($product_attributes);
// Insert new attribute in existing array of attributes (if there is any)
$product_attributes[$taxonomy] = array(
'name' => $taxonomy,
'value' => '',
'position' => $count, // added
'is_visible' => '1',
'is_variation' => '0', // added (set the right value)
'is_taxonomy' => '1'
);
// Save the data
update_post_meta( $product_id, '_product_attributes', $product_attributes );
工作正常。但是,我需要先插入属性,所以我尝试稍微更改一下数组,并使用array_unshift更改了update_post_meta。
$product_att = array($taxonomy=>
array(
'name' => $taxonomy,
'value' => '',
'position' => $count, // added
'is_visible' => '1',
'is_variation' => '0', // added (set the right value)
'is_taxonomy' => '1'
));
// Save the data
update_post_meta( $product_id, '_product_attributes', array_unshift($product_id->get_attributes(), $product_att) );
它不起作用...