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:
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();
Related posts:
- Magento: Get parent id of simple product associated to configurable product
- Magento: How to get all associated children product of a configurable product?
- Magento: Load store specific product
- Magento: How to filter product collection using 2 or more category filters?
- Magento: How to get product stock quantity & other stock information?
- Magento: Get category name and url from product
- Magento: Sort latest product by ‘created date’ and ‘new from date’
- Magento: Get manufacturer name and id from product
- Magento: Get sub categories and product count
- Magento: Get Product Collection by Type
