Magento 2 Get Order Entity id from the Order increment id using programmatically by the given solutions in this article.
Many times you required Order Entity id instead of Order increment id while developing functionality related to Order. The order Entity id is the Primary key of the Order Entity from the Sales_order table.
When you want to get Order details by order repository interface using get($id) method, you must need to pass order entity id as a parameter for order id.
While in the real scenario, You have only order increment id but you don’t have order entity id, in this case, you need to follow below solutions to get Order entity id.
<?php namespace Jesadiya\OrderId\Block; use Exception; use Psr\Log\LoggerInterface; use Magento\Framework\View\Element\Template; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Framework\View\Element\Template\Context; class OrderIdByIncrementId extends Template { /** * @var SearchCriteriaBuilder */ protected $searchCriteriaBuilder; /** * @var OrderRepositoryInterface */ private $orderRepository; /** * @var LoggerInterface */ private $logger; /** * @var array */ private $data; /** * @param Context $context * @param SearchCriteriaBuilder $searchCriteriaBuilder * @param LoggerInterface $logger * @param OrderRepositoryInterface $orderRepository * @param array $data */ public function __construct( Context $context, SearchCriteriaBuilder $searchCriteriaBuilder, OrderRepositoryInterface $orderRepository, LoggerInterface $logger, array $data = [] ) { $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->orderRepository = $orderRepository; $this->logger = $logger; parent::__construct($context,$data); } public function getOrderEntityID() { $orderId = null; try { $incrementId = '1000000010'; $searchCriteria = $this->searchCriteriaBuilder ->addFilter('increment_id', $incrementId)->create(); $orderData = $this->orderRepository->getList($searchCriteria)->getItems(); foreach ($orderData as $order) { $orderId = $order->getId(); } } catch (Exception $exception) { $this->logger->error($exception->getMessage()); } return $orderId; } }
Get Order Entity id using Increment id is just a simple by using the SearchCriteria Builder filter.
Output:
Specific Order Entity Id.