Programmatically update product price per website in Magento

If you want to programmatically update product prices per website (this means that you have many websites and have set a website scope for price in the configuration), then you can use this script:

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

Important note: I haven’t developed this, but I think it’s interesting to note it down. Thanks to Knowledge Craving from Stackoverflow.

1 comment

    • Trainee on February 11, 2013 at 1:43 pm

    Reply

    The Changes Happened Only Front End Not Back-ends

Leave a Reply

Your email address will not be published.