无法在Magento中获取订单的所有商品?

时间:2013-03-15 12:25:26

标签: php magento

我正在module开发一个magento,其中我在页面上显示所有订单以及每个订单,我正在显示完整的订单信息,包括针对该订单购买的产品。我能够显示所有订单,但我不明白,其中有多个产品的订单只是显示单个产品,当我在销售订单中检查这是Sales->订单时,那么超过1个产品是在那里展示。

这是我的代码:

public function indexAction()
{
    $model = Mage::getModel('sales/order');
    $product_model = Mage::getModel('catalog/product');

    $collection = $model->getCollection()
    ->addFieldToFilter('status', array("in" => array('complete','closed','pending','holded','payment_review','pending_payment','pending_paypal','processing')));

    $data = array();
    $orderArr = array();

    $records = 0;
    foreach($collection as $order)
    {
        $data[$records]['order_data']['shipping_address'] = $order->getShippingAddress()->getData(); // get shipping details
        $data[$records]['order_data']['billing_address'] = $order->getBillingAddress()->getData(); // get billing details

        $data[$records]['order_data']['order_total'] = $order->getGrandTotal(); // get total amount
        $items_obj = $model->loadByIncrementId($order['increment_id']); // get all items
        $items = $items_obj->getAllItems();

        $data[$records]['order_data']['order_id'] = $order['increment_id']; 

        $i = 0;
        $productData = array();
        $orderProductArr = array();
        foreach($items as $itemId => $item)
        {  
            $_product = $product_model->load($item->getProductId()); // getting product details here to get description

            $taxClassId = $_product->getData("tax_class_id");
            $taxClass = $_product->getData("tax_class_name");

            $taxRate = $order['tax_amount'];

            $orderProductArr[$i]['web_id'] = $item->getProductId();
            $orderProductArr[$i]['quantity'] = $item->getQtyToInvoice();
            $orderProductArr[$i]['price'] = $item->getPrice();
            $orderProductArr[$i]['description'] = $_product->getDescription();
            $orderProductArr[$i]['currency'] = $order['order_currency_code'];
            $orderProductArr[$i]['tax_id'] = $taxClassId;
            $orderProductArr[$i]['tax_amt'] = $taxRate;
            $orderProductArr[$i]['total_amt'] = ($item->getPrice()*$item->getQtyToInvoice())+($taxRate);

            $productData[$i]['title'] = $item->getName();
            $productData[$i]['web_product_id'] = $item->getProductId();
            $productData[$i]['price'] = $item->getPrice();
            $productData[$i]['product_sku'] = $item->getSku();
            $productData[$i]['tax_class'] = $taxClassId;
            $productData[$i]['description'] = $_product->getDescription();

            $tax_arr[$i]['tax_id'] = $taxClassId;
            $tax_arr[$i]['tax_class'] = $taxClassId;
            $tax_arr[$i]['tax_amt'] = $taxRate;

            $i++;
        }

        $data[$records]['order_data']['product_details'] = $orderProductArr;
        $data[$records]['order_data']['order_product_details'] = $productData;
        $data[$records]['order_data']['tax_arr'] = $tax_arr;
        $data[$records]['order_data']['shipping_amount'] = $order->getShippingAmount();
        $data[$records]['order_data']['order_details'] = $order->toArray();
        unset($orderProductArr);
        unset($productData);
        $records++;
    }

     echo "<pre>";
     print_r($data);
     echo "</pre>";

}

有人可以帮我解决这个问题吗?

任何帮助都将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:1)

我在我的模块中使用过它。它可能对你有所帮助。

加载磁力订单和产品系列

$resource_model = Mage::getResourceModel('sales/order_collection');
$product_model = Mage::getModel('catalog/product');

循环收集订单

    $records = 0;
    $productData = array();
    $orderProductArr = array();

    foreach($resource_model as $all_orders)
    {  
        if($all_orders['status']!="canceled" && $all_orders['status']!="fraud")
        {   
            $i = 0;
            $items = $all_orders->getAllVisibleItems();

            foreach($items as $item)
            {
                $_product = $product_model->load($item->getProductId()); // getting product details here to get description

                $taxClassId = $_product->getData("tax_class_id");
                $taxClass = $_product->getData("tax_class_name");

                $taxRate = $order['tax_amount'];

                $orderProductArr[$i]['web_id'] = $item->getProductId();
                $orderProductArr[$i]['quantity'] = $item->getQtyToInvoice();
                $orderProductArr[$i]['price'] = $item->getPrice();
                $orderProductArr[$i]['description'] = $_product->getDescription();
                $orderProductArr[$i]['currency'] = $order['order_currency_code'];
                $orderProductArr[$i]['tax_id'] = $taxClassId;
                $orderProductArr[$i]['tax_amt'] = $taxRate;
                $orderProductArr[$i]['total_amt'] = ($item->getPrice()*$item->getQtyToInvoice())+($taxRate);

                $productData[$i]['title'] = $item->getName();
                $productData[$i]['web_product_id'] = $item->getProductId();
                $productData[$i]['price'] = $item->getPrice();
                $productData[$i]['product_sku'] = $item->getSku();
                $productData[$i]['tax_class'] = $taxClassId;
                $productData[$i]['description'] = $_product->getDescription();

                $tax_arr[$i]['tax_id'] = $taxClassId;
                $tax_arr[$i]['tax_class'] = $taxClassId;
                $tax_arr[$i]['tax_amt'] = $taxRate;

                $i++;
                unset($items);
            }    
            $data[$records]['order_data']['product_details'] = $orderProductArr;
            $data[$records]['order_data']['order_product_details'] = $productData;
            $data[$records]['order_data']['tax_arr'] = $tax_arr;
            unset($orderProductArr);
            unset($productData);
            unset($tax_arr);
            $records++;
        }    
    }