0

Im in a custom controller tying to get the order info with checkout session. There is my code.

    $order = $this->_checkoutSession->getLastRealOrder(); // Magento\Sales\Model\Order\Interceptor
    $orderId = $order->getEntityId(); // null
    $orderIncrementId = $order->getIncrementId(); // null 

But actually Im getting null on $orderId and $orderIncrementId.

Seems like the order is not being correctly placed.

1
  • Where you put the code is important, the reason is the last order is not persist in checkout_session. I suggest you first check with checkout order success event first.
    – Jimmy
    Commented Apr 6, 2021 at 1:45

2 Answers 2

0

Try this :-

public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        $this->checkoutSession =  $checkoutSession;
        parent::__construct($context, $data);
      } // close bracket
    public function getLastOrder()
    {
        return $this->_checkoutSession->getLastRealOrder()->getEntityId();
    }
0

Try like this:

<?php
    protected $checkoutSession;

    /**
     * Constructor
     *
     * @param Session $checkoutSession
     */
    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    ) {
        $this->checkoutSession = $checkoutSession;
    }

    public function getOrderInfo() {
         $orderId = $this->checkoutSession->getData('last_order_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.