WooCommerce: issue setting attributes with wp_set_object_terms

*爱你&永不变心* 提交于 2019-12-11 10:44:29

问题


I need to set product's attributes via PHP code. According to other threads on StackOverflow, there's the method 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". And there is a product, ID 593, with no "hersteller" attribute set.

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. 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.

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? I don't know what to do anymore now. May anyone here can help me? Thanks!


回答1:


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

<?php wp_set_object_terms( $object_id, $terms, $taxonomy, $append ); ?>

$append (bool) (optional) If true, tags will be appended to the object. If false, tags will replace existing tags (Default: False)

So if you change the $append to true it may be works =)




回答2:


Based on the attributes output you provided, it seems that you are targeting the wrong taxonomy, I can see pa_herstellergarantiein the following output :

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. If the taxonomy doesn't exist it will throw an Invalid taxonomy error message.

If pa_herstellergarantie and pa_hersteller are 2 distinct taxonomies, then ensure that pa_hersteller exists before using wp_set_object_terms



来源:https://stackoverflow.com/questions/32957477/woocommerce-issue-setting-attributes-with-wp-set-object-terms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!