Latest news

[Fix It Series] Magento 2 Error: getLastRealOrderId Doesn’t Work After Checkout With Enabled Page Cache

No comments

PROBLEM:

Creating a payment gateway module in order to redirect to the gateway page after checkout. However, while page cache is enabled the method for getting last real order id returns null (it is used to get order id from session).

So, how to use the module without disabling page cache?

SOLUTIONS:

There are 2 simple steps to handle this Magento 2 error:

  • Step 1: Defining the constructor – pass Magento\Framework\App\Cache\TypeListInterface and Magento\Framework\App\Cache\Frontend\Pool to your file’s constructor as the following:

public function __construct(
    Context $context,
    \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
    \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
) {
    parent::__construct($context);
    $this->_cacheTypeList = $cacheTypeList;
    $this->_cacheFrontendPool = $cacheFrontendPool;
}
  • Step 2: Add the following code to the method where you want clear/ flush 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();

We have shown you 2 quick steps to deal with the Magento 2 error: getLastRealOrderId doesn’t work after checkout with enabled page cache. If you have any difficulties when follow our instruction, be free to leave the comment below.

See More Magento Tutorials:

[Fix It Series] Front Controller Reached 100 router Match Iterations

[Fix It Series] Magento Error: Invalid Form Key. Please refresh the page

[Fix It Series] Total Price In Wishlist Page Is $0.00 In Magento 2

[Fix It Series] Magento Error: Custom Option Type Fields Are Not Displayed

[Hot Release] Magento Progressive Web Apps – Free Download

1 Star2 Stars3 Stars4 Stars5 Stars (43 votes, average: 1.09 out of 5)

Loading...
April Lee[Fix It Series] Magento 2 Error: getLastRealOrderId Doesn’t Work After Checkout With Enabled Page Cache

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *