Skip to content

Instantly share code, notes, and snippets.

@molotovbliss
Created December 2, 2015 01:02
Show Gist options
  • Save molotovbliss/5ec3dab283f0d78bf97d to your computer and use it in GitHub Desktop.
Save molotovbliss/5ec3dab283f0d78bf97d to your computer and use it in GitHub Desktop.
Use Iterator to mass update products for performance optimization
// Get Collection
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku')
->addAttributeToSelect('publihser')
->addFieldToFilter(array(array('attribute'=>'publisher','eq'=>$publisher)));
function productUpdateCallback($args){
$product = Mage::getModel('catalog/product');
$product->setData($args['row']);
$productId = $product->getId();
$sku = 'yourSku';
// Updates a single attribute, much faster than calling a full product save
Mage::getSingleton('catalog/product_action')
->updateAttributes(array($productId), array('sku' => $sku), 0);
}
// Walk through collection, for large collections this is much faster than using foreach
Mage::getSingleton('core/resource_iterator')->walk($collection->getSelect(), array('productUpdateCallback'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment