如何防止Magento在以编程方式更新产品时覆盖其他网站/商店的属性值
我已经实现了自定义Magento模块,该模块通过外部服务的数据循环,并在Magento多语言,多商店网站中更新价格,重量,名称和其他一些产品属性。
我的解决方案非常简单(在Cron每天调用的模型中),如下所示:
/* 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();
我每天在cron中运行这个,每个商店都单独运行。它通常可以正常工作,仅更改每个商店的每个产品的价格,但有时会发生一些奇怪的事情(例如每2个月一次) - 除了价格之外,所有其他属性都会从商店X覆盖到当前商店。这意味着我所有受影响产品的英文产品描述都变成了德语(例如)。$storeId
我不知道这是怎么发生的,因为每次我调试它都能正常工作,只更改当前范围内的价格,这是我明确设置的,但保留了所有其他产品属性不变。它似乎从商店X加载所有产品数据,设置价格,然后存储所有这些值以存储我在通过调用保存产品之前设置的值。$extistingProduct->setStoreId($storeId)
在这种情况下,所有属性都会从同一商店被覆盖(例如,所有英语文本都变成德语,但在其他情况下,所有属性都将变成西班牙语 - 它们都来自一个随机的商店)。
有没有人知道这怎么可能发生?我做错了什么?