6

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

Thanks.

11

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();
  • 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/… – Magento Team Jul 29 '16 at 10:54
  • You mean by using <route url="/V1/creditmemo/:id" method="GET">? – Bartłomiej Szubert Jul 29 '16 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. – Magento Team Jul 29 '16 at 11:07
  • In each credit memo you should have order_id also. With it you can do as in my answer :) – Bartłomiej Szubert Jul 29 '16 at 13:20
  • Hi, But I have this PHP file, out of magento root directory. So Can I do it with above code? – Magento Team Jul 30 '16 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;
}
  • Not a good approach. – Avesh Naik Sep 20 at 12:00

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.