I have implemented custom Magento module that loops trough data from external service and updates price, weight, name and some other product attributes in Magento multi-language, multi-store website.
My solution is pretty straight forward (inside my Model invoked by Cron every day), as following:
/* THIS IS CODE SNIPPET INSIDE FOREACH LOOP */
$storeId = (string)$jobConfig->store; //cron for each store
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$extistingProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$extistingProduct->setPrice($newPrice); //update price
//some code here dealing with Associated products of Configurable product probably not relevant
//...
$extistingProduct->setCanSaveConfigurableAttributes(true);
$extistingProduct->setCanSaveCustomOptions(true);
$extistingProduct->setConfigurableAttributesData($configurableAttributesData);
// This tells Magento to associate the given simple products to this configurable product..
$extistingProduct->setConfigurableProductsData($configurableProductsData);
$extistingProduct->setStoreId($storeId);
$extistingProduct->save();
I have this in cron running daily, separately for each Store. It usually works correctly, only changing the price of each product per Store, but sometimes a weird things happens (like once every 2 months) - all other attributes besides price get overwritten from Store X to current store $storeId
. Meaning that all my English product description become German (for ex.) for all affected products.
I have no clue how could this happen, since every time I debug it is working correctly, only changing the price in current scope, which I explicitly set, but leaving all other product attributes intact. It seems like it loads all product data from Store X, sets price and then stores all those values to store which I set before saving product by calling $extistingProduct->setStoreId($storeId)
.
In situations when this happens, all attributes get overwritten from same Store (for example all English texts become German, but in other case all will become Spanish - they are all from one random Store).
Does anybody have a clue how could this possibly happen? What am I doing wrong?
$extistingProduct
. If you want some help, then pasting the full foreach loop, at least, would help. – β.εηοιτ.βε