2

I have code for last order detail but I want last 5 orders. For last order detail code is:

$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();
        }

I want last 5 oders detail.

7
  • As per separate customer's last 5 order or all over last 5 order? Commented Nov 2, 2018 at 11:53
  • Last 5 in All orders. Commented Nov 2, 2018 at 11:54
  • Please check my answer. Commented Nov 2, 2018 at 12:01
  • Check my updated answer. Commented Nov 2, 2018 at 12:34
  • Is it working ? Commented Nov 2, 2018 at 12:39

2 Answers 2

1

Use this below code. It maybe helpful for you :

$orderDatamodel = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
$orderDatamodel->getSelect()->limit(5);
    foreach ($orderDatamodel as $key => $value) {
        $order = $objectManager->create('\Magento\Sales\Model\Order')->load($value->getId());
        $orderItems = $order->getAllItems();
        foreach ($orderItems as $item) {
            $product_name = $item->getName();
            $product_id = $item->getProductId();
            echo $product_name . "--" . $product_id;
        }
    }
2
  • Can you give me hole code with loop like item name and items detail Commented Nov 2, 2018 at 12:11
  • $objectManager without using how to get all orders imformation on home page in magento 2? Commented Apr 29, 2019 at 8:51
0

In below code orders has been set in descending order and setPageSize() will give you 5 records only.

$ordercollection = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
$ordercollection->setOrder(
    'created_at',
    'desc'
)
    ->setPageSize(5)
    ->setCurPage(1);

foreach($ordercollection as $order){
    echo $order->getRealOrderId();

    //To get order items
    $items = $order->getAllItems();
    foreach ($items as $item) {
        echo $item->getPrice();
        echo $item->getSku();
    }
}

Notes:

  • Don't use Object Manager.
  • Inject OrderRepository or Interface Class
6
  • Can you give me Inject OrderRepository code for fetch last 5 order item name ? Commented Nov 2, 2018 at 12:14
  • Refer this class github.com/magento/magento2/blob/2.1/app/code/Magento/Sales/Api/…
    – Priyank
    Commented Nov 2, 2018 at 12:27
  • Sorry, I am not that level of a programmer so this link does not solve my answer. Commented Nov 2, 2018 at 12:30
  • So the above answer not working for you to get result?
    – Priyank
    Commented Nov 2, 2018 at 12:30
  • I didn't check I want whole code with loop I want order item name and details Commented Nov 2, 2018 at 12:32

Your Answer

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

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