获取订单内的订单商品


10

我需要在订单循环中获取订单商品,如何实现这一点,这是我的代码

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

我需要将这两者结合在一个数组中,并在同一数组中显示订单和订单项。


如果我有2个订单数据,如何遍历foreach循环
user75563 '18

这不能为问题提供答案。一旦您拥有足够的声誉,您就可以在任何帖子中发表评论;相反,提供不需要澄清从提问者的回答。- 评分
埃西姆Goriya

如果您有新问题,请单击“ 提问”按钮提问。如果它有助于提供上下文,请包括指向该问题的链接。- 评分
Murtuza Zabuawala

Answers:


16

试试下面的代码:

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

第一次,它正确加载了所有值,但是第二次,它也以第二个订单项的数量获取了第一订单项的数量
vellai durai

更新代码我的错误更新答案
Qaisar Satti

此代码对我不显示任何输出@QaisarSatti
Shan Haider

@shan,这正在创建一个数组
Qaisar Satti,

你能回答我这个问题吗?magento.stackexchange.com/questions/310297/... @QaisarSatti
山海德尔

1

使用以下代码:

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

希望对您有帮助。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.