Question

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?

Was it helpful?

Solution

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;  

OTHER TIPS

You don't need to get the collection of order instead use the order interface Magento\Sales\Api\Data\OrderInterface to get only one product object

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$incrId = "100005363";
$orderInterface = $objectManager->create('Magento\Sales\Api\Data\OrderInterface'); 
$order = $orderInterface->loadByIncrementId($incrId);
$orderId = $order->getId();
echo $orderId;

Use dependency injection instead of objectManager

private $order;

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

public function getOrderId()
{
    $order = $this->order->loadByIncrementId('100005363');
    return $order->getId();
}

Check this for more info: blog.mageprince.com

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->create();
             $criteria->addFilter(OrderInterface::INCREMENT_ID, $incrementId);
             $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().

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;
    }
}

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top