Magento 2 Order Status – How to Get Order Status from Order ID

magento2order-status

I'm creating a custom module for order tracking in which i'm accepting order id from customer and using the order id i want to display the status of particular order.

Now I'm not getting the order status where it gets store.

I want the status of order using order id in frontend. Is there any way to get status of order in magento 2.

this is my controller where I get order id

<?php 
namespace Codazon\AjaxCartPro\Controller\Customer;  
class Trackorder extends \Magento\Framework\App\Action\Action {

    protected $resultPageFactory;
    protected $request;

    /**
     * Constructor
     * 
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \Magento\Framework\App\Request\Http $request,
    \Magento\Framework\Registry $coreRegistry
    )
    {
    $this->_coreRegistry = $coreRegistry;
    $this->request = $request;
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Execute view action
     * 
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {           
    $orderid = $this->request->getParam('orderID'); // all params             
    $this->registry->register('order_status', $orderStatus);    
    return $this->resultPageFactory->create();
    }
}

I need whole process after this.

Best Answer

You need to inject \Magento\Sales\Api\OrderRepositoryInterface class to get order status

In Your Block

    protected $orderRepository;

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

    public function getOrderStatus($orderId)
    {
        $order = $this->orderRepository->get($orderId);
        $state = $order->getState(); //Get Order State(Complete, Processing, ....)
        return $state;
    }

Call function in your phtml

$orderId = 1; //Order Id
echo $block->getOrderStatus($orderId); 
Related Topic