Magento – Get Store/Website by Language/Country

country-regionslanguagemagento-1.9stores

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

Like:

$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()) {
            continue;
        }
        foreach (
            explode(',', $store->getConfig('general/country/allow'))
            as $countryId
        ) {
            if (trim((string)$countryId) === $countryIdCustomer) {
                $countryIdReturn = $store->getId();
                break 2;
            }
        }
    }
    return $countryIdReturn;
}

Best Answer

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

<ul>
  <?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; ?>
  </ul>

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

Related Topic