2

我在 magento 商店根文件夹(1.4)之外工作,我想获得客户的订单总额和最后订单的日期。我从这个开始:

$customer = Mage::getModel('customer/customer')->load($entity_id);
         $customerTotals =Mage::getResourceModel('sales/sale_collection')
              ->setCustomerFilter($customer)
              ->load()
              ->getTotals();

         echo $customerTotals->getNumOrders();
         echo money_format('$%i',$customerTotals->getLifetime());

这似乎给了我来自错误客户的总数......而且,不能完全弄清楚如何在最后一个订单日获得客户。

任何帮助,将不胜感激。

4

1 回答 1

4

这将起作用,为您提供给定客户的订单总数、总销售额和最近的订单。

代码可以缩短一点,但为了清楚起见,如下...

$customerId = 2;
$orderCollection = Mage::getModel('sales/order')->getCollection()
    ->addFilter('customer_id', $customerId)
    ->setOrder('created_at', Varien_Data_Collection_Db::SORT_ORDER_DESC)
;
$numberOfOrders = $orderCollection->count();
$newestOrder = $orderCollection->getFirstItem();

$orderCollection->clear()->getSelect()
    ->columns(array('total_sales'=>'SUM(main_table.base_grand_total)'))
    ->group('customer_id')
;
$totalSales = $orderCollection->getFirstItem()
    ->getData('total_sales');

//Some output to confirm...
echo "<p>" . $numberOfOrders . "</p>";
echo "<p>" . Mage::helper('core')->currency($totalSales, true, false) ."</p>";
echo "<p>" . $newestOrder->getData('increment_id') ."</p>";
于 2012-09-01T19:56:42.827 回答