Home > database >  Get shipping address on checkout_onepage_controller_success_action in Magento 2
Get shipping address on checkout_onepage_controller_success_action in Magento 2

Time:09-18

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();

Getting 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.

Rashi

CodePudding user response:

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