1

How to Get the latest 5 order in home page in Magento2 with all order details?

Any help would be appreciated.

0

Try this,

Create a helper and add the below code to it,

Helper path be like

app/code/Vendor/Module/Helper/Data.php

<?php
namespace Vendor\Module\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function __construct(
    \Magento\Sales\Model\OrderFactory $orderFactory
) {
    $this->orderFactory = $orderFactory;
}
public function getCollection()
{
    $collection = $this->orderFactory->create()->getCollection()->setOrder('entity_id', 'DESC');
    $collection->setPageSize(5)->setCurPage(1);
    return $collection;
}
}

then in your phtml you can call like below

$order = $this->helper('Vendor\Module\Helper\Data')->getCollection();
foreach($order as $items){
    echo "Entity Id :". $items->getEntityId();
    echo "Order Status :". $items->getStatus();
}

you can call any of the phtml in the home page to get this information.

To call a phtml only on home page follow the below steps

create a cms_index_index.xml in the below path

app/code/Vendor/Module/view/frontend/layout/cms_index_index.xml

then add the below code to it

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
   <referenceContainer name="content">
        <block class="Magento\Framework\View\Element\Template"  name="sales_total" template="Vendor_Module::total.phtml">
        </block>
    </referenceContainer>
</body>
</page>

Vendor_Module - this should you namespace_modulename

And also place total.phtml in the below path

app/code/Vendor/Module/view/frontend/templates/total.phtml

Hope this helps.

  • Thanks help to me Prathap .. but I have add echo "Price : ".$items->getPrice(); not show me price ? – sanni kalariya Apr 15 at 7:53
  • You can use it like $items->getSubtotal(); and for more information about attributes see sales_order table and you can get all the information from that table using above method – Prathap Gunasekaran Apr 15 at 7:59
  • If you want a link in for order status means do it like this <a href="order detail page url"><?php echo "Order Status :". $items->getStatus(); ?></a> inside foreach and for more information create a separte question and let others answer to it – Prathap Gunasekaran Apr 15 at 8:41
  • ok fine details show but I have get only login customer details what to do ?using something y "Magento\Customer\Model\Session" object and constructor define how to do? – sanni kalariya Apr 18 at 9:47
1

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;
}

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.