0

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();
$resultPage->addHandle('custom_request_index');
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?

asked Nov 8, 2017 at 13:08
6
  • It may be caching issue because block and template has been cached. Commented 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. Commented 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 Commented 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? Commented Nov 9, 2017 at 12:11
  • try to get $incrementId into the controller and pass it to the block from controller. Commented Nov 9, 2017 at 12:14

1 Answer 1

0
$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
5,9249 gold badges35 silver badges67 bronze badges
answered Nov 10, 2017 at 7:00
3
  • @Piyush, Actually I posted this via cell phone so it was not well arranged Commented Nov 10, 2017 at 7:18
  • please check my above comments, now changes in form.phtml also not reflecting on production server. Commented Nov 10, 2017 at 7:31
  • i am able to get only the last order id but i need to get all order id Commented Nov 29, 2017 at 7:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.