Add Attribute to WooCommerce Product Programmatically

Are you looking for how to add product attribute(s) through in WooCommerce programmatically, then here is the solution for all the above query.

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

<?php
$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.

<?php

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.

Raunak Gupta

Raunak Gupta

I'm Raunak Gupta, a seasoned software developer with over 9 years of experience in a wide range of programming languages, frameworks, and tools. I started my journey as a WordPress & CakePHP developer in 2014, diving deep into the world of OOPs, Request handling, and SEO. Along the way, I crafted numerous dazzling WooCommerce stores, tamed payment gateways, optimized for full filament functionality, and achieved ultra-low latency for lightning-fast load times. My expertise extends to BI tools, website builders, DevOps, and team leadership. I like to help upcoming developers, so I share my experience through this blog and by assisting fellow developers on Stack Overflow, where I've earned a stellar reputation with over 10k+ points of recognition.

Articles: 29

12 Comments

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

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

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

Leave a Reply to Raunak GuptaCancel Reply

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