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;
}
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;
}
| improve this answer | |
  • yea that would work too – Robin Nov 15 '17 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;
}
| improve this answer | |
  • How to get total quantity – jibin george Nov 26 '19 at 13:09

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.