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.
Did this work with WooCommerce 3x?
Hi Marshal,
Yes it work with WooCommerce 3.x as well.
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.
Thanks, ya sure you can defiantly follows us on Twitter (@webhat14)
Keep this going please, great job!
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.WooCommerce really awesome plugin and so as this article 🙂 (y)
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!!
Muchos Gracias for your blog article. Really Cool.
Hi, can you share me some of the resource on woocommerce database schema? I want to understand it in details.
Hi,
Yes, recently I have written an article on this topic, you can check this series. Understanding WooCommerce Database Structure: A Deep Dive Series for Developers
Im thankful for the blog article.Really thank you! Really Great.