cancel
Showing results for 
Search instead for 
Did you mean: 

how to use getLastRealOrder()->getEntityId(); in .phtml file magento 2

SOLVED

how to use getLastRealOrder()->getEntityId(); in .phtml file magento 2

Hi guys , how can i use  getLastRealOrder()->getEntityId(); in .phtml file.

can any one guide me ?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: how to use getLastRealOrder()->getEntityId(); in .phtml file magento 2

You can use this way by Creating Block file,

 

<?php
namespace Rbj\Checkout\Block;

class Success extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        $this->checkoutSession =  $checkoutSession;
        parent::__construct($context, $data);

    public function getLastOrder()
    {
        return $this->_checkoutSession->getLastRealOrder()->getEntityId();
    }
}

Call Block function in a template file,

echo $block->getLastOrder();

 

Directly template file, Check using below file, I am not sure its working or not,

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

 

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

2 REPLIES 2

Re: how to use getLastRealOrder()->getEntityId(); in .phtml file magento 2

Hello @bharath553

 

In which module phtml file you want to call?

Manish Mittal
https://www.manishmittal.com/

Re: how to use getLastRealOrder()->getEntityId(); in .phtml file magento 2

You can use this way by Creating Block file,

 

<?php
namespace Rbj\Checkout\Block;

class Success extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        $this->checkoutSession =  $checkoutSession;
        parent::__construct($context, $data);

    public function getLastOrder()
    {
        return $this->_checkoutSession->getLastRealOrder()->getEntityId();
    }
}

Call Block function in a template file,

echo $block->getLastOrder();

 

Directly template file, Check using below file, I am not sure its working or not,

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

 

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial