Last active
September 15, 2017 07:20
-
-
Save cristian0/2b8763056b00de388349563f011b868a to your computer and use it in GitHub Desktop.
Magento update multiple attribute product cases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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($allProductIds, $attrData, $storeId); | |
// - Multimple attributes | |
// - Store management | |
// - No reindex event registered | |
// Case 3 | |
Mage::getSingleton('catalog/product_action')->updateAttributes($allProductIds, $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