0

I found basically an easy script here to update my prices in all store views. Just tried to test it, it echos correctly, runs without error but doesn't save the new prices - tried with and without setStoreId.

    require_once MAGENTO . '/app/Mage.php';
    Mage::app();

$sku = '123';
$newPrice = '1.64';

updateProductPrices ( $sku, $newPrice );

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

                    $product->setStoreId($_eachStoreId) -> setPrice($newPrice) ->save() ;

echo "<br/>Store ID: " . $_eachStoreId;                
echo "<br/>Alt: " . $oldPrice;
echo "<br/>Neu: " . $newPrice;
                }
            }
        }

        unset($storeIds, $websiteObj, $_websiteId);
    }

    unset($product);
}

Does anyone see a problem there?

  • Chris i had a similar issue. Do you have a csv with prices and sku´s then I have a solution for you which works nicely – Klettseb Mar 24 '15 at 16:21
  • I usually got two CSVs: One German, one English, both completely filled with SKU, ShopID, Prices, etc. Maybe I just need to omit the prices in the csv? – Chris Mar 26 '15 at 1:57
1

Magento can have the prices either on website or on global level. Make sure, your prices are set to website level:

System > Config > Catalog > Catalog > Price > Catalog Price Scope

Then you are able to set them per website (still not per store!)

Because you set the store in the beginning to ADMIN, you don't have a problem with

public function setOrigData($key=null, $data=null)
{
    if (Mage::app()->getStore()->isAdmin()) {
        return parent::setOrigData($key, $data);
    }

    return $this;
}

As https://magento.stackexchange.com/a/13409/217 suggests you only need one StoreID, because the price is website based.

To be honest, I don't see a problem with your script. I hope something of this helps.

How do you test that the price is updated? If you use flat tables and they are not updated you get wrong prices. So maybe better check the database catalog_product_entity_decimal for changes.

Beside this, there is no need to reload the website:

    foreach ($websites as $_eachWebsite) {
/*
        $_websiteId = $_eachWebsite->getWebsiteId();

        $websiteObj = new Mage_Core_Model_Website();
        $websiteObj->load($_websiteId);
*/
 // you have all websites in $_eachWebsite, no need to reload it.
$websiteObj = $_eachWebsite;
| improve this answer | |
  • Thanks for your reply. My prices are set to global (and basically this is what I want as well since I only have one price for all websites). So it might be better to delete the prices from the website scope? (Don't even know how they get there if the settings in the admin don't permit that) – Chris Mar 2 '15 at 9:57
  • There shouldn't be saved any prices on website scope if it isn't allowed – Fabian Blechschmidt Mar 2 '15 at 14:18
  • Maybe a lack of checks in the Magento code? Could be that the Magmi Importer injects data somehow without Magento tests here? What do you suggest? Can I delete the values somehow, rather than updating them (since I don't want to have them or should I open a new question for this?) – Chris Mar 3 '15 at 1:14
  • No idea. Haven't had the problem yet and yes. Magmi is writing directly to database, that is the reason why it is so fast and I have no clue whether they check anything. – Fabian Blechschmidt Mar 3 '15 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.