12

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

Thanks.

3 Answers 3

18

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();
5
  • Hi, Thanks for your answer. Can you please tell me how can I get the order_increment_id by using 'salesCreditmemoRepositoryV1' in SOAP API. magento.stackexchange.com/questions/127613/… Commented Jul 29, 2016 at 10:54
  • You mean by using <route url="/V1/creditmemo/:id" method="GET">? Commented Jul 29, 2016 at 10:58
  • Please check here: magento.stackexchange.com/questions/127613/… I have added code snippet. What i am trying to do is: First getting all the credit memo list by using 'salesCreditmemoRepositoryV1GetList' after that I am using 'salesCreditmemoRepositoryV1Get' for getting the information about each credit memo by passing ''$creditmemo['incrementId']' . Now the problem is I am not getting the order_increment_id in $fullMemoResult array. Let me know if you need further explanation. Commented Jul 29, 2016 at 11:07
  • In each credit memo you should have order_id also. With it you can do as in my answer :) Commented Jul 29, 2016 at 13:20
  • Hi, But I have this PHP file, out of magento root directory. So Can I do it with above code? Commented Jul 30, 2016 at 5:48
0

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;
}
1
  • 1
    Not a good approach.
    – Avesh Naik
    Commented Sep 20, 2019 at 12:00
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/

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.