1

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?

5

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;  
| improve this answer | |
7

You don't want 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

| improve this answer | |
  • 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(). – fantasticrice Aug 19 at 17:03
2

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().

| improve this answer | |
1

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;
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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