Magento – the effect of Mage::getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID)

ce-1.7.0.2magento-1.7scriptstores

I've found the following line of code used in some of our one-off scripts, and have also found it used in various unofficial (i.e. not entirely trustworthy) websites:

Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

In our usage it's called during the bootstrapping of various .php scripts; things like one-time data-load scripts.

In debugging I see that it change's the current store's Id to zero; however I don't understand the reason or need to do so.

What purpose would this serve, and when (if at all) would it be correct to be used?

Best Answer

There are some methods that require an admin level to perform properly as they will be applied across all store views.

By default the store ID will always be set as 1 if not specified, causing some methods to fail or odd behaviour.

An example would be needing to apply a scripted change across all store views, for example product Images or such.

Related Topic