Add Attribute to WooCommerce Product Programmatically

Add Attribute to WooCommerce Product Programmatically

Add unique attribute to Product

Suppose you want to add an attribute a green color to all those product which does not have color attribute.
So to do this will first have to get all the products then we have to check it has color attribute or not if not then store the product ID in and array then we have to add the attribute to those products

$attribute_name = 'pa_color'; //slug of the attribute(taxonomy) with prefix 'pa_'
$attribute_value = 'green'; //slug of the attribute value (term)

$product_ids = []; //array to hold Product ID(s)
//getting all simple products
$args = ['post_type' => 'product', 'posts_per_page' => -1];
$loop = new WP_Query($args);

while ($loop->have_posts()) : $loop->the_post();

    global $product;
    $flag = 0;
    //get product attributes
    $attributes = $product->get_attributes();

    if (empty($attributes)) {
        $product_ids[] = get_the_ID(); //product which does not have any attributes
    }
    //if it has product attributes
    else {
        //looping throught all the prodct attribute
        foreach ($attributes as $attribute) :
            echo $attribute['name'];

            if ($attribute['is_taxonomy']) {
                if ($attribute['name'] == $attribute_name) {
                    $flag = 1;
                }
                //to get the value attribute
                //$values = wc_get_product_terms($product->id, $attribute['name'], ['fields' => 'names']);
            }
        endforeach;
        if ($flag != 1) {
            $product_ids[] = get_the_ID(); //product which does not have the given attributes
        }
    }
endwhile;

wp_reset_query();

$product_ids holds all the Product ID which does not has color attribute, so now we have to loop it and set the color attribute.

foreach ($product_ids as $product_id) {
    //Appending term to the object/product.
    $term_taxonomy_ids = wp_set_object_terms($product_id, $attribute_value, $attribute_name, true);
    $data = array(
        $attribute_name => array(
            'name' => $attribute_name,
            'value' => '',
            'is_visible' => '1',
            'is_variation' => '0',
            'is_taxonomy' => '1'
        )
    );
    //First getting the Post Meta
    $_product_attributes = get_post_meta($product_id, '_product_attributes', TRUE);
    //Updating the Post Meta
    update_post_meta($product_id, '_product_attributes', array_merge($_product_attributes, $data));
}

Let me know in the comment if you has any easier way to do this or if I have miss any important point in this.

Share this Post:

About Raunak Gupta

I'm an expert Laravel, WooCommerce, WordPress theme and plugin developer, over the time I have developed several sites and applications. I like to help the upcoming developer, So I share my experience through this blog.  

Related Posts:

7 thoughts on “Add Attribute to WooCommerce Product Programmatically

  1. Stoviak44828@hotmail.com'

    Did this work with WooCommerce 3x?

    Reply
  2. Mano85965@hotmail.com'

    Howdy! Do you use Twitter? I’d like to follow you if that would be okay. I’m undoubtedly enjoying your blog and look forward to new updates.

    Reply
  3. xomhjnskln@gmail.com'

    Keep this going please, great job!

    Reply
  4. Pignataro62466@hotmail.com'

    Now I know where I made the mistake, I updated the term table by wp_set_object_terms() but forget to update _product_attributes meta field so it was not rendering in front end correctly.

    Reply
  5. whoiswoowoo@gmail.com'

    WooCommerce really awesome plugin and so as this article 🙂 (y)

    Reply
  6. florianporteus@gmail.com'

    Oh my goodness! Awesome article dude! Thank you, However I am going through troubles with your RSS.
    I don’t know why I can’t join it. Is there anybody having similar RSS issues?

    Anybody who knows the answer can you kindly respond? Thanx!!

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *


*