Magento: Retrieving Allowed Countries as OptionsArray
In Magento, you can easily retrieve a list of all allowed countries for your store using the directory/country model. However, to obtain this data in the format suitable for dropdown options (options array), you’ll need to follow some specific steps.
Firstly, let’s load the collection of available countries:
$countryCollection = Mage::getModel('directory/country')->getResourceCollection()->loadByStore();
This code snippet loads all countries that are enabled for your current Magento store. The loadByStore() method ensures we get only the countries set as allowed in your store’s configuration.
Converting to OptionsArray
To convert this collection into an options array, you can use a custom function or simply loop through the results. Here’s how you can achieve this:
public function getAllowedCountriesOptions()
{
$options = array();
$countryCollection = Mage::getModel('directory/country')->getResourceCollection()->loadByStore();
foreach ($countryCollection as $country) {
$options[$country->getCountryId()] = $country->getName();
}
return $options;
}
In this function, we initialize an empty array called $options to store our results. We then loop through each country in the collection and add a key-value pair to the options array.
The key is obtained using getCountryId(), which returns the ISO 2-letter code for the country (e.g., ‘US’ for the United States). The value is the full name of the country, retrieved with getName().
Filtering by Website
If you want to retrieve only the countries allowed per website, instead of the entire store, you can modify the function slightly:
public function getAllowedCountriesOptionsByWebsite($websiteId)
{
$options = array();
$countryCollection = Mage::getModel('directory/country')->getResourceCollection()->loadByStore($websiteId);
foreach ($countryCollection as $country) {
$options[$country->getCountryId()] = $country->getName();
}
return $options;
}
In this version, we pass the $websiteId parameter to loadByStore(). This ensures that only countries allowed for the specified website are included in the options array.
Using the OptionsArray
Now that you have your options array, you can use it directly in your dropdowns or any other UI elements that require a list of countries:
$options = $this->getAllowedCountriesOptions();
echo Mage::helper('core')->htmlSelect(
'country_id',
$options,
null,
true
);
In this example, we’re using Magento’s built-in helper to generate an HTML select element with the options array as its data source. The true parameter ensures the first option is empty (useful for “Please Select” type dropdowns).
Conclusion
Retrieving a list of allowed countries in Magento and converting it into an options array is a straightforward process once you understand how to load the collection by store or website.
Remember, for multi-site setups, always use the loadByStore() method with the appropriate $websiteId parameter to ensure you get the right data for each site.
If you’re looking for more tips on Magento development and best practices, check out our in-depth guides at Laravel Company.