0

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 .

2

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

8
  • Please note this is on checkout page. Before placing order . – Manik Nov 3 '20 at 5:39
  • 1
    On the same page where you enter address details? I mean before going Next to Review and Payments section? – Abdul Samad Abbasi Nov 3 '20 at 5:49
  • Really thank you for this . Could you please expand the answer buy giving sample code. Currently i have public function execute(\Magento\Framework\Event\Observer $observer) { } so inside this i have to write this new code – Manik Nov 3 '20 at 6:34
  • What event are you calling? – Abdul Samad Abbasi Nov 3 '20 at 6:55
  • $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); – Manik Nov 3 '20 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.