Magento - 模块没有按网站更新价格

时间:2011-06-27 14:01:23

标签: magento

3 个答案:

答案 0 :(得分:8)

假设您使用模块类中的方法更新产品价格,并使用以下示例代码: -

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);
}

希望它有所帮助。

答案 1 :(得分:0)

尝试:

$product->setPrice($newprice)->save();

答案 2 :(得分:0)

我有类似的问题,我认为知识渴望的答案中有一个错误。实际上,代码不会在每个网站上更新,而是为每个网站更新一个独特的价格

我所说的错误实际上发生在我身上的类似代码,并且其他产品属性将从默认情况下取消链接(至少,它发生在我身上)。

以下是基于Knowledge Craving解决方案的建议,我刚刚修改了一下

public function updateProductPrices ($sku, $newPrice, $websiteId) {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$product = Mage::getModel('catalog/product')
$productId = $product->getIdBySku($sku);


    $websiteObj = new Mage_Core_Model_Website();
    $websiteObj->load($websiteId);

    $storeIds = $websiteObj->getStoreIds();

    if (count($storeIds)) {
        foreach ($storeIds as $_eachStoreId) { //Does it need to do for each storeview, price being anyway website scope?
            $product->load($productId);

            $oldPrice = $product->getPrice();

            if ($oldPrice != $newPrice) { //actually, this if would not be necessary as you anyway want to update, right?
                $product->setStoreId($_eachStoreId)
                    ->setPrice($newPrice)
                    ->save();

            }
        }
    }

 unset($productId, $storeIds, $websiteObj, $product);
 }