Magento 1.9 – Get Customer Order Count for All Customers

collection;magento-1.9

I Tried this and it is working but i can only do for a specific customer but i need this for all customer.

<?php
$customer_id = 5;
$_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_id',$customer_id);                        
$_orderCnt = $_orders->count(); //orders count
echo 'Customer with ID '.$customer_id.' has '.$_orderCnt.' orders';
?>

Best Answer

Use below code:

<?php
   $customerCollection = Mage::getModel('customer/customer')->getCollection()
      ->addAttributeToSelect('entity_id');

   foreach($customerCollection as $customer){
      $_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_id',$customer->getId());                        
      $_orderCnt = $_orders->count(); //orders count
      echo 'Customer with ID '.$customer_id.' has '.$_orderCnt.' orders';
   }
?>
Related Topic