Magento get current store information

Posted by Damodar Bashyal on July 08, 2013

 

Here are some of the codes that will help retrieving information about the current or selected store.

Load store object with below code. This will load current store by default unless you pass store id.

$store = Mage::app()->getStore(); // see below for magento core function

Now if you getData() from $store you will get these information about the store:

Array
(
    [store_id] => 0
    [code] => admin
    [website_id] => 0
    [group_id] => 0
    [name] => Admin
    [sort_order] => 0
    [is_active] => 1
)

Now we know how to get store Id, code etc:

Get Store ID:

Mage::app()->getStore()->getStoreId();
or,
Mage::app()->getStore()->getData('store_id');

Get Store code:

Mage::app()->getStore()->getCode();
or,
Mage::app()->getStore()->getData('code');

Get Website ID:

Mage::app()->getStore()->getWebsiteId();
or,
Mage::app()->getStore()->getData('website_id');

Get Group ID:

Mage::app()->getStore()->getGroupId();
or,
Mage::app()->getStore()->getData('group_id');

Get Store Name:

Mage::app()->getStore()->getName();
or,
Mage::app()->getStore()->getData('name');

Get sort order:

Mage::app()->getStore()->getSortOrder();
or,
Mage::app()->getStore()->getData('sort_order');

Check if store is active or not:

Mage::app()->getStore()->getIsActive();
or,
Mage::app()->getStore()->getData('is_active');

Today I was working with new tab in product manager where client could assign different file attachment per store. So I needed to know which store is currently selected from the store switcher in the product manager. Below code gave me selected store id:

$storeId = Mage::app()->getRequest()->getParam('store', 0);

Source: app/code/core/Mage/Core/Model/App.php

    /**
     * Retrieve application store object
     *
     * @param null|string|bool|int|Mage_Core_Model_Store $id
     * @return Mage_Core_Model_Store
     * @throws Mage_Core_Model_Store_Exception
     */
    public function getStore($id = null)
    {
        if (!Mage::isInstalled() || $this->getUpdateMode()) {
            return $this->_getDefaultStore();
        }

        if ($id === true && $this->isSingleStoreMode()) {
            return $this->_store;
        }

        if (!isset($id) || ''===$id || $id === true) {
            $id = $this->_currentStore;
        }
        if ($id instanceof Mage_Core_Model_Store) {
            return $id;
        }
        if (!isset($id)) {
            $this->throwStoreException();
        }

        if (empty($this->_stores[$id])) {
            $store = Mage::getModel('core/store');
            /* @var $store Mage_Core_Model_Store */
            if (is_numeric($id)) {
                $store->load($id);
            } elseif (is_string($id)) {
                $store->load($id, 'code');
            }

            if (!$store->getCode()) {
                $this->throwStoreException();
            }
            $this->_stores[$store->getStoreId()] = $store;
            $this->_stores[$store->getCode()] = $store;
        }
        return $this->_stores[$id];
    }

Mage_Core_Model_Store Object loaded using Mage::app()->getStore() will look like this:

Mage_Core_Model_Store Object
(
    [_cacheTag:protected] => 1
    [_eventPrefix:protected] => store
    [_eventObject:protected] => store
    [_priceFilter:protected] => 
    [_website:protected] => Mage_Core_Model_Website Object
        (
            [_cacheTag:protected] => 1
            [_eventPrefix:protected] => website
            [_eventObject:protected] => website
            [_configCache:protected] => Array
                (
                )

            [_groups:protected] => Array
                (
                    [0] => Mage_Core_Model_Store_Group Object
                        (
                            [_cacheTag:protected] => 1
                            [_eventPrefix:protected] => store_group
                            [_eventObject:protected] => store_group
                            [_stores:protected] => Array
                                (
                                    [0] => Mage_Core_Model_Store Object
 *RECURSION*
                                )

                            [_storeIds:protected] => Array
                                (
                                    [0] => 0
                                )

                            [_storeCodes:protected] => Array
                                (
                                    [0] => admin
                                )

                            [_storesCount:protected] => 1
                            [_defaultStore:protected] => Mage_Core_Model_Store Object
 *RECURSION*
                            [_website:protected] => Mage_Core_Model_Website Object
 *RECURSION*
                            [_isReadOnly:Mage_Core_Model_Store_Group:private] => 
                            [_resourceName:protected] => core/store_group
                            [_resource:protected] => 
                            [_resourceCollectionName:protected] => core/store_group_collection
                            [_dataSaveAllowed:protected] => 1
                            [_isObjectNew:protected] => 
                            [_data:protected] => Array
                                (
                                    [group_id] => 0
                                    [website_id] => 0
                                    [name] => Default
                                    [root_category_id] => 0
                                    [default_store_id] => 0
                                )

                            [_hasDataChanges:protected] => 1
                            [_origData:protected] => Array
                                (
                                    [group_id] => 0
                                    [website_id] => 0
                                    [name] => Default
                                    [root_category_id] => 0
                                    [default_store_id] => 0
                                )

                            [_idFieldName:protected] => group_id
                            [_isDeleted:protected] => 
                            [_oldFieldsMap:protected] => Array
                                (
                                )

                            [_syncFieldsMap:protected] => Array
                                (
                                )

                        )

                )

            [_groupIds:protected] => Array
                (
                    [0] => 0
                )

            [_groupsCount:protected] => 1
            [_stores:protected] => Array
                (
                    [0] => Mage_Core_Model_Store Object
 *RECURSION*
                )

            [_storeIds:protected] => Array
                (
                    [0] => 0
                )

            [_storeCodes:protected] => Array
                (
                    [0] => admin
                )

            [_storesCount:protected] => 1
            [_defaultGroup:protected] => Mage_Core_Model_Store_Group Object
                (
                    [_cacheTag:protected] => 1
                    [_eventPrefix:protected] => store_group
                    [_eventObject:protected] => store_group
                    [_stores:protected] => Array
                        (
                            [0] => Mage_Core_Model_Store Object
 *RECURSION*
                        )

                    [_storeIds:protected] => Array
                        (
                            [0] => 0
                        )

                    [_storeCodes:protected] => Array
                        (
                            [0] => admin
                        )

                    [_storesCount:protected] => 1
                    [_defaultStore:protected] => Mage_Core_Model_Store Object
 *RECURSION*
                    [_website:protected] => Mage_Core_Model_Website Object
 *RECURSION*
                    [_isReadOnly:Mage_Core_Model_Store_Group:private] => 
                    [_resourceName:protected] => core/store_group
                    [_resource:protected] => 
                    [_resourceCollectionName:protected] => core/store_group_collection
                    [_dataSaveAllowed:protected] => 1
                    [_isObjectNew:protected] => 
                    [_data:protected] => Array
                        (
                            [group_id] => 0
                            [website_id] => 0
                            [name] => Default
                            [root_category_id] => 0
                            [default_store_id] => 0
                        )

                    [_hasDataChanges:protected] => 1
                    [_origData:protected] => Array
                        (
                            [group_id] => 0
                            [website_id] => 0
                            [name] => Default
                            [root_category_id] => 0
                            [default_store_id] => 0
                        )

                    [_idFieldName:protected] => group_id
                    [_isDeleted:protected] => 
                    [_oldFieldsMap:protected] => Array
                        (
                        )

                    [_syncFieldsMap:protected] => Array
                        (
                        )

                )

            [_defaultStore:protected] => Mage_Core_Model_Store Object
 *RECURSION*
            [_isCanDelete:protected] => 
            [_isReadOnly:Mage_Core_Model_Website:private] => 
            [_resourceName:protected] => core/website
            [_resource:protected] => 
            [_resourceCollectionName:protected] => core/website_collection
            [_dataSaveAllowed:protected] => 1
            [_isObjectNew:protected] => 
            [_data:protected] => Array
                (
                    [website_id] => 0
                    [code] => admin
                    [name] => Admin
                    [sort_order] => 0
                    [default_group_id] => 0
                    [is_default] => 0
                )

            [_hasDataChanges:protected] => 1
            [_origData:protected] => Array
                (
                    [website_id] => 0
                    [code] => admin
                    [name] => Admin
                    [sort_order] => 0
                    [default_group_id] => 0
                    [is_default] => 0
                )

            [_idFieldName:protected] => website_id
            [_isDeleted:protected] => 
            [_oldFieldsMap:protected] => Array
                (
                )

            [_syncFieldsMap:protected] => Array
                (
                )

        )

    [_group:protected] => Mage_Core_Model_Store_Group Object
        (
            [_cacheTag:protected] => 1
            [_eventPrefix:protected] => store_group
            [_eventObject:protected] => store_group
            [_stores:protected] => Array
                (
                    [0] => Mage_Core_Model_Store Object
 *RECURSION*
                )

            [_storeIds:protected] => Array
                (
                    [0] => 0
                )

            [_storeCodes:protected] => Array
                (
                    [0] => admin
                )

            [_storesCount:protected] => 1
            [_defaultStore:protected] => Mage_Core_Model_Store Object
 *RECURSION*
            [_website:protected] => Mage_Core_Model_Website Object
                (
                    [_cacheTag:protected] => 1
                    [_eventPrefix:protected] => website
                    [_eventObject:protected] => website
                    [_configCache:protected] => Array
                        (
                        )

                    [_groups:protected] => Array
                        (
                            [0] => Mage_Core_Model_Store_Group Object
 *RECURSION*
                        )

                    [_groupIds:protected] => Array
                        (
                            [0] => 0
                        )

                    [_groupsCount:protected] => 1
                    [_stores:protected] => Array
                        (
                            [0] => Mage_Core_Model_Store Object
 *RECURSION*
                        )

                    [_storeIds:protected] => Array
                        (
                            [0] => 0
                        )

                    [_storeCodes:protected] => Array
                        (
                            [0] => admin
                        )

                    [_storesCount:protected] => 1
                    [_defaultGroup:protected] => Mage_Core_Model_Store_Group Object
 *RECURSION*
                    [_defaultStore:protected] => Mage_Core_Model_Store Object
 *RECURSION*
                    [_isCanDelete:protected] => 
                    [_isReadOnly:Mage_Core_Model_Website:private] => 
                    [_resourceName:protected] => core/website
                    [_resource:protected] => 
                    [_resourceCollectionName:protected] => core/website_collection
                    [_dataSaveAllowed:protected] => 1
                    [_isObjectNew:protected] => 
                    [_data:protected] => Array
                        (
                            [website_id] => 0
                            [code] => admin
                            [name] => Admin
                            [sort_order] => 0
                            [default_group_id] => 0
                            [is_default] => 0
                        )

                    [_hasDataChanges:protected] => 1
                    [_origData:protected] => Array
                        (
                            [website_id] => 0
                            [code] => admin
                            [name] => Admin
                            [sort_order] => 0
                            [default_group_id] => 0
                            [is_default] => 0
                        )

                    [_idFieldName:protected] => website_id
                    [_isDeleted:protected] => 
                    [_oldFieldsMap:protected] => Array
                        (
                        )

                    [_syncFieldsMap:protected] => Array
                        (
                        )

                )

            [_isReadOnly:Mage_Core_Model_Store_Group:private] => 
            [_resourceName:protected] => core/store_group
            [_resource:protected] => 
            [_resourceCollectionName:protected] => core/store_group_collection
            [_dataSaveAllowed:protected] => 1
            [_isObjectNew:protected] => 
            [_data:protected] => Array
                (
                    [group_id] => 0
                    [website_id] => 0
                    [name] => Default
                    [root_category_id] => 0
                    [default_store_id] => 0
                )

            [_hasDataChanges:protected] => 1
            [_origData:protected] => Array
                (
                    [group_id] => 0
                    [website_id] => 0
                    [name] => Default
                    [root_category_id] => 0
                    [default_store_id] => 0
                )

            [_idFieldName:protected] => group_id
            [_isDeleted:protected] => 
            [_oldFieldsMap:protected] => Array
                (
                )

            [_syncFieldsMap:protected] => Array
                (
                )

        )

    [_configCache:protected] => Array
        (
            [web/secure/use_in_adminhtml] => 0
            [web/cookie/cookie_lifetime] => 3600
            [web/cookie/cookie_httponly] => 1
            [web/session/use_remote_addr] => 0
            [web/session/use_http_via] => 0
            [web/session/use_http_x_forwarded_for] => 0
            [web/session/use_http_user_agent] => 0
            [general/locale/code] => en_AU
            [dev/translate_inline/active_admin] => 0
            [design/theme/default] => default
            [system/adminnotification/frequency] => 1
            [admin/security/use_form_key] => 1
            [cataloginventory/item_options/manage_stock] => 1
            [design/theme/layout_ua_regexp] => a:0:{}
            [design/head/title_prefix] => 
            [design/head/title_suffix] => 
            [web/unsecure/base_url] => http://local.magentocommerce.com/
            [web/unsecure/base_link_url] => http://local.magentocommerce.com/
            [web/url/use_store] => 0
            [acidgreen_utils/environment/env_enable_marker_admin] => 0
            [design/theme/skin_ua_regexp] => a:0:{}
            [web/secure/base_url] => http://local.magentocommerce.com/
            [web/unsecure/base_skin_url] => http://local.magentocommerce.com/skin/
            [design/theme/template_ua_regexp] => a:0:{}
            [web/unsecure/base_js_url] => http://local.magentocommerce.com/js/
            [web/unsecure/base_media_url] => http://local.magentocommerce.com/media/
            [web/seo/use_rewrites] => 1
            [advanced/modules_disable_output/Mage_Sales] => 0
            [cataloginventory/item_options/min_qty] => 0
            [cataloginventory/item_options/min_sale_qty] => 1
            [cataloginventory/item_options/max_sale_qty] => 10000
            [cataloginventory/item_options/backorders] => 0
            [cataloginventory/item_options/notify_stock_qty] => 1
            [cataloginventory/item_options/enable_qty_increments] => 0
            [catalog/productalert/allow_price] => 0
            [catalog/productalert/allow_stock] => 0
            [advanced/modules_disable_output/Uni_Fileuploader] => 0
            [dev/log/active] => 1
            [dev/log/file] => system.log
        )

    [_configCacheBaseNodes:protected] => Array
        (
            [0] => catalog/price/scope
            [1] => web/secure/base_url
            [2] => web/secure/use_in_adminhtml
            [3] => web/secure/use_in_frontend
            [4] => web/url/use_store
            [5] => web/unsecure/base_url
            [6] => web/seo/use_rewrites
            [7] => web/unsecure/base_link_url
            [8] => web/secure/base_link_url
            [9] => general/locale/code
        )

    [_dirCache:protected] => Array
        (
        )

    [_urlCache:protected] => Array
        (
        )

    [_baseUrlCache:protected] => Array
        (
            [link/false] => http://local.magentocommerce.com/index.php/
            [skin/null] => http://local.magentocommerce.com/skin/
            [link/null] => http://local.magentocommerce.com/index.php/
            [js/null] => http://local.magentocommerce.com/js/
            [web/null] => http://local.magentocommerce.com/
            [media/null] => http://local.magentocommerce.com/media/
        )

    [_session:protected] => 
    [_isAdminSecure:protected] => 
    [_isFrontSecure:protected] => 
    [_frontendName:protected] => 
    [_isReadOnly:Mage_Core_Model_Store:private] => 
    [_resourceName:protected] => core/store
    [_resource:protected] => 
    [_resourceCollectionName:protected] => core/store_collection
    [_dataSaveAllowed:protected] => 1
    [_isObjectNew:protected] => 
    [_data:protected] => Array
        (
            [store_id] => 0
            [code] => admin
            [website_id] => 0
            [group_id] => 0
            [name] => Admin
            [sort_order] => 0
            [is_active] => 1
        )

    [_hasDataChanges:protected] => 1
    [_origData:protected] => Array
        (
            [store_id] => 0
            [code] => admin
            [website_id] => 0
            [group_id] => 0
            [name] => Admin
            [sort_order] => 0
            [is_active] => 1
        )

    [_idFieldName:protected] => 
    [_isDeleted:protected] => 
    [_oldFieldsMap:protected] => Array
        (
        )

    [_syncFieldsMap:protected] => Array
        (
        )

)

paresh posted on - Thursday 5th of February 2015 06:16:50 AM

How can i get current selected store id on my module controller file.
 
not published on website


QR Code: Magento get current store information