Update Single Attribute for Large Product Collection in Magento

In my latest task, i was to update an attribute, for all products.

the first thing came to my mind was the following (like any other Magento Noob 🙂 ):

$product_collection=Mage::getModel("catalog/product")->getCollection();
$product_collection -> addAttributeToSelect("attribute_code");
foreach($product_collection as $product) {
$product->setAttributeCode("something pretty");
$product->save();
}

But this has a drawback, of consuming lot of resources and time, if you have a large product set.

In order to find an alternative solution, I came across this awesome solution, in this blog

Mage::getSingleton('catalog/product_action')->updateAttributes($array_product, array('attribute_code' => "something pretty"), 0);

$array_product => array of product ids, which could be easily fetched using Mage::getModel("catalog/product")->getCollection()->getAllIds()

this method is way faster and cheaper(in terms of resource consumption), all credits to the author.

Advertisements

One thought on “Update Single Attribute for Large Product Collection in Magento

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s