Reputation: 687
I need to update description on more than 1000 products. I use this code to do this task. It works but the created_at and updated_at fields got messed up after running this code. Did I miss something?
$products = Mage::getModel('catalog/product')->getCollection()
->setStoreId(0)
->addAttributeToSelect('description')
->addAttributeToFilter('price', array('gt' => 10.00))
;
foreach ($products as $product) {
$sku = $product->getSku();
$desc = $product->getDescription();
$descUp = $desc.'<p>New Text Here</p>';
$update = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
$update->setStoreId(0);
$update->setDescription($descUp);
try {
$update->save();
echo "<pre>".$product->getSku(). " updated</pre>";
} catch (Exception $e) {
$e->getMessage();
}
}
Upvotes: 0
Views: 4668
Reputation: 745
Have you looked at Magmi, http://sourceforge.net/projects/magmi/ ? You can manage all the details in a spreadsheet and load and reload and overwrite data as many times as you want.
Upvotes: 0
Reputation: 11
Better way to do it:
$model = Mage::getSingleton('catalog/product_action')
->getResource();
$products = Mage::getModel('catalog/product')->getCollection()
->setStoreId(0)
->addAttributeToSelect('description')
->addAttributeToFilter('price', array('gt' => 10.00));
foreach ($products as $product) {
$attrData = array(); // Array of attributes to mass update for the product
$attrData['description'] = $product->getDescription() . '<p>New Text Here</p>';
//$attrData['other_attr'] = 'value';
// Mass update action, 0 - Store ID
$model->updateAttributes(array($product->getId()), $attrData, 0);
}
Upvotes: 1