2

we have the opposite question from this one: How to get the store id from the code

question: How do I get the Magento store_code when I have store_id? (quick without loading the whole config)

So we have ID=6 and need the code = store_EN

thanks in advance

2 Answers 2

4

Use this below code to get the store code using store id

Mage::app()->getStore($StoreId)->getCode();

Note:

Magento provides a single function getStore() by which you can get store data by store id or store code.

In Magento, you can load the store Object by code /idon getStore() function.

After that from that StoreObject you can get store code or store id as per as your requirement

see at Mage_Core_Model_App class function at getStore().

On this function if your parameter is numeric then * store object is loaded by store id*

 if (is_numeric($id)) {
                $store->load($id);
            }

if param is string then load store object by store code

 elseif (is_string($id)) {
        $store->load($id, 'code');
    }

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;
        }
6

If you want to do it quicker than loading the whole store model, you could also use a database query.

$resource = Mage::getModel('core/resource');
$db = $resource->getConnection('core_read');
$storeCode = $db->fetchOne($db->select()->from($resource->getTableName('core_store'), 'code')->where('store_id = ?', $storeId));

There seems to be a group of users that seem to always advise against these type of 'direct' queries (although we're using an ORM) due to backward/forward compatibility. I usually agree, although you have to look at it on a case-by-case basis. Concerning this piece of code, there is only one table used (core_store) and that table hasn't changed since version 1.1 when multistore was introduced and won't ever change again in the 1.x range since there will only be minor updates from now on, with Magento 2 and all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.