Working with orders programmatically is routine in Magento 2 development — custom reports, integrations, cron jobs, order-processing logic. This guide covers the common lookups: load an order by increment ID, load an order by entity ID, load an order by quote ID, and load a quote by customer ID — each with the correct, modern approach (and the pitfalls that trip people up, like the leading-zero increment ID).
How to Load an Order by Increment ID in Magento 2
The increment ID is the human-facing order number (e.g., 000000030). There are three ways to load an order from it — the API repository is the recommended one for real modules; the others are shown for completeness.
Using the Order Repository (Recommended)
Because OrderRepositoryInterface has no direct "by increment ID" method, filter with SearchCriteriaBuilder:
<?php
namespace Mageants\Blog\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
class Test extends Action
{
protected $orderRepository;
protected $searchCriteriaBuilder;
public function __construct(
Context $context,
OrderRepositoryInterface $orderRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
parent::__construct($context);
}
public function execute()
{
$incrementId = '000000030'; // string — keep the leading zeros
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('increment_id', $incrementId)
->create();
$orders = $this->orderRepository->getList($searchCriteria)->getItems();
$order = current($orders); // first (and only) match
if ($order) {
echo $order->getEntityId();
}
}
}
|
Using the Order Factory
The order model has a convenient loadByIncrementId() method. Direct model loading is discouraged in new code in favor of repositories, but it's still common and works:
<?php
namespace Mageants\Blog\Block;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\OrderFactory;
class OrderInfo extends Template
{
protected $orderFactory;
public function __construct(
Context $context,
OrderFactory $orderFactory,
array $data = []
) {
$this->orderFactory = $orderFactory;
parent::__construct($context, $data);
}
public function getOrderByIncrementId()
{
$incrementId = '000000002'; // string, not 000000002
$order = $this->orderFactory->create()->loadByIncrementId($incrementId);
return $order->getEntityId();
}
}
|
Using the Object Manager (Quick Tests Only)
Direct ObjectManager use is fine in a throwaway script but should never appear in module code — always inject dependencies instead:
<?php
$incrementId = '000000002';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->get(\Magento\Sales\Model\OrderFactory::class)
->create()
->loadByIncrementId($incrementId);
echo $order->getEntityId();
|
How to Load an Order by ID (Entity ID) in Magento 2
The entity ID is the internal numeric primary key. Loading by it is the simplest case — OrderRepositoryInterface::get() takes it directly:
<?php
namespace Mageants\Blog\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Sales\Api\OrderRepositoryInterface;
class Order extends Action
{
protected $orderRepository;
public function __construct(
Context $context,
OrderRepositoryInterface $orderRepository
) {
$this->orderRepository = $orderRepository;
parent::__construct($context);
}
public function execute()
{
$orderId = 2; // entity ID
$order = $this->orderRepository->get($orderId);
echo $order->getIncrementId();
}
}
|
How to Load an Order by Quote ID in Magento 2
To find the order(s) tied to a quote, filter the order repository on quote_id:
<?php
namespace Mageants\Blog\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
class Test extends Action
{
protected $orderRepository;
protected $searchCriteriaBuilder;
public function __construct(
Context $context,
OrderRepositoryInterface $orderRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
parent::__construct($context);
}
public function execute()
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('quote_id', 5)
->create();
$orders = $this->orderRepository->getList($searchCriteria)->getItems();
foreach ($orders as $order) {
echo $order->getEntityId() . ' - ' . $order->getIncrementId() . '<br>';
}
}
}
|
How to Load a Quote by Customer ID in Magento 2
To get a customer's quote(s), use the CartRepositoryInterface with a filter on customer_id:
<?php
namespace Mageants\Blog\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
class Test extends Action
{
protected $quoteRepository;
protected $searchCriteriaBuilder;
public function __construct(
Context $context,
CartRepositoryInterface $quoteRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->quoteRepository = $quoteRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
parent::__construct($context);
}
public function execute()
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('customer_id', 1)
->create();
$quotes = $this->quoteRepository->getList($searchCriteria)->getItems();
foreach ($quotes as $quote) {
echo $quote->getId() . '<br>';
}
}
}
|
Looking for the customer-side equivalent? See our detailed post on how to load customers by ID and by email in Magento 2.
Conclusion
Loading orders and quotes in Magento 2 comes down to picking the right repository and filtering it — OrderRepositoryInterface for orders (by entity ID directly, or by increment/quote ID via SearchCriteria) and CartRepositoryInterface for quotes. Two things to remember: increment IDs are strings (never write the leading zeros as a bare number), and prefer repositories over direct model or ObjectManager loading in production code.
Need this kind of order automation built and maintained properly in your store? You can hire Magento 2 developers from MageAnts to handle custom order processing, integrations, and reporting. Any trouble with the code above? Feel free to contact our Magento experts.