Get shipping address county in Magento 2 observer

Get shipping address county in Magento 2 observer

September 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get shipping address county in Magento 2 observer

How to Get Shipping Address Country Name in Magento 2 Observer

Magento 2 observers provide a powerful way to intercept specific events within the application lifecycle. In this post, we’ll guide you through extracting the shipping address country name from an observer method.

Understanding Magento Observers

Observers allow us to listen for certain events (like checkout success or order placement) and execute custom code when those events occur. They are defined as plugins that can be enabled or disabled independently of the core application.

Accessing Shipping Address Data

Let’s assume you have an observer method like this:

public function execute(\Magento\Framework\Event\Observer $observer)
{
  // Your observer logic here
}

To access the shipping address data, you need to retrieve the checkout session and then get the quote object. The quote contains all the information about the current shopping cart, including the shipping address.

Retrieving Shipping Address Country

The shipping address is stored within the getShippingAddress() method of the quote object. To extract the country name, use the following code:

public function execute(\Magento\Framework\Event\Observer $observer)
{
  $checkoutSession = $this->_checkoutSession;
  $quote = $checkoutSession->getQuote();
  $shippingAddress = $quote->getShippingAddress();

  if ($shippingAddress) {
    // Get the country name
    $countryName = $shippingAddress->getCountryId(); // ISO code, e.g., "US" for United States

    // Print or use the country name as needed
    echo 'Shipping Country: ' . $countryName;
  }
}

Note that $_checkoutSession should be auto-injected using dependency injection in your constructor. Ensure you have properly configured the di.xml file to enable this.

Getting the Full Country Name

The above code returns the ISO country code, not the full name. To get the full country name, you need to use the Magento 2 directory setup:

public function execute(\Magento\Framework\Event\Observer $observer)
{
  // ...
  if ($shippingAddress) {
    $countryCode = $shippingAddress->getCountryId();
    $directory = $this->_directory;
    $fullCountryName = $directory->getCountryNameByCode($countryCode);

    echo 'Shipping Country: ' . $fullCountryName;
  }
}

Conclusion

In this post, we demonstrated how to retrieve the shipping address country from a Magento 2 observer. By using the checkout session and quote object, you can access various cart details, including the shipping address.

Remember that observers are powerful tools for extending core functionality without modifying the original codebase. They should be used judiciously to maintain the application’s stability and performance.

For more advanced techniques in working with Magento 2 events and observers, consider reading our comprehensive guide on Magento 2 Observers: The Definitive Guide.

Magento 2 Checkout Extensions Orders

Our network