Skip to content

Instantly share code, notes, and snippets.

@cristian0
Last active September 15, 2017 07:20
Show Gist options
  • Save cristian0/2b8763056b00de388349563f011b868a to your computer and use it in GitHub Desktop.
Save cristian0/2b8763056b00de388349563f011b868a to your computer and use it in GitHub Desktop.
Magento update multiple attribute product cases
// Case 1
$product=Mage::getModel('catalog/product')->load($id);
$product->setSpecialFromDate('2010-10-28');
// below code use for time format
$product->setSpecialFromDateIsFormated(true);
$product->getResource()->saveAttribute($product, 'special_from_date');
// Using Product Collection:
$productIds = array(1,3,2);
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('entity_id', array('in' => $productIds));
foreach($products as $product)
{
$product->setSpecialFromDate('2010-10-28');
// below code use for time format
$product->setSpecialFromDateIsFormated(true);
$product->getResource()->saveAttribute($product, 'special_from_date');
}
// - No reindex event registered
// - Probably no store management
// - Only one product each time (commit transaction every time)
// Case 2
Mage::getSingleton('catalog/resource_product_action')->updateAttributes($allPro‌​ductIds, $attrData, $storeId);
// - Multimple attributes
// - Store management
// - No reindex event registered
// Case 3
Mage::getSingleton('catalog/product_action')->updateAttributes($allPro‌​ductIds, $attrData, $storeId);
// - Same as case 2 + register mass action indexer event
// TIP: To reindex a sinble product:
$product = Mage::getModel('catalog/product')->load('29412'); // Product Id
$event = Mage::getSingleton('index/indexer')->logEvent(
$product,
$product->getResource()->getType(),
Mage_Index_Model_Event::TYPE_SAVE,
false
);
Mage::getSingleton('index/indexer')
->getProcessByCode('catalog_product_flat') // Adjust the indexer process code as needed
->setMode(Mage_Index_Model_Process::MODE_REAL_TIME)
->processEvent($event);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment