-1

I want customers having 0 orders and there count

I have done for order count of each customer but I want count of customer having 0 orders

$CustomersCollection = Mage::getModel('customer/customer')->getCollection();
    foreach ($CustomersCollection->getData() as $customer) {
        $collection = Mage::getModel('sales/order')->getCollection()
            ->addAttributeToFilter('customer_id', array('eq' => $customer['entity_id']));
    //    var_dump(count($collection->getData()));
        if(!count($collection->getData())) {
            var_dump(count($collection->getData()) . "----" . $customer['entity_id']);

        }
    }
sv3n
  • 11,657
  • 7
  • 40
  • 73

2 Answers2

0

I found solution within some seconds I have used counter for it.

$CustomersCollection = Mage::getModel('customer/customer')->getCollection();
$i=0;
foreach ($CustomersCollection->getData() as $customer) {
    $collection = Mage::getModel('sales/order')->getCollection()
        ->addAttributeToFilter('customer_id', array('eq' => $customer['entity_id']));
//    var_dump(count($collection->getData()));
    if(!count($collection->getData())) {
        var_dump(count($collection->getData()) . "----" . $customer['entity_id']);
        $i++;
    }
}
var_dump($i);

Is there any other way to find it tell me ?

-1

Instead loading order collection for each customer you can try this ...

/** @var Mage_Customer_Model_Resource_Customer_Collection $collection */
$collection = Mage::getResourceModel('customer/customer_collection');
$collection->joinTable(
    ['order' => $collection->getTable('sales/order')],
    'customer_id = entity_id',
    ['customer_id' => 'customer_id'],
    null,
    'left'
);
$collection->getSelect()->where('order.customer_id IS NULL');

$count = $collection->getSize();


SELECT `e`.*, `order`.`customer_id` FROM `prefix_customer_entity` AS `e` LEFT JOIN `prefix_sales_flat_order` AS `order` ON (order.customer_id=e.entity_id) WHERE (`e`.`entity_type_id` = '1') AND (order.customer_id IS NULL)

Edit: if you downvote, please let me know why ... i'll try to improve ...

sv3n
  • 11,657
  • 7
  • 40
  • 73