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?
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 arrangedHimanshu Pareta– Himanshu Pareta2017年11月10日 07:18:28 +00:00Commented Nov 10, 2017 at 7:18
-
please check my above comments, now changes in form.phtml also not reflecting on production server.Vinaya Maheshwari– Vinaya Maheshwari2017年11月10日 07:31:02 +00:00Commented Nov 10, 2017 at 7:31
-
i am able to get only the last order id but i need to get all order idJaisa– Jaisa2017年11月29日 07:44:03 +00:00Commented Nov 29, 2017 at 7:44
$objectManager->createmethod instead of$objectManager->getmethod so it fetches new object for you. This is blind shootphp bin/magento cache:clean,php bin/magento cache:flush,php bin/magento setup:di:compile,php bin/magento setup:static-content:deploycommands, but still changes not reflecting on frontend. How to remove cache?