【问题标题】:PHP Magento get customers last order totalPHP Magento 获取客户最后的订单总数
【发布时间】:2012-08-31 21:36:44
【问题描述】:

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

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

任何帮助将不胜感激。

【问题讨论】:

    标签: php magento magento-1.4


    【解决方案1】:

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

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

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

    【讨论】:

    • 德鲁,这太棒了!!有什么方法可以同时提取最后一个订单的日期?
    • 我使用了 $newestOrder->getData('created_at') 但不确定这是否是帐户创建日期
    • 是的,$newestOrder->getData('created_at') 会给你订单的创建日期
    猜你喜欢
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 2023-01-21
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多