Magento: Product Edit Warning: Invalid argument supplied for foreach()

Scenario:

While editing product programatically from frontend. I was trying to change the status of the product with the following code. I was trying to disable the product. Status value 2 = Disabled.


// Trying to disable the product
// $product->getId() = PRODUCT ID
Mage::getModel('catalog/product')->load($product->getId())->setStatus(2)->save();

Problem:

Product could not be edited programatically from frontend. The following error message is shown:

Warning: Invalid argument supplied for foreach()  in /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 937

Solution:

It seems that product updates are only made on the admin side and you have to change the store view while editing product from frontend.

Write the following before you edit product:


Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Hence, my new code will be :


Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::getModel('catalog/product')->load($item->getProductId())->setStatus(2)->save();

Thanks.