I have two stores in Magento, every product is added to the "global" store (id=0) so that it's available in both stores, the first store reflects the product in the global store, no attribute is added at store level, the second one should do the same except for the price that is different. I've created a script to synchronize products prices and description from a CSV:
//Load global product
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$prod = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
//Save global data for product
if ($prod->getDescription() != $desc) $prod->setDescription($desc);
if ($prod->getPrice() != $price1) $prod->setPrice($price1);
$prod->save();
//Reload product in store 2
$prod2 = Mage::getModel('catalog/product')->setStoreId(2)->load($prod->getId());
//Save data for store 2
if ($prod2->getPrice() != $price2) $prod2->setPrice($price2);
$prod2->save();
The problem here is that when i save the price for the store 2 it saves every other attribute at store level, this means that for example for the product's description the "use default value" flag in unchecked in administration and if i want to change the description on both stores i have to do that in both global and store 2. What am i doing wrong? Is there a way to do this?
You can use singleton:
Mage::getSingleton('catalog/product_action')->updateAttributes(
array($prod->getId()),
array('price'=>$price2),
Mage_Core_Model_App::ADMIN_STORE_ID
);