-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']);

        }
    }
-1

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 ?

| improve this answer | |
-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 ...

| improve this answer | |
  • 1
    Thanks i will try this and get back to you Thanks for response. :) – Inzamam Waikar May 10 '19 at 18:15
  • Thanks @Rk Rathod ... – sv3n May 10 '19 at 23:44
  • @InzamamWaikar did this work for you? – sv3n May 13 '19 at 18:56
  • Yes somewhat it's working, but as per my code I need to do some modifications. Thanks for helping 😊 – Inzamam Waikar May 13 '19 at 19:35
  • @InzamamWaikar if it was still usefull, feel free to upvote/accept. :) – sv3n May 14 '19 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.