PHP Magento为客户提供最后一笔订单

时间:2012-08-31 21:36:45

标签: php magento magento-1.4

我在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());

这似乎是给了我错误客户的总数......而且,还不能弄明白如何让客户最后订购。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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>";