最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - add_post_meta add latest data from meta_value array

programmeradmin0浏览0评论

I want to add list of color to a product attribute with following code:

function wcproduct_set_attributes($post_id, $_attributes) {
        $i = 0;
            foreach ($_attributes as $name => $value) {
                wp_set_object_terms($post_id, $value, $name);
                $product_attributes[$i] = array (
                    'name' => $name, // set attribute name
                    'value' => $value, // set attribute value
                    'is_visible' => 0,
                    'is_variation' => 1,
                    'is_taxonomy' => 1
                );
            }
            $i++;

        update_post_meta($post_id, '_product_attributes', $product_attributes);
    };

    wcproduct_set_attributes($_product_id_data, $my_product_attributes);

$product_attributes is an array of colors and $name is pa_color and $value is color name. this function works but it add last color to pa_color.

I want to add list of color to a product attribute with following code:

function wcproduct_set_attributes($post_id, $_attributes) {
        $i = 0;
            foreach ($_attributes as $name => $value) {
                wp_set_object_terms($post_id, $value, $name);
                $product_attributes[$i] = array (
                    'name' => $name, // set attribute name
                    'value' => $value, // set attribute value
                    'is_visible' => 0,
                    'is_variation' => 1,
                    'is_taxonomy' => 1
                );
            }
            $i++;

        update_post_meta($post_id, '_product_attributes', $product_attributes);
    };

    wcproduct_set_attributes($_product_id_data, $my_product_attributes);

$product_attributes is an array of colors and $name is pa_color and $value is color name. this function works but it add last color to pa_color.

Share Improve this question asked Jun 1, 2020 at 8:35 Web DesignWeb Design 32 bronze badges 5
  • Welcome to WPSE! I've read your question but there are a few things that don't make sense, e.g. you mention it added last color to pa_color, but you've not mentioned pa_color before so I don't know what you're referring to. There are a few other things that don't make sense, such as where my_product_attributes comes from, or how you're testing this. Keep in mind that WooCommerce is a 3rd party plugin, and 3rd party plugin dev support is off topic here and not in this stacks scope – Tom J Nowell Commented Jun 1, 2020 at 8:52
  • $my_product_attributes is an array of pa_color and color names. I want add multiple terms to a taxonomy. i.e. red, green and blue to pa_color simultaneously. $product_attributes is an array of for example 'pa_color' => blue, ... but it add last term to that taxonomy – Web Design Commented Jun 1, 2020 at 9:06
  • @TomJNowell you can see following link which ask about woocommerce!: wordpress.stackexchange/questions/337316/… – Web Design Commented Jun 1, 2020 at 9:13
  • That does not mean WooCommerce is on topic, it means a question slipped through, thankyou for bringing it to me so I can close it. The rules are clear, if you find deviations then you've found cases where the rules were broken that need addressing. Vote to close those questions, or flag them. Nobody here is paid and there is no staff working 9-5 to look at every single Q, so things get missed :( – Tom J Nowell Commented Jun 1, 2020 at 9:15
  • @TomJNowell I keep in mind the rules. – Web Design Commented Jun 1, 2020 at 9:18
Add a comment  | 

1 Answer 1

Reset to default 0

If we look at the official documentation, this is how wp_set_object_terms is used:

wp_set_object_terms( int $object_id, string|int|array $terms, string $taxonomy, bool $append = false )

Notice the final parameter that isn't being used in your code, it has a default value of false:

bool $append = false )

So everytime your loop runs, it wipes the slate clean and sets the terms rather than adding them. You need to pass the final parameter.

Also consider deliberatly calling this with a blank array before your loop so that terms can be unset. Alternatively, add them to an array and do a single call after the loop

Make sure in these situations to always read the official API docs at https://developer.wordpress

发布评论

评论列表(0)

  1. 暂无评论