TransWikia.com

Get order id by order increment id in Magento 2

Magento Asked by Arshad Hussain on December 9, 2021

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

$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$incrId = 100005363;
$collection = $objectManager->create('MagentoSalesModelOrder'); 
$orderInfo = $collection->loadByIncrementId($incrId);
$orderId = $orderInfo->getOrderId();
echo $orderId;

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

5 Answers

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

use Exception;
use MagentoSalesModelResourceModelOrder as OrderResource;

/**
 * Class GetOrderIdByIncrementId: To get order.entity_id by order.increment_id
 * Reference from MagentoSalesModelOrderIncrementIdChecker
 */
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 MagentoFrameworkExceptionLocalizedException
     */
    public function get($incrementId): int
    {
        $result = 0;

        try {
            /** @var  MagentoFrameworkDBAdapterAdapterInterface $adapter */
            $adapter = $this->resource->getConnection();
            $bind = [':increment_id' => $incrementId];
            /** @var MagentoFrameworkDBSelect $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/

Answered by Milind Singh on December 9, 2021

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 MagentoSalesApiDataOrderInterface;
    
     class Example
     {
         protected $orderRepository;
         protected $searchCriteriaBuilder;
    
         public function __construct(
             MagentoSalesApiOrderRepositoryInterface $orderRepository,
             MagentoFrameworkApiSearchCriteriaBuilder $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 MagentoSalesApiDataOrderInterface;
    
     class Example
     {
         protected $orderResource;
         protected $orderFactory;
    
         public function __construct(
             MagentoSalesModelSpiOrderResourceInterface $orderResource,
             MagentoSalesApiDataOrderInterfaceFactory $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().

Answered by fantasticrice on December 9, 2021

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

<?php
namespace PackageModuleModel;


use MagentoSalesModelOrderFactory;

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

    /**
     * @param MagentoSalesModelOrderFactory $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;
    }
}

Answered by Pandurang on December 9, 2021

You don't need to get the collection of order instead use the order interface MagentoSalesApiDataOrderInterface to get only one product object

$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$incrId = "100005363";
$orderInterface = $objectManager->create('MagentoSalesApiDataOrderInterface'); 
$order = $orderInterface->loadByIncrementId($incrId);
$orderId = $order->getId();
echo $orderId;

Use dependency injection instead of objectManager

private $order;

public function __construct(
    ...
    MagentoSalesApiDataOrderInterface $order,
    ...
) {
    $this->order = $order;
}

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

Check this for more info: blog.mageprince.com

Answered by Prince Patel on December 9, 2021

Try below Code

$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$incrId = 100005363;
$collection = $objectManager->create('MagentoSalesModelOrder'); 
$orderInfo = $collection->loadByIncrementId($incrId);
$orderId = $orderInfo ->getId();
echo $orderId;  

Answered by Sanjay Gohil on December 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP