9

I know how to retrieve last order id by this code,

$this->_checkoutSession->getQuote()->getReservedOrderId();

Where _checkoutSession is obtained with the injection,

\Magento\Checkout\Model\Session $checkoutSession

I want to know how to get the current order ID in magento 2.

2
  • At which place you want current order id? Apr 12, 2017 at 5:06
  • After the order is placed @Nalin. I am observing order placed after event. Apr 12, 2017 at 5:07

1 Answer 1

12

below code will give you last order id

protected $_checkoutSession;


public function __construct(
 .....
\Magento\Checkout\Model\Session $checkoutSession,

.....
) {

        $this->_checkoutSession = $checkoutSession;


    }

 $order = $this->_checkoutSession->getLastRealOrder();
 $orderId=$order->getEntityId();
 $order->getIncrementId();

Reference

4
  • ++1 from me, and how to get the increment id, insted of entity id ? Apr 12, 2017 at 5:09
  • 2
    ummm.. ! i guess $order->getIncrementId() should work Apr 12, 2017 at 5:12
  • Okay saif I will check.. Apr 12, 2017 at 5:12
  • 1
    @DishaSingh i updated the code.. Apr 12, 2017 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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