Question

Can anyone please tell me How to Get order increment id using order id in Magento 2?

Thanks.

Was it helpful?

Solution

You have to pass \Magento\Sales\Api\OrderRepositoryInterface in construct of your class.

protected $orderRepository;

public function __construct(
    ..., 
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
){
    ...
    $this->orderRepository = $orderRepository;
}

Then you can do following:

$order = $this->orderRepository->get($orderId);
$orderIncrementId = $order->getIncrementId();

OTHER TIPS

This may help if you don't want to edit the _construct function:

private static function getIncrementIDfromOrderID($orderID) {
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();
    $tableName = $resource->getTableName('sales_order'); //gives table name with prefix

    //Select Data from table
    $result = $connection->fetchAll("SELECT increment_id FROM " . $tableName . " WHERE entity_id = " . $orderID);
    if (count($result)) {
        return $result[0]['increment_id'];
    }

    return 0;
}

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

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

namespace Adapttive\SalesOrder\Model\Order;

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

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

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

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

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

        return $result;
    }
}

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

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