[英]WooCommerce: issue setting attributes with wp_set_object_terms
I need to set product's attributes via PHP code.我需要通过 PHP 代码设置产品的属性。 According to other threads on StackOverflow, there's the method wp_set_object_terms .
根据 StackOverflow 上的其他线程,有wp_set_object_terms方法。 Unfortunately it is not working as it is supposed to do in my case:
不幸的是,在我的情况下,它无法正常工作:
For example, there is an attribute named "Hersteller", slug "hersteller".例如,有一个名为“Hersteller”的属性,slug“hersteller”。 And there is a product, ID 593, with no "hersteller" attribute set.
还有一个产品,ID 593,没有设置“hersteller”属性。
I tried the following code to fill the attribute:我尝试了以下代码来填充属性:
wp_set_object_terms(593, 'Alpina', 'pa_hersteller' , false);
When I'm attempting to display the attribute on the product page, there is no output, so it seems that the wp_set_object_terms process hasn't been successful.当我尝试在产品页面上显示属性时,没有输出,因此 wp_set_object_terms 过程似乎没有成功。 Following code produces an empty output:
以下代码产生一个空输出:
$product->get_attribute('hersteller');
Furthermore, the attribute "hersteller" isn't even listed in the attribute list in the admin backend menue.此外,属性“hersteller”甚至没有列在后台管理菜单的属性列表中。
To debug the problem I've also tried the following code:为了调试问题,我还尝试了以下代码:
$attributes = get_post_meta( 593, '_product_attributes' );
print_r($attributes);
resulting in the following output:导致以下输出:
Array ( [0] => Array ( [pa_marken] => Array ( [name] => pa_marken [value] => [position] => 0 [is_visible] => 1 [is_variation] => 0 [is_taxonomy] => 1 ) [pa_kategorien] => Array ( [name] => pa_kategorien [value] => [position] => 1 [is_visible] => 1 [is_variation] => 0 [is_taxonomy] => 1 ) [pa_referenznummer] => Array ( [name] => pa_referenznummer [value] => [position] => 2 [is_visible] => 1 [is_variation] => 0 [is_taxonomy] => 1 ) [pa_herstellergarantie] => Array ( [name] => pa_herstellergarantie [value] => [position] => 3 [is_visible] => 1 [is_variation] => 0 [is_taxonomy] => 1 ) [pa_schlagwoerter] => Array ( [name] => pa_schlagwoerter [value] => [position] => 4 [is_visible] => 1 [is_variation] => 0 [is_taxonomy] => 1 ) [pa_lieferzeit] => Array ( [name] => pa_lieferzeit [value] => [position] => 5 [is_visible] => 1 [is_variation] => 0 [is_taxonomy] => 1 ) ) )
Am I using the wp_set_object_terms method essentially wrong?我使用 wp_set_object_terms 方法本质上是错误的吗? I don't know what to do anymore now.
我现在不知道该怎么办了。 May anyone here can help me?
这里有人可以帮助我吗? Thanks!
谢谢!
Try to set your code like this:尝试像这样设置你的代码:
wp_set_object_terms(593, 'Alpina', 'pa_hersteller' , true);
And try again.然后再试一次。
According to the function in WordPress Codex根据WordPress Codex中的功能
<?php wp_set_object_terms( $object_id, $terms, $taxonomy, $append ); ?>
$append (bool) (optional) If true, tags will be appended to the object.
$append (bool) (optional) 如果为真,标签将被附加到对象。 If false, tags will replace existing tags (Default: False)
如果为 false,标签将替换现有标签(默认值:False)
So if you change the $append to true it may be works =)因此,如果您将 $append 更改为 true,它可能会起作用 =)
Based on the attributes output you provided, it seems that you are targeting the wrong taxonomy, I can see pa_herstellergarantie
in the following output :根据您提供的属性输出,您的目标似乎是错误的分类法,我可以在以下输出中看到
pa_herstellergarantie
:
Array ( .....
[pa_herstellergarantie] => Array (
[name] => pa_herstellergarantie
[value] =>
[position] => 3
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1 )......
Change your code to:将您的代码更改为:
wp_set_object_terms(593, 'Alpina', 'pa_herstellergarantie' , false);
Also a note to say that wp_set_object_terms
will not create the taxonomy.还有一个说明
wp_set_object_terms
不会创建分类法。 If the taxonomy doesn't exist it will throw an Invalid taxonomy
error message.如果分类不存在,它将抛出
Invalid taxonomy
错误消息。
If pa_herstellergarantie
and pa_hersteller
are 2 distinct taxonomies, then ensure that pa_hersteller
exists before using wp_set_object_terms
如果
pa_herstellergarantie
和pa_hersteller
是 2 个不同的分类法,则在使用wp_set_object_terms
之前确保pa_hersteller
存在
To set attributes programatically you must update the post meta with the taxonomy's data after setting the terms, like this要以编程方式设置属性,您必须在设置条款后使用分类数据更新帖子元数据,如下所示
wp_set_object_terms(593, 'Alpina', 'pa_hersteller' , false);
$thedata = array('pa_hersteller' => array(
name' => 'pa_hersteller',
'value' => '',
'is_visible' => '0',
'is_taxonomy' => '1'
));
update_post_meta(get_the_ID(), '_product_attributes', $thedata);
Hope it helps :)希望能帮助到你 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.