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.

  • 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 Jun 1 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 Jun 1 at 9:06
  • @TomJNowell you can see following link which ask about woocommerce!: wordpress.stackexchange.com/questions/337316/… – Web Design Jun 1 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 Jun 1 at 9:15
  • @TomJNowell I keep in mind the rules. – Web Design Jun 1 at 9:18
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.org

| improve this answer | |
  • Thank you A LOOOOOOOT :) It works – Web Design Jun 1 at 9:17
  • I made an edit to offer an alternative approach as well involving an array, and to catch an edge case bug – Tom J Nowell Jun 1 at 9:18
  • I didn't attention to last part of wp_set_object_terms. Thank you. – Web Design Jun 1 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.