I am facing this weird issue on the checkout page. We are using 3rd party payment module, which redirects customer to the payment page after placing order in Magento. In the controller, it tries to get the orderId from checkoutSession. Sometimes the ID is available in session and sometime it is missing in session.

Sometime it returns this value from checkoutSession:


and sometimes it return below value from checkoutSession:


I am not sure what is the issue. Please let me know, if anyone know about this issue.

FYI, we are using Magento 2.4.2 CE, saving session in DB and frontend in Redis.

1 Answer 1


Try this. In your payment module's return controller, instantiate these two classes in the constructor:

\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool

and in the function where you are retrieving the lastRealOrderId, before the function call, use them to clear/flush the cache:

$types = array('config','layout','block_html','collections','reflection','db_ddl','eav','config_integration','config_integration_api','full_page','translate','config_webservice'); foreach ($types as $type) { $this->_cacheTypeList->cleanType($type); } foreach ($this->_cacheFrontendPool as $cacheFrontend) { $cacheFrontend->getBackend()->clean(); }

It would seem this happens when you have full page cache on. Not sure it will work for you but it's worth a shot. So far it's worked for me. I've yet to still do more testing though.

Courtesy of this article: https://www.tigren.com/blog/magento-2-error-getlastrealorderid-doesnt-work/

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.