I want to get estimated shipping adddress for a custom controle of country so how to prodceed please and thanks in advance
3
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
-
-
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 – Developper Magento Aug 14 at 9:57
-
-
-