How to Get Order Details from DB Using Order ID in Magento 2

magento2magento2.2PHP

I need to fetch Order details from core table(sale_order_status_history) using order id.

Best Answer

Using Object Manager Instance

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($IncrementId); //Use increment id here.
$order->getCustomerName(); 

Using Blocks

You need to inject \Magento\Sales\Api\OrderRepositoryInterface class to get order status Add following code in your block

protected $orderRepository;

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

    public function getOrder($orderId)
    {
        $order = $this->orderRepository->get($orderId);

        return $order;
    }

Call function in your .phtml

$orderId = 1; //Order Id
$order = $block->getOrder($orderId); 
// Check all order data using debug \Zend_Debug::dump($order->debug());
$order->getCustomerName(); 
Related Topic