0

I have an observer after placed an order, and in that observer, I need to get the customer address id that customer used for shipment and load the customer Address Model that can save and update data. Is there a way to do this:

events.xml

<event name="checkout_onepage_controller_success_action">
    <observer name="namespace_module_sales_order_save_after" instance="Namespace\Module\Observer\AfterOrder"  />
    </event>

Observer/AfterOrder.php

use \Psr\Log\LoggerInterface; 
class AfterOrder implements ObserverInterface
{
    protected $_checkoutSession;
    protected $_customerRepositoryInterface;
    protected $_addressInterface;

  public function __construct(
    LoggerInterface $loggerInterface,
    \Magento\Framework\Registry $registry,
    \Magento\Sales\Model\OrderFactory $orderFactory,
  \Magento\Quote\Model\QuoteFactory $quoteFactory
    )
  {
      $this->_logger = $loggerInterface;
            $this->orderFactory = $orderFactory;
            $this->registry = $registry;
      $this->quoteFactory = $quoteFactory;
  }

  public function execute(\Magento\Framework\Event\Observer $observer)
  {
    $orderId = $observer->getEvent()->getOrderIds();
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId[0]);
    $quote = $this->quoteFactory->create()->load($order->getQuoteId());
    echo $order->getQuoteId(); exit;
  }
}

2 Answers 2

2

Get customer address id:

$order->getShippingAddress()->getCustomerAddressId();

or

$order->getShippingAddress()->getData('customer_address_id');

Get all shipping details:

$order->getShippingAddress()->getData();
0

Not test yet. But you can try with

$shippingAddress = $this->checkoutSession->getLastRealOrder()->getShippingAddress();

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.