skip to Main Content

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

public function execute(MagentoFrameworkEventObserver $observer)
    {

Please let me know how can i get this this ?

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

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

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

2

Answers


  1. declare a public variable and intialize the variable like below ,then you will be able to use it in your execute method

    class Example {
     
    public $_checkOutSession;
    
    public function __construct(MagentoCustomerModelSession $checkOUtSession)
    {
    $this->_checkOutSession = $checkOutSession;
    }
    
    public function execute(MagentoFrameworkEventObserver $observer)
    {
      $postCode = $this->_checkoutSession->getQuote()->getShippingAddress()->getPostcode();
    }
    
    }
    
    Login or Signup to reply.
  2. It depends on which observer do you want to get it. Can you provide a more specific scenario?

    In which checkout session currently has a quote. You can use the above solution from Reggie.

    Or else you need to initialize quote/order to get it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search