12

I need to get order item inside the order loop , how to achieve this, here is my code

      $orderCollection = Mage::getModel("sales/order")->getCollection();





    foreach ($orderCollection as $order) {
        $orders['orders'][] = array(
            'id'            => $order->getIncrementId(),
            'store'         => 'magento',
            'status'        => $order->getStatus(),
            'name'          => $order->getCustomerName(),
            'email'         => $order->getCustomerEmail(),
            'telephone'     => $order->getShippingAddress()->getTelephone(),
            'street'        => $order->getShippingAddress()->getStreet(),
            'pincode'       => $order->getShippingAddress()->getPostcode(),
            'city'          => $order->getShippingAddress()->getCity(),
            'weight'        => $order->getWeight(),


        );




     foreach ($order->getAllItems() as $item) {
        $orders['items'][] = array(
            'id'            => $order->getIncrementId(),
            'name'          => $item->getName(),
            'sku'           => $item->getSku(),
            'Price'         => $item->getPrice(),
            'Ordered Qty'   => $item->getQtyOrdered(),


        );

     }

    }
    return $orders;
}

I need to combine these two in a single array with order and order item displayed in the same array.

3

2 Answers 2

18

Try below code:

foreach ($orderCollection as $order) {
  $items=array();
  foreach ($order->getAllItems() as $item) {
        $items[] = array(
            'id'            => $order->getIncrementId(),
            'name'          => $item->getName(),
            'sku'           => $item->getSku(),
            'Price'         => $item->getPrice(),
            'Ordered Qty'   => $item->getQtyOrdered(),
        );
    }
    $orders['orders'][] = array(
        'id'            => $order->getIncrementId(),
        'store'         => 'magento',
        'status'        => $order->getStatus(),
        'name'          => $order->getCustomerName(),
        'email'         => $order->getCustomerEmail(),
        'telephone'     => $order->getShippingAddress()->getTelephone(),
        'street'        => $order->getShippingAddress()->getStreet(),
        'pincode'       => $order->getShippingAddress()->getPostcode(),
        'city'          => $order->getShippingAddress()->getCity(),
        'weight'        => $order->getWeight(),
        'items'        => $items,
    );
}
5
  • first time the it loads all values correctly but second time it fetch first order item quantity also with second order item quantity Mar 18, 2016 at 9:14
  • update the code my mistake update the answer Mar 18, 2016 at 9:16
  • this code not showing any output for me @QaisarSatti Apr 13, 2020 at 14:50
  • @shan this is creating an array Apr 13, 2020 at 16:34
  • can u answer me on this question..? magento.stackexchange.com/questions/310297/… @QaisarSatti Apr 14, 2020 at 6:39
1

Use below code:

 foreach ($orderCollection as $order) {
        $orders['orders'][] = array(
            'id'            => $order->getIncrementId(),
            'store'         => 'magento',
            'status'        => $order->getStatus(),
            'name'          => $order->getCustomerName(),
            'email'         => $order->getCustomerEmail(),
            'telephone'     => $order->getShippingAddress()->getTelephone(),
            'street'        => $order->getShippingAddress()->getStreet(),
            'pincode'       => $order->getShippingAddress()->getPostcode(),
            'city'          => $order->getShippingAddress()->getCity(),
            'weight'        => $order->getWeight(),


        );    

     foreach ($order->getAllItems() as $item) {
       $orders['orders']['items'][] = array(
            'id'            => $order->getIncrementId(),
            'name'          => $item->getName(),
            'sku'           => $item->getSku(),
            'Price'         => $item->getPrice(),
            'Ordered Qty'   => $item->getQtyOrdered(),


        );

    }
    }
// check array output
echo "<pre>"; print_r($orders);

Hope it will helpful to you.

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.