Get current store ID in Magento

#magento #snippet

This here is a really short example of how to retrieve current store ID in Magento. Additionally to that, an example of getting current store object and some of its data. Let’s roll!


Get current store in Magento

<?php

$currentStore = Mage::app()->getStore();

This approach returns an instance of Mage_Core_Model_Store class and can be run from anywhere in Magento - well, as long as main Mage class is loaded (which pretty much always is).


Retrieve current store ID in Magento

<?php

$currentStoreId = Mage::app()->getStore()->getId();

As you can see, this is almost the same as retrieving current store object - we simply call the getId() method of class instance we retrieved via Mage::app()->getStore(). You can, of course, combine the two:

<?php

$currentStore = Mage::app()->getStore(); // Retrieve current store object
$currentStoreId = $currentStore->getId(); // Retrieve the ID of current store

Get current store URL, name or other data in Magento

Naturally, by having retrieved current store, you can as well access its other data (URL, name, etc.):

<?php

$currentStore = Mage::app()->getStore();
var_dump($currentStore->getName()); // Returns store name
var_dump($currentStore->getBaseUrl()); // Returns current store URL
var_dump($currentStore->getData()); // Returns an array or other available data

Hope you find these few lines of code useful! Cheers!

⇐ All Blog Posts
Tweet Share