Mini Tutorial: The Fix for an Unintended Price Change when Updating a Product

I was trying to manipulate the price of a store when a back-end controller, when something weird happened.  The default price got modified too when the product saves!

The code was very straight forward.  It was something like this…

$product = Mage::getModel(‘catalog/product’)->setStore(3)->load(12);

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

It ended up changing default value.  I had no idea what the heck was happening.  I tried to debug it, but there wasn’t much progress on that.  Due to a time constraint,  I ended up finding an alternative solution,  using the product mass update function:

Mage::getSingleton(‘catalog/product_action’)->updateAttributes(array(20),array(‘price’=>5.00), 3);

Hurray!… It changed the value without messing up the default one.  The performance also increased by using this function.  I didn’t have a proper test, but the load time after  clicking the save button, to save 300 product price changes was shorten by at least 1/5.  I think saving an attribute is less resource intensive than saving whole product.  That was an interesting discovery!