5

I tried the below code to get all the items in that order but am getting only first product please find the code below and tell me what mistake i did.

public function getAllItems()
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId(000000014);
    $orderItems = $order->getAllItems();
    $itemQty = array();
    foreach ($orderItems as $item) {
         $itemQty['quantity'] = $item->getQtyOrdered();
         $itemQty['description'] = $item->getDescription();
         $itemQty['name'] = $item->getName();
         $itemQty['price'] = $item->getPrice();
    }
    return $itemQty;
}

2 Answers 2

9

Create associative array like below.

public function getAllItems()
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId(000000014);
    $orderItems = $order->getAllItems();
    $itemQty = array();
    foreach ($orderItems as $item) {
        $itemQty[]=array('quantity'=>$item->getQtyOrdered(),'description'=>$item->getDescription(),'name'=>$item->getName(),'price'=>$item->getPrice());

    }
    return $itemQty;
}
1
  • yea that would work too
    – Robin
    Nov 15, 2017 at 12:36
4

I think you need an index in the array. Try this

public function getAllItems()
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId(000000014);
    $orderItems = $order->getAllItems();
    $itemQty = array();
    $i = 0;
    foreach ($orderItems as $item) {
         $itemQty[$i]['quantity'] = $item->getQtyOrdered();
         $itemQty[$i]['description'] = $item->getDescription();
         $itemQty[$i]['name'] = $item->getName();
         $itemQty[$i]['price'] = $item->getPrice();
         $i++;
    }
    return $itemQty;
}
1
  • How to get total quantity Nov 26, 2019 at 13:09

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .