Magento – Get customer address id from order

customer-addressmagento2modelsales-order

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;
  }
}

Best Answer

Get customer address id:

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

or

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

Get all shipping details:

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