Magento, Magento 2

How to Get Order Information by Order Id in Magento 2

How to Get Order Information by Order Id in Magento 2

In this tutorial, Today I will explain to how to get order information by order id in Magento 2. You can get details from order information object like order items, payment, customer’s billing and shipping address details etc. We can get order information using factory methods and using repository method.

You may also like this :

Steps of Get Order Information From Order ID :

1) First of all, You need to inject \Magento\Sales\Api\OrderRepositoryInterface class in your construct and I added details of order in one controller :

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\Helloworld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    protected $orderRepository;

    /**
     * @param \Magento\Framework\App\Action\Context       $context
     * @param \Magento\Framework\View\Result\PageFactory  $resultPageFactory
     * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->orderRepository = $orderRepository;
        parent::__construct($context);
    }
    /**
     * Detect Mobile view or Desktop View
     *
     * @return void
     */
    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $orderId = 1;
        $order = $this->orderRepository->get($orderId);
        echo "Order Increment ID : " . $order->getIncrementId() . "<br/>";
        echo "Order Grand Total : " . $order->getGrandTotal() . "<br/>";
        echo "Order Sub Total : " . $order->getSubtotal() . "<br/>";

        echo "Customer ID : " . $order->getCustomerId() . "<br/>";
        echo "Customer Email : " . $order->getCustomerEmail() . "<br/>";
        echo "First Name : " . $order->getCustomerFirstname() . "<br/>";
        echo "Last Name : " . $order->getCustomerLastname() . "<br/>";

        //Billing Information
        echo "<pre/>";
        print_r($order->getBillingAddress()->getData());

        //Shipping Information
        print_r($order->getShippingAddress()->getData());

        //Payment Information
        print_r($order->getPayment()->getData());

        exit;

        return $resultPage;
    }
}

2) After that, You need to clean cache.

Using this above code, You can details of customer information, billing information, shipping information and order’s items data from order object. In addition, You can get all visible items and all items from order object. If you want to get payment information then, you can get it using this above code.

You can see in below screenshot.

Output :

 

Order Information Rohan Hapani

 

That’s it !!!

I hope this blog is easy to understand about how to get order information by order id in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Stay Safe and Stay Connected !!

Tagged ,