I am creating a custom payment method module in magento2.

On order place redirecting to to my request controller from script. In request controller I have below code :

$resultPage = $this->_pageFactory->create();
return $resultPage;

In xml file added template file form.phtml and it is loading on the page.

All are working but I am trying to get last order id in form.phtml file as below :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$checkout_session = $objectManager->get('\Magento\Checkout\Model\Session');
$incrementId = $checkout_session->getLastRealOrderId();
echo $incrementId;

But above code returning nothing. But when I try to log same code in request controller then it returns correct order id.

I have also tried to add a function in block file to get order id in template file like $block->getLastOrder() but this function also returning nothing, while other functions in block file working properly.

Why this is happening? How to get last order id in form.phtml file?

  • It may be caching issue because block and template has been cached. Nov 8, 2017 at 13:35
  • No, I tried to echo some different string every time on form.phtml, and that is showing on page. Nov 8, 2017 at 13:43
  • @YogeshKarodiyais right sometimes it creates an issue because of cache but you can also try to $objectManager->create method instead of $objectManager->get method so it fetches new object for you. This is blind shoot
    – Keyur Shah
    Nov 8, 2017 at 14:28
  • @YogeshKarodiya, Now changes not reflecting on frontend, I tried php bin/magento cache:clean, php bin/magento cache:flush, php bin/magento setup:di:compile, php bin/magento setup:static-content:deploy commands, but still changes not reflecting on frontend. How to remove cache? Nov 9, 2017 at 12:11
  • try to get $incrementId into the controller and pass it to the block from controller. Nov 9, 2017 at 12:14

1 Answer 1

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$checkout_session = $objectManager->get('Magento\Checkout\Model\Session');
$order = $checkout_session->getLastRealOrder();
$orderId= $order->getEntityId();
echo $order->getIncrementId();

It can help you There is a mistake in 2nd line

$checkout_session = $objectManager->get('Magento\Checkout\Model\Session');
  • @Piyush, Actually I posted this via cell phone so it was not well arranged Nov 10, 2017 at 7:18
  • please check my above comments, now changes in form.phtml also not reflecting on production server. Nov 10, 2017 at 7:31
  • i am able to get only the last order id but i need to get all order id
    – Jaisa
    Nov 29, 2017 at 7:44

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.