0

I need to get last order details in a custom phtml page

0
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection()->getLastItem();
$orderId   =   $orderDatamodel->getId();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
$orderItems = $order->getAllItems();

foreach ($orderItems as $item) {
   $product_name=   $item->getName();
   $product_id=   $item->getProductId();
}
  • This is not a good idea, because (in theory) a different order could be placed at the same time (for example from another customer, or through the admin interface), and therefore getLastItem() might not return the correct order. Again, this is just theoretical, but I think retrieving the order from the session is more safe. – Niels Oct 16 '17 at 14:39
0

You can get it from the session:

protected $checkoutSession;
protected $orderFactory;
protected $scopeConfig;

public function __construct(
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Sales\Model\OrderFactory $orderFactory,
    \Magento\Framework\View\Element\Context $context
) {
    $this->checkoutSession = $checkoutSession;
    $this->orderFactory = $orderFactory;
    $this->scopeConfig = $context->getScopeConfig();
}


// Use this method to get ID    
public function getRealOrderId()
{
    $lastorderId = $this->checkoutSession->getLastOrderId();
    return $lastorderId;
}

public function getOrder()
{
    if ($this->_checkoutSession->getLastRealOrderId()) {
         $order = $this->orderFactory->create()->loadByIncrementId($this->checkoutSession->getLastRealOrderId());
         return $order;
    }
}
  • I already used this code. But i didn't get the result in home page...can u tell me why – Rita Jose Oct 17 '17 at 3:59
  • how to Load latest 5 order with the home page in magento 2 ? – sanni kalariya Apr 15 at 7:00
0

Use this code in your block class or helper:

    protected $_checkoutSession;
    protected $_orderFactory;
    protected $_scopeConfig;

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Framework\View\Element\Context $context
    ) {
        $this->_checkoutSession = $checkoutSession;
        $this->_orderFactory = $orderFactory;
        $this->_scopeConfig = $context->getScopeConfig();
    }


    // Use this method to get ID    
    public function getRealOrderId()
    {
        $lastorderId = $this->_checkoutSession->getLastOrderId();
        return $lastorderId;
    }

    public function getOrder()
    {
        if ($this->_checkoutSession->getLastRealOrderId()) {
             $order = $this->_orderFactory->create()->loadByIncrementId($this->_checkoutSession->getLastRealOrderId());
        return $order;
        }
        return false;
    }

Using this function, you can get the latest order Id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.