4

I want to get order id by order increment id. I have this code but it is returning empty value:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$incrId = 100005363;
$collection = $objectManager->create('Magento\Sales\Model\Order'); 
$orderInfo = $collection->loadByIncrementId($incrId);
$orderId = $orderInfo->getOrderId();
echo $orderId;

What is the wrong with code or I am doing some wrong approach?

2
  • 3
    Please use, $orderId = $orderInfo->getId(); Commented Apr 18, 2019 at 6:26
  • @ArshadHussain, If answer is helpful please accept and upvote Commented Apr 18, 2019 at 6:36

5 Answers 5

9

Try below Code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$incrId = 100005363;
$collection = $objectManager->create('Magento\Sales\Model\Order'); 
$orderInfo = $collection->loadByIncrementId($incrId);
$orderId = $orderInfo ->getId();
echo $orderId;  
1
11

You don't need to get the collection of order instead use \Magento\Sales\Model\Spi\OrderResourceInterface to get a single order object

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 = '000000001';
        $order = $this->orderFactory->create();
        $this->orderResource->load($order, $incrementId, OrderInterface::INCREMENT_ID);
        return $order;
    }
}
1
  • 3
    Magento\Sales\Api\Data\OrderInterface does not include the loadByIncrementId() method so you are depending on a specific realization from Magento\Sales\Model\Order which wraps the deprecated Magento\Framework\Model\AbstractModel::load(). Commented Aug 19, 2020 at 17:03
10

I would not use any of the current answers as they rely on model’s load() method that has been deprecated since 2.1. Here are two ways you could do it using future-proof approaches:

  1. Use the order repository if you don’t need to load detailed attributes and don’t care about memoization of the order data for later use.

     use Magento\Sales\Api\Data\OrderInterface;
    
     class Example
     {
         protected $orderRepository;
         protected $searchCriteriaBuilder;
    
         public function __construct(
             \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
             \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
         ){
             $this->orderRepository = $orderRepository;
             $this->searchCriteriaBuilder = $searchCriteriaBuilder;
         }
    
         public function getOrderByIncrementId($incrementId): ?OrderInterface
         {
             $criteria = $this->searchCriteriaBuilder
                 ->addFilter(OrderInterface::INCREMENT_ID, $incrementId)
                 ->create();
             $orders = $this->orderRepository->getList($criteria)->getItems();
             return count($orders)? $orders[0] : null;
         }
     }
    
  2. Otherwise, you could use the resource model interface.

     use Magento\Sales\Api\Data\OrderInterface;
    
     class Example
     {
         protected $orderResource;
         protected $orderFactory;
    
         public function __construct(
             \Magento\Sales\Model\Spi\OrderResourceInterface $orderResource,
             \Magento\Sales\Api\Data\OrderInterfaceFactory $orderFactory
         ){
             $this->orderResource = $orderResource;
             $this->orderFactory = $orderFactory;
         }
    
         public function getOrderByIncrementId($incrementId): OrderInterface
         {
             $order = $this->orderFactory->create();
             $this->orderResource->load($order, $incrementId, OrderInterface::INCREMENT_ID);
             return $order;
         }
     }
    

Once you have an instance of the order model, you can get the ID like this: $order->getId().

2

Use below code to get order id by order increment id in Magento 2.

<?php
namespace Package\Module\Model;


use Magento\Sales\Model\OrderFactory;

class Test
{
    /**
     * @var OrderFactory
     */
    protected $orderFactory;

    /**
     * @param \Magento\Sales\Model\OrderFactory $orderFactory
     */
    public function __construct(
        OrderFactory $orderFactory
    ) {
        $this->orderFactory = $orderFactory;
    }

    /**
     * Get order id by increment id
     *
     * @param void
     * @return int $orderId
     */
    public function getOrder($incrementId)
    {
        $orderModel = $this->orderFactory->create();
        $order = $orderModel->loadByIncrementId($incrementId);
        $orderId = $order->getId();
        return $orderId;
    }
}
0

The best (optimized) way to get order id i.e sales_order.entity_id from increment id by creating a custom DB query in Magento2.

  1. Create a GetOrderIdByIncrementId class:
<?php
declare(strict_types =1);

namespace Adapttive\SalesOrder\Model\Order;

use Exception;
use Magento\Sales\Model\ResourceModel\Order as OrderResource;

/**
 * Class GetOrderIdByIncrementId: To get order.entity_id by order.increment_id
 * Reference from \Magento\Sales\Model\OrderIncrementIdChecker
 */
class GetOrderIdByIncrementId
{
    /**
     * @var OrderResource
     */
    private $resource;

    /**
     * @param OrderResource $resource
     */
    public function __construct(OrderResource $resource)
    {
        $this->resource = $resource;
    }

    /**
     * Get order id by order increment id.
     *
     * @param string|int $incrementId
     * @return int
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function get($incrementId): int
    {
        $result = 0;

        try {
            /** @var  \Magento\Framework\DB\Adapter\AdapterInterface $adapter */
            $adapter = $this->resource->getConnection();
            $bind = [':increment_id' => $incrementId];
            /** @var \Magento\Framework\DB\Select $select */
            $select = $adapter->select();
            $select->from($this->resource->getMainTable(), $this->resource->getIdFieldName())
                ->where('increment_id = :increment_id');
            $entityId = $adapter->fetchOne($select, $bind);
            if ($entityId > 0) {
                $result = (int)$entityId;
            }
        } catch (Exception $e) {
            $result = 0;
        }

        return $result;
    }
}

  1. Usage:
$orderId = $this->getOrderIdByIncrementId->get($orderIncrementId)

Reference: https://adapttive.com/blog/magento-2-get-order-id-by-increment-id/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.