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\Api\Data\OrderInterface to load order by increment id.

By Object Manager:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$incrementId = "100009633";
$orderInterface = $objectManager->create('Magento\Sales\Api\Data\OrderInterface'); 
$order = $orderInterface->loadByIncrementId($incrementId);

// Print Order Object
print_r($order->debug());

// Get Order Id
echo $order->getId(); 

By Dependency Injection:

private $order;

public function __construct(
    ...
    \Magento\Sales\Api\Data\OrderInterface $order,
    ...
) {
    $this->order = $order;
}

public function getOrder($incrementId)
{
    $order = $this->order->loadByIncrementId($incrementId);
    return $order;
}

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

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

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