How to load Order data by Id programmatically in Magento 2

In this tutorial, we are going to explain to you a detailed guide on how to get the Order data by order ID in Magento 2. Order Data contains the order information like Customer Info, Order Date, Order Status, Billing Address, Shipping Address, Total summary, and Payment related data in Magento 2.

In this method, we will use the OrderRepositoryInterface way to get Order Data information by Order Id. We can get order data by Order id using below code snippets.

 

public function __construct(
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
){
    $this->orderRepository = $orderRepository;
}


/**
  * Get Order data by orderId
  * @param int $orderId The order ID.
  * @return array
  */
public function getOrderData($orderId)
{
   try {
      $order = $this->orderRepository->get($id);
   } catch (NoSuchEntityException $e) {
      throw new \Magento\Framework\Exception\LocalizedException(__('This order no longer exists.'));
   }
   return $order;
}

 

Now call getOrderData() function to get order object in template file by below way,

$orderData = $this->getOrderData($data['order_id']);
foreach($orderData as $order) {
	echo "<pre>";print_r($order->debug());
}

 

The result will be given order data of specific order id. We hope the above blog helps you to clearly understand How To Load Order By Id Magento 2 Programmatically. In case of any kind of problem with the above code implementation, you can contact us or let us know in the comment section.