Home » Magento

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

9 December 2009 1,251 views Popularity: 3% Share/Bookmark

email

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:

  1. Magento: Get parent id of simple product associated to configurable product
  2. Magento: How to get all associated children product of a configurable product?
  3. Magento: Load store specific product
  4. Magento: How to filter product collection using 2 or more category filters?
  5. Magento: How to get product stock quantity & other stock information?
  6. Magento: Get category name and url from product
  7. Magento: Sort latest product by ‘created date’ and ‘new from date’
  8. Magento: Get manufacturer name and id from product
  9. Magento: Get sub categories and product count
  10. Magento: Get Product Collection by Type
  • http://www.fastfireplaces.com Kevin Connor

    Thanks! This was quite helpful.

  • Paolo

    u may need to check the number in setAttributeSetId(?)

  • Alex Kochetyga

    Thanks for this solution. Do you know how this can affect on other code related to product that is ran after switching to the admin store?

  • http://alexandersitedesign.com Matt Alexander
  • Pleth

    Thank you !!

  • http://www.homobit.com Simone

    Thank you for this helpful post!

    I was wondering why

    - inserting products from frontend: no problem.
    - updating products from frontend: problem.

  • jpk

    thanks for this…
    it worked like a charm :)