我遇到了一个应该按网站更新价格的模块.
在我的模块的顶部,我有:
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Run Code Online (Sandbox Code Playgroud)
这应该意味着当我调用模块(模块有一个前端)时,我可以更新价格等'admin'.然而,这并没有发生.目前我已经做了一些绝望的努力让它工作 - 看看数字格式是否绊倒或索引没有重新索引,缓存问题和其他任何事情.
以下代码段实际上生成了正确的数字,但它只是没有保存到数据库中.有任何想法吗?
$product = Mage::getModel('catalog/product')->
setStoreId($storeId)->setWebsiteId($websites[$store])->
loadByAttribute('sku',$price['SKU']);
$oldprice=(float)str_replace(",","",$product->getPrice());
if($newprice!=$oldprice) {
Mage::log($product->getPrice());
$product->setPrice(number_format($newprice,4,".",""));
Mage::log($product->getPrice());
$product->getResource()->saveAttribute($product, 'price');
$product->save();
Mage::log($product->getPrice());
unset($product);
}
Run Code Online (Sandbox Code Playgroud)
假设您使用模块类中的方法更新产品价格,并使用以下示例代码: -
public function updateProductPrices ($sku, $newPrice) {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$websites = Mage::app()->getWebsites();
$product = Mage::getModel('catalog/product')
$productId = $product->getIdBySku($sku);
foreach ($websites as $_eachWebsite) {
$_websiteId = $_eachWebsite->getWebsiteId();
$websiteObj = new Mage_Core_Model_Website();
$websiteObj->load($_websiteId);
$storeIds = $websiteObj->getStoreIds();
if (count($storeIds)) {
foreach ($storeIds as $_eachStoreId) {
$product->setStoreId($_eachStoreId)
->load($productId);
$oldPrice = $product->getPrice();
if ($oldPrice != $newPrice) {
$product->setPrice($newPrice);
$product->save();
}
}
}
unset($storeIds, $websiteObj, $_websiteId);
}
unset($product);
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.