有时候产品的属性为website,无法直接修改产品属性使其在不同的Website呈现不同的值.可以参考如下代码:
首先需要设admin store, Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
第二步,设置product的scope,
$website = Mage::app()->getWebsite($website);
$store = $website->getDefaultStore();
$store = $website->getDefaultStore();
$product->setStoreId($store->getStoreId());
参考下面的代码:
We must set the admin store first, and then set the scope of the specific product later ($product->setStoreId($store->getStoreId());) You may also notice lines like this: $product->setSpecialToDateIsFormated(true);. We need these to pass in a date as a string and tell Magento that it is indeed a date and should be stored that way.
public function updateSpecialPriceByWebsite($price, $specialFromDate, $specialToDate, $sku, $website) { try { $specialFromDate = date('Y-m-d', strtotime($specialFromDate)); $specialToDate = date('Y-m-d', strtotime($specialToDate)); $website = Mage::app()->getWebsite($website); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $store = $website->getDefaultStore(); $product = Mage::getModel('catalog/product'); $productId = $product->getIdBySku($sku); $product->load($productId); if ($product && $product->getId()) { $product->setStoreId($store->getStoreId()); $product->setSpecialFromDate($specialFromDate); $product->setSpecialFromDateIsFormated(true); $product->setSpecialToDate($specialToDate); $product->setSpecialToDateIsFormated(true); $product->setSpecialPrice(strval($price)); $product->save(); return true; } } catch (Exception $e) { Mage::logException($e); return false; } return true; }
转载至: http://www.demacmedia.com/ecommerce/mini-tutorial-updating-products-within-website-scope/