1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
  • This updates the products to the same attribute value; suppose you would have to update all products to different/unique values - how would this work? – snh_nl Jun 17 '17 at 15:32

2 Answers2

2

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

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Asrar Malik
  • 125
  • 3
  • 8
-3

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

rfeni
  • 17
  • 7