Question

Hi I have a custom module . I want to get shipping country inside

public function execute(\Magento\Framework\Event\Observer $observer)
    {

Please let me know how can i get this?

I have seen this code for postcode , but i am not sure this is working or not

  $postCode = $this->_checkoutSession->getQuote()-getShippingAddress()->getPostcode();

I need shipping address country name in public function execute. Please help .

Was it helpful?

Solution

If you're using sales_order_payment_save_after event then in your observer, get address details as follow :

public function execute(Observer $observer)
    {
        /** @var OrderPaymentInterface $payment */
        $payment = $observer->getDataByKey('payment');

        $order = $payment->getOrder();

        $shippingAddress = $order->getShippingAddress();
        $city = $shippingAddress->getCity();
        $state = $shippingAddress->getRegion();
        $countryData = $this->countryInformation->getCountryInfo($shippingAddress->getCountryId());
        $country = $countryData->getFullNameLocale();
        return $this;
    }

Source: link

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top