Get shipping address on checkout_onepage_controller_success_action in Magento 2

Viewed 191

I want to fetch all shipping details on the checkout_onepage_controller_success_action event.

I am using the following code:

$event = $observer->getEvent();
$orderIds = $event->getOrderIds();
$order_id = $orderIds[0];
$shipping = $order_id->getShippingAddress()->getData();
$shippingMethod = $order_id->getShippingMethod();

And I'm getting the error:

Error: Call to a member function getShippingAddress() on string.

According to me the way of passing $order_id on getShippingAddress() is wrong. Can someone brief me on the same? I want to fetch complete data across order id.

1 Answers

This code within your observer class should work:

protected $orderFactory;

public function __construct(
    \Magento\Sales\Model\OrderFactory $orderFactory
) {
    $this->orderFactory = $orderFactory;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $orderId = $observer->getEvent()->getOrderIds()[0];
    $order = $this->orderFactory->create()->load($orderId);
    $shippingInformation = $order->getShippingAddress();
}
Related