Magento - How to get the store country?

Magento - How to get the store country?

July 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - How to get the store country?

Magento: Retrieving the Store Country for Transactional Emails

Introduction

As an e-commerce developer, you might find yourself needing to display or use the store’s country in transactional emails sent to customers. This could be useful for compliance purposes, addressing customers by their region, or simply providing them with relevant information about your business.

Understanding Magento Store Configuration

Magento allows you to manage multiple stores and websites within a single installation. Each store has its own configuration settings, including the country where it operates. To access this information, we need to understand how Magento structures its data models.

Getting Started: The Mage_Core_Model_Store Class

The Mage_Core_Model_Store class is the primary model for managing stores in Magento. It contains a variety of attributes related to each store’s configuration, including the country.

Retrieving the Store Country

Method 1: Using the Current Store Object

One of the simplest ways to retrieve the store country is by using the current store object provided by Magento:

$store = Mage::app()->getStore();
$country = $store->getCode('country_id');

In this code, Mage::app()->getStore() retrieves the current store being accessed. The getCode method then retrieves the country ID associated with that store.

Method 2: Using the Core Helpers

Magento also provides core helpers for dealing with store configurations:

$country = Mage::helper('core')->decrypt(Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_COUNTRY));

Here, we use Mage::helper('core') to retrieve the core helper responsible for handling various system-level operations. We then call its decrypt method with the country configuration path (Mage_Core_Model_Store::XML_PATH_COUNTRY) to retrieve the encrypted country value.

Using the Country in Transactional Emails

Adding a Variable to Your Email Template

To use this information in your transactional emails, you’ll need to add it as a variable to your email template:

<!-- app/design/frontend/base/default/template/email/order/items.phtml -->
<strong>Country:</strong>
<?php echo $this->getCountry() ?>

In this example, we assume that the order object passed to the template contains a method called $this->getCountry().

Populating the Variable in Your Observer

To populate this variable with the actual country value, you would use an event observer:

/**
 * Add store country to order emails
 *
 * @param Varien_Event_Observer $observer
 */
public function addStoreCountry(Varien_Event_Observer $observer)
{
    /** @var Mage_Core_Model_Email_Template_Mailer $mailer */
    $mailer = $observer->getMailer();
    $templateId = 'your_email_template_id';
    $store = $mailer->getSentMessage()->getEmail()->getStoreId();
    $country = Mage::app()->getStore($store)->getCode('country_id');
    $mailer->setStore($store);
    $mailer->addVariable('country', $country, true);
}

In this observer method, we retrieve the country ID of the store associated with the current email template. We then add this as a variable to the mailer using $mailer->addVariable().

Conclusion

Retrieving and using the store country in Magento is a straightforward process that involves understanding the core data models and utilizing existing methods provided by the framework.

Remember, proper configuration of your store settings is crucial for accurate results. Always ensure you have configured the correct country code for each store within the Magento admin panel.

For more advanced scenarios or custom implementations, consider exploring the Magento extension ecosystem or consulting with a Magento development partner like Laravel Company.

Happy coding!

Configuration Email Customers

Our network