向WooCommerce产品添加变体时,它会添加一个空白属性。这意味着当我尝试添加我自己的属性时,它会被附加到现有数组。
我正在运行的示例代码:
$product_variation = wc_get_product( $variation_id );
$product_variation->set_attributes(array('attribute_pa_varinfo' => 'blue'));
$product_variation->save();
然后当我var_dump($product_variation);
时,我得到以下内容:
["attributes"]=>
array(2) {
[0]=>
string(0) ""
["pa_varinfo"]=>
string(4) "5034"
}
因此,当我在WooCommerce管理员中查看该产品时,我的所有变体都存在,但属性仍然停留在所有这些属性的“任意选项”。
奇怪的是当我从wp-admin“更新”产品的所有变体然后选择正确的属性。
有没有人遇到过这个问题,或者对我能做什么有任何想法?
另一个例子,如果我运行以下内容:
$product_variation = wc_get_product( $variation_id );
$product_variation->set_attributes( array ( 'simon' => 'confused' ) );
$product_variation->save();
var_dump($product_variation->get_attributes());
返回:
array(2) {
[0]=> string(0) ""
["simon"]=> string(8) "confused"
}
第一件商品来自哪里?我似乎无法清除它。
答案 0 :(得分:1)
Update (related to your update and comments)
To resume (our comments): The product attribute exist. Also all terms for this attribute are defined and set in the parent variable product (in the "Attribute" settings tab)
I have maid some tests:
pa_varinfo
) with 4 values (term names): 104 mm
, 110 mm
, 130 mm
and 140 mm
(so term slugs are like 104-mm
…). 使用此代码时(与您的代码类似):
$parent_product = wc_get_product( 738 ); // Get the variable product
$variation_ids = $parent_product->get_children(); // Get all children variations (Here only one)
// Iterating through each variation
foreach( $variation_ids as $variation_id ){
$variation = wc_get_product($variation_id);
$variation->set_attributes(array('pa_varinfo' => '104-mm'));
$variation->save();
}
注意我在属性中使用分类名称,在数组中使用术语SLUG ...
所以我不知道你做错了什么 ......
当您设置不存在和/或未注册为父变量产品的后期的属性词时,会发生这种情况。你可以试试这个:
// Get an instance of the WC_Product_Variation object
$variation = wc_get_product( $variation_id );
// Initialising variables
$taxonomy = 'pa_varinfo'; // The taxonomy
$term_name = 'Blue'; // The term "NAME"
// Check if the term exist and if not we create it.
if( ! term_exists( $term_name, $taxonomy ) )
wp_insert_term( $term_name, $taxonomy );
// Get an instance of the WP_Term object
$term = get_term_by( 'name', $term_name, $taxonomy );
// Get the post terms names from the parent variable product.
$post_term_names = wp_get_post_terms( $variation->get_parent_id(), $taxonomy, array('fields' => 'names') );
// Check if the post term exist and if not we set it in the parent variable product.
if( ! in_array( $term_name, $post_term_names ) )
wp_set_post_terms( $variation->get_parent_id(), $term_name, $taxonomy, true );
// Now you can set the term for the attribute in your variation
$variation->set_attributes( array( $taxonomy => $term->slug ) );
$variation->save(); // Registering the data
// Get an instance of the parent WC_Product_Variable object
$parent_product = wc_get_product( $variation->get_parent_id() );
// Sync the data of the variation in the parent variable product
$parent_product->sync( $variation_id );
这是经过测试和运作的
假设您已经在WooCommerce中创建了附加属性...... ,您将获得:
答案 1 :(得分:1)
事实证明,问题在于对主要父产品实际设置了属性,我通过添加wc_attribute_taxonomy_name('varinfo') =>
(下面的第2行)传递了一个未命名的数组,这正确地保存了数据和删除我有的空白数组。
$product_attributes = array(
wc_attribute_taxonomy_name('varinfo') =>
array (
'name' => wc_attribute_taxonomy_name( 'varinfo' ), // set attribute name
'value' => '', // set attribute values
'position' => 1,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 1
)
);
update_post_meta($post_id, '_product_attributes', $product_attributes);