我怎么能得到我买的所有产品

时间:2014-10-29 11:29:39

标签: magento

我尝试编写此代码

public function allsalesblock(){
    echo 'other block ';
    $orders = Mage::getResourceModel('sales/order_collection')
        ->addFieldToSelect('*')
        ->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getId());
        ->addAttributeToSort('created_at', 'DESC');


    $order = Mage::getModel("sales/order")->load($orders); //load order by order id
    $ordered_items = $order->getAllItems();
    foreach($ordered_items as $item){
        echo $item->getItemId(); //product id
        echo $item->getSku();
        echo $item->getQtyOrdered(); //ordered qty of item
        echo $item->getName();
    }
  }

但这不起作用 - 我看到白屏。我在这里找到了这段代码(和修改版)How can I display all products bought by a customer in magento?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码

来获取它
<?php

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();



$email = 'albert@example.com';
$_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email',$email);                       


foreach ($_orders as $order) {
    $id[]= $order->getId();
}

//print_r($id);

foreach ($id as $order_id) {
$order = Mage::getModel('sales/order')->load($order_id);
#get all items
$items = $order->getAllItems();
$itemcount= count($items);
$data = array();
$i=0;
#loop for all order items
foreach ($items as $itemId => $item)
{
    $data[$i]['name'] = $item->getName();
    $data[$i]['price'] = $item->getPrice();
    $data[$i]['sku'] = $item->getSku();
    $data[$i]['id'] = $item->getProductId();
    $data[$i]['qty'] = $item->getQtyToInvoice();
}
#displaying the array in order to see the products
echo '<pre/>';print_r($data);

}

此处更改电子邮件地址,因为您希望获得该客户购买的产品,

$email = 'albert@example.com';