Im searching since a week how i can get a store_id / website_id for a given language.


$countryId = 'DE'; // or country = Germany
foreach (Mage::app()->getStores() as $store) {
    if($store->getCountryId() === $countryId){
        return $store;

How could i solve this?

EDIT: 03022016

Im now using the general/country/allow config (system/general/allowed countries)

This will get the first store that is using the given country.

Ofc the country could be used on multiply stores, but any other solution i found got the same problem.

The point why im using this solution is that the IDs are real country_id s (not editable codes-strings).

public function getStoreIdByCustomerCountryId($countryIdCustomer)
    $countryIdReturn = null;
    $countryIdCustomer = trim((string)$countryIdCustomer);
    if (!strlen($countryIdCustomer)) {
        return false;
    foreach (Mage::app()->getStores() as $store) {
        if (!$store->getIsActive()) {
        foreach (
            explode(',', $store->getConfig('general/country/allow'))
            as $countryId
        ) {
            if (trim((string)$countryId) === $countryIdCustomer) {
                $countryIdReturn = $store->getId();
                break 2;
    return $countryIdReturn;

2 Answers 2


Try like that (in some phtml file, but you can rearange it to fit only in your php function):

  <?php foreach(Mage::app()->getWebsites() as $website): ?>
    <?php foreach ($website->getGroups() as $group): ?>
      <?php $stores = $group->getStores();
        foreach ($stores as $store): ?>
        <li class="<?php echo Mage::app()->getStore($store->getStoreId())->getCode();?>"><a href="<?php echo Mage::app()->getStore($store->getStoreId())->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);?>"></a></li>
      <?php endforeach; ?>
    <?php endforeach; ?>
  <?php endforeach; ?>

Now, with $store->getStoreId() you are able to get the store id.

  • Not sure if you got my question right. How should i now get a storeId by a countryId ?
    – cottton
    Commented Jan 29, 2016 at 18:16

If you have access to the Admin Panel, go to System -> Manage Stores -> choose a store view and add a code of the country in there. For example: enter image description here

In php you can get this code with Mage::app()->getStore($store->getStoreId())->getCode(); and you can compare this:

if (Mage::app()->getStore($store->getStoreId())->getCode() == $countryId) {
  return Mage::app()->getStore($store->getStoreId());

Is this what you wanted?

  • (Sorry for the delay) Thank you, this would work but depends on access/able to change the store code (which i can/should not). I edited my question and added the solution i found.
    – cottton
    Commented Feb 3, 2016 at 10:57

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.