Get all Magento StoreViews
I show you how to get all Magento StoreViews. In a recent project I had to create user rights for each StoreView, user were only allowed to edit CMS sites for specific Stores. In combination with already existing Magento plugins it was only possible with some coding.
Get all Magento StoreViews
I created a simple module for doing some work in Helper Class. One of them is to return an array with all StoreViews. This is really simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 | function getStoreViewList() { $_store = array(); foreach (Mage::app()->getWebsites() as $website) { foreach ($website->getGroups() as $group) { $stores = $group->getStores(); foreach ($stores as $store) { $_store[] = array("id" => $store->getId(), "name" => $store->getName()); } } } return $_store; } |
With this array you can display all StoreViews by name or you can create forms for setting values for each specific store. In this example I use only name and id attribute of stores, you can use even more:
- store object1$store = Mage::app()->getStore();
- store id1$store->getId();
- store code1$store->getCode();
- store name1$store->getName();
- store website id1$store->getWebsiteId();
- store group id1$store->getGroupId();
- store sort order1$store->getSortOrder();
- store active flag1$store->getIsActive();
- store locale code1$store->getLocaleCode();
- store home url1$store->getHomeUrl();
- store current currency1$store->getCurrentCurrencyCode();
Get current StoreView
For frontend developers it is sometimes necessary to get the current displayed StoreView. This is possible with the following code:
1 | $store = Mage::app()->getStore(); |
which can be called from every phtml or php file from Magento.
Get StoreView by Id
Sometimes you have your StoreView Id and you need to get the StoreView object to get for example the name of this StoreView. You get StoreView by Id the following way:
1 | $store = Mage::getModel('core/store')->load($storeId); |
Create StoreViews in backend
You can create StoreViews, Stores and Websites under System->Manage Stores. If you create your shop or shops hierarchy, check you attributes first if they can be set at StoreView level or only website level. For different prices for example, you have to create different websites. StoreViews are perfect, if you want to offer different translations for your website.
Conclusion
It is easy to create StoreViews for your Magento shop. It is also very easy to get information about current StoreView to use in all phtml files of your theme. From a StoreView object you can get many things like code or name. You can programmatically get a list of all installed StoreViews in your model to set for example user permissions.