1

I want to get estimated shipping adddress for a custom controle of country so how to prodceed please and thanks in advance

1 Answer 1

4

Using Event observer to get estimated shipping address before place order. Please find below steps to get shipping address.

Step-1: Create custom Module

    Path- app/code/Custom/Module

Step-2: Create events.xml under module etc folder

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="checkout_submit_before">
       <observer name="custom_module_observer_index" instance="Custom\Module\Observer\Index" />
   </event>
</config>

Step-3: Create Observer to execute event below path

    app/code/Custom/Module/Observer/Index.php

Index.php

<?php
namespace Custom\Module\Observer;
use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\ObjectManagerInterface;
use \Magento\Checkout\Model\Session as CheckoutSession;

class Index implements ObserverInterface
{
     protected $messageManager;
     protected $scopeConfig;
     protected $checkoutSession;
     protected $logger;
   protected $_coreSession;
   protected $_customerRepositoryInterface;

     public function __construct(
        ManagerInterface $messageManager,
        ScopeConfigInterface $scopeConfig,
        ObjectManagerInterface $objectmanager,
        CheckoutSession $checkoutSession,
        \Psr\Log\LoggerInterface $logger,
     \Magento\Framework\Session\SessionManagerInterface $coreSession,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
                    ) {
        $this->messageManager = $messageManager;
        $this->scopeConfig = $scopeConfig;
        $this->_objectManager = $objectmanager;
        $this->checkoutSession = $checkoutSession;
        $this->logger = $logger;
        $this->_coreSession = $coreSession;
        $this->_customerRepositoryInterface = $customerRepositoryInterface;

    }
  public function execute(Observer $observer)
  { 



         $order = $observer->getEvent()->getOrder();
         $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
         $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
         $store_id = $storeManager->getStore()->getStoreId();
         $this->logger->info("storeid==>".$store_id."<");

         // get shipping address

         $shippingAddress = $order->getShippingAddress(); 
         $city = $shippingAddress->getCity();
         $country = $shippingAddress->getCountryId();
         $this->logger->info("shippingAddress=>".$country."<");
         $this->logger->info("shippingAddress=>".$city."<");

        } 


  }

Note

9
  • thanks for your replay i will test this code immediatly Aug 14, 2019 at 8:53
  • now how to pass variable from this observer to onother please i need this estimated country to be present in an observer that belong to event payment_method_is_active so how to to Aug 14, 2019 at 9:57
  • where are you pass this variable?
    – Devidas
    Aug 14, 2019 at 11:25
  • you can set variable in custom session and get it where you want.
    – Devidas
    Aug 14, 2019 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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