创建WooCommerce产品变体会添加一个空的属性值 [英] Creating WooCommerce product variation adds an empty attribute value
问题描述
向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
}
第一项从何而来?我似乎无法清除它。
所以事实证明问题出在针对该属性的实际设置上主要的父产品,我通过添加 wc_attribute_taxonomy_name('varinfo')=>
(下面的第2行)来传递未命名数组,这样可以正确保存数据并删除我拥有的空数组。 b数组(
'name'=> wc_attribute_taxonomy_name('varinfo'),//设置属性名称
'value'=>'',//设置属性值
'position' => 1,
'is_visible'=> 1,
'is_variation'=> 1,
'is_taxonomy'= >> 1
)
) ;
update_post_meta($ post_id,‘_product_attributes’,$ product_attributes);
When adding variations to a WooCommerce product it is adding in a blank attribute. Meaning that when I try to add in my own attribute it gets appended to an existing array.
Example code I am running:
$product_variation = wc_get_product( $variation_id );
$product_variation->set_attributes(array('attribute_pa_varinfo' => 'blue'));
$product_variation->save();
Then when I var_dump($product_variation);
I get the following:
["attributes"]=>
array(2) {
[0]=>
string(0) ""
["pa_varinfo"]=>
string(4) "5034"
}
So when I view the product in WooCommerce admin all my variations are there but the attribute is stuck at "any option" for all of them.
The weird thing is when I then "update" the product from wp-admin all of the variations then get the correct attribute selected.
Has anyone encountered this before or got any ideas of what I can do?
As another example if I run the following:
$product_variation = wc_get_product( $variation_id );
$product_variation->set_attributes( array ( 'simon' => 'confused' ) );
$product_variation->save();
var_dump($product_variation->get_attributes());
This returns:
array(2) {
[0]=> string(0) ""
["simon"]=> string(8) "confused"
}
Where does the first item come from? I can't seem to clear it.
So it turns out the problem was with the actual setting up of the attribute against the main parent product, I was passing through an unnamed array, by adding wc_attribute_taxonomy_name('varinfo') =>
(line 2 below) this correctly saves the data and removes the blank array I had.
$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);
这篇关于创建WooCommerce产品变体会添加一个空的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!