How to get order data by order id programmatically in magento 2?

Magento  2 Fetch order details by Order ID. With the help of Magento\Sales\Api\OrderRepositoryInterface, You can retrieve order-related data programmatically.

Order Entity generated after the Customer Placed Order successfully from the Storefront or Admin Can Place an order from the Magento Back Panel.

Get Order Data By Order Id to retrieve order information using the OrderRepositoryInterface way.

Always Use the Order Repository to fetch order details instead of the Order Factory Pattern.

You can get order data by Order id using below code snippets,


namespace Rbj\SalesOrder\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

class GetOrder
    public function __construct(
        private OrderRepositoryInterface $orderRepository

     * @param int $id The order ID.
     * @return OrderInterface
     * @throws LocalizedException
    public function getOrderData(int $id): OrderInterface
        try {
            $order = $this->orderRepository->get($id);
        } catch (NoSuchEntityException $exception) {
            throw new LocalizedException(__('This order no longer exists.'));

        return $order;

Call function 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. You can fetch data as per your requirement from the Order Object.