Update All Product Attributes programmatically

2.9k views Asked by At

How can I set the value of a given attribute to the same value for all products (efficiently)?

By efficient I mean in one transaction, not having to loop through the entire product collection.

In the past I've used Mage_Catalog_Model_Product_Action for bulk updates on products, and it runs pretty fast

Mage::getSingleton('catalog/product_action')
    ->updateAttributes($productIds, array('some_attribute' => 'some_value'), 0)

But it requires you specify which product ids you're updating, creating a huge WHERE entity_id IN(...) clause in the MySQL statement. Is there a way to do this for everything?

2

There are 2 answers

0
Asrar Malik On

I had same problem before, when I added 11096 product(downloadable products) in my store then client told me to add new attributes in product so i create 1 attribute (Type is Yes/No) and set to attribute set. Now my problem is how can iIedit all product and set that attribute yes or not. If I don't set then value is null so I wrote few line code.

Please check this code may be it'll helpful to you.

$ProductId = Mage::getResourceModel('catalog/product_collection') -
addAttributeToFilter('type_id', Mage_Downloadable_Model_Product_Type::TYPE_DOWNLOADABLE) -
getAllIds(); //Now create an array of attribute_code => values

$attributeData = array("my_attribute_code" =>"my_attribute_value");

//Set the store to affect. I used admin to change all default values

$storeId = 0;

//Now Update the attribute for the given products.

Mage::getSingleton('catalog/product_action') ->updateAttributes($ProductId, $attributeData, $storeId);

It was work for me.I hope it'll work for you

1
rfeni On

Amasty "Mass Product Actions" will allow you to edit product attributes in a variety of ways. This is what we use and it's going to be a life saver once we get the Configurable Products pricing tier working properly.

http://amasty.com/mass-product-actions.html