Get order id by increment id Magento 2

Get order id by increment id Magento 2

In this article, I will show you how you can load order by increment id in Magento 2

We can use the order interface \Magento\Sales\Model\Spi\OrderResourceInterface to load order by increment id.

Sample Code:

use Magento\Sales\Api\Data\OrderInterface;

class Mageprince
{
    private $orderResource;
    private $orderFactory;

    public function __construct(
        \Magento\Sales\Model\Spi\OrderResourceInterface $orderResource,
        \Magento\Sales\Api\Data\OrderInterfaceFactory $orderFactory
    ){
        $this->orderResource = $orderResource;
        $this->orderFactory = $orderFactory;
    }

    public function getOrder(incrementId)
    {
        $order = $this->orderFactory->create();
        $this->orderResource->load($order, $incrementId, OrderInterface::INCREMENT_ID);
        return $order;
    }
}

Now use getOrder() function to get order object in phtml file

$incrementId = '100009633';
$order = $this->getOrder($incrementId);

print_r($order->debug()); // Print Order Object
echo $order->getId(); // Get Order Id