3

When saving products in magento through custom scripts I have always had to add the following:

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

Otherwise you get a wierd error from eav/config model about an invalid argument foreach.

Can anyone explain why products can only be saved under the admin store id? and if this code i have been using is in fact the best way to deal with it?

5

When saving a product the call stack reaches Mage_Eav_Model_Entity_Abstract::_collectSaveData(). In this method you loop through the _origData member of the model and get the attributes that are different and save only those ones.
The _origData member is populated when loading a model by calling setOrigData().
So far so good. The difference is that for the product model the method setOrigData() looks like this (for performance reasons I assume):

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

    return $this;
}

This means that if you are not in the admin environment _origData will be null.
In this case _collectSaveData() tries to loop through null, thus the error you are getting.

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.