2012-08-31 3 views
2

Magento 상점 루트 폴더 (1.4) 외부에서 작업 중이며 고객 주문 총계 및 마지막 주문 날짜를 얻고 싶습니다. 나는이와 함께 시작 :PHP Magento가 고객에게 마지막 주문 총계를 얻습니다.

$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

주어진 고객의 총 주문 수, 총 판매 수 및 최근 주문 수를 제공합니다.

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

드류,이 굉장합니다 ...

이 코드는 조금 짧아 질 수 있지만, 다음과 같이 명확하게 그 것이다! 마지막 주문 날짜를 가져 오는 방법이 있습니까? – simian

+0

$ newestOrder-> getData ('created_at')를 사용했으나 계정 생성일이 맞는지 확실하지 않습니다. – simian

+2

예, $ newestOrder-> getData ('created_at')는 주문이 생성 된 날짜를 알려줍니다 –