How to Get Order Information By Order Id in Magento 2

In this code snippet, we will see how to get order information such as order items, payment, customer, billing and shipping details from order id in Magento 2.

Using Dependency Injection (DI)

Here is the example code to get the order information by order Id and increment Id in Magento 2 using dependency injection.

In this, we might need to inject the object of \Magento\Sales\Model\OrderRepository class in the constructor of our module’s block class and access it from the view ( .phtml ) file.

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;
 
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{ 
    protected $orderRepository;

    protected $searchCriteriaBuilder;
  
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context, 
        \Magento\Sales\Model\OrderRepository $orderRepository,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        array $data = []
    ) {
        $this->orderRepository = $orderRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        parent::__construct($context, $data);
    }
  
    public function getOrderById($id) {
        return $this->orderRepository->get($id);
    }
  
    public function getOrderByIncrementId($incrementId) {
        $this->searchCriteriaBuilder->addFilter('increment_id', $incrementId);

        $order = $this->orderRepository->getList(
            $this->searchCriteriaBuilder->create()
        )->getItems();

        return $order;
    }
}

Now, we can use the functions in our view (.phtml) file as follows,

// Get order by id
$order = $block->getOrderById(1);
 
// Get order by increment id
$order = $block->getOrderByIncrementId('000000001');

// Fetch whole order information
print_r($order->getData());

// Fetch specific order information
echo $order->getIncrementId();
echo $order->getGrandTotal();
echo $order->getSubtotal();

// Fetch order customer information
echo $order->getCustomerId();
echo $order->getCustomerEmail();
echo $order->getCustomerFirstname();
echo $order->getCustomerLastname();

// Fetch order items information
foreach ($order->getAllItems() as $item)
{
   echo $item->getId();
   echo $item->getProductType();
   echo $item->getQtyOrdered();
   echo $item->getPrice();
}

Using Object Manager

Here is the example code to get the order information by order id in Magento 2 using object manager.

$orderId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);
 
// Fetch whole order information
print_r($order->getData());
Get Order Information From Order ID
$orderId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);

// Fetch whole order information
print_r($order->getData());
 
// Fetch specific information
echo $order->getIncrementId();
echo $order->getGrandTotal();
echo $order->getSubtotal();

// Fetch customer information
echo $order->getCustomerId();
echo $order->getCustomerEmail();
echo $order->getCustomerFirstname();
echo $order->getCustomerLastname();
Get Order Items Information
$orderId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);

foreach ($order->getAllItems() as $item)
{
    echo $item->getId();
    echo $item->getName();
    echo $item->getProductType();
    echo $item->getQtyOrdered();
    echo $item->getPrice();
}
Get Order Payment Information
$orderId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);

// Fetch whole payment information
print_r($order->getPayment()->getData());
 
// Fetch specific payment information
echo $order->getPayment()->getAmountPaid();
echo $order->getPayment()->getMethod();
echo $order->getPayment()->getAdditionalInformation('method_title');
Get Order Billing & Shipping Information
$orderId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId);

// Fetch whole billing information
print_r($order->getBillingAddress()->getData());
 
// Fetch specific billing information
echo $order->getBillingAddress()->getCity();
echo $order->getBillingAddress()->getRegionId();
echo $order->getBillingAddress()->getCountryId();
 
// Fetch whole shipping information
print_r($order->getShippingAddress()->getData());
 
// Fetch specific shipping information
echo $order->getShippingAddress()->getCity();
echo $order->getShippingAddress()->getRegionId();
echo $order->getShippingAddress()->getCountryId();

Read: Programmatically Create New Order in Magento 2

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *