2

This question already has an answer here:

I have the total number of orders placed by the customer. Now i want to get the orders between the particular range of time,like i want to get all the orders placed in last year. I am adding the code i used for getting total no of order

$_customerCollection = Mage::getModel('customer/customer')
        ->getCollection()->addAttributeToSelect('*');

foreach($_customerCollection as $customer)
{
 $customerTotals = Mage::getResourceModel('sales/sale_collection')
     ->setCustomerFilter($customer)
     ->load()
     ->getTotals();

    $customerNumberOfOrders = $customerTotals->getNumOrders();
}

marked as duplicate by Community May 31 at 7:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Use the filter to the collection to get records between dates.

Try with below way.

$date = (new \DateTime());
$_customerCollection = Mage::getModel('customer/customer')
        ->getCollection()->addAttributeToSelect('*');

foreach($_customerCollection as $customer)
{
 $customerTotals = Mage::getResourceModel('sales/sale_collection')
     ->setCustomerFilter($customer)
     ->addFilter('created_at', $date->format('Y-m-d 00:00:00'), 'gteq')
     ->addFilter('created_at', $date->format('Y-m-d 23:59:59'), 'lteq')
     ->load()
     ->getTotals();

    $customerNumberOfOrders = $customerTotals->getNumOrders();
}

Note : Above code is not tested I just share logic you have to change as per your requirement.

I hope it helps!

  • Code is working but it is not returning correct value. It returns zero for all customer no matter the date range – surbhi agr May 29 at 10:03
  • Print SQL query and check query from & to date is correct and run this query in database. – Chirag Patel May 29 at 10:13
  • Make sure you have passed correctly date. – Chirag Patel May 29 at 10:16
  • The date is correctly passed – surbhi agr May 29 at 10:27
  • print sql query and check. $products->getSelect(); – Chirag Patel May 29 at 10:28
1

I resolved the problem by using this:

$fromDate = date('2016-01-01 00:00:00', strtotime($fromDate));
$toDate = date('2016-12-31 23:59:59', strtotime($toDate));


$_customerCollection = Mage::getModel('customer/customer')
        ->getCollection()->addAttributeToSelect('*');

foreach($_customerCollection as $customer)
{

$customerOrders = Mage::getResourceModel('sales/sale_collection')
->setCustomerFilter($customer)
->addFieldToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate))
->load()
->getTotals();

$customerCheckoutFrequency = $customerOrders->getNumOrders();
}
1

Try this code, It will help you


$_customerCollection = Mage::getModel('customer/customer')
        ->getCollection()->addAttributeToSelect('entity_id');

foreach($_customerCollection as $customer)
{
    $customerTotals = Mage::getModel('sales/order')->getCollection()
       ->addAttributeToFilter('customer_id',$customer->getId());
        $fromDate = date('2018-01-01 00:00:00');
        $toDate = date('2018-12-31 23:59:59');
        $customerTotals->addFieldToFilter('created_at', array(
        'from' => $fromDate,
        'to' => $toDate,
        'date' => true,
        ))->load();
    echo $customerNumberOfOrders = count($customerTotals);
}

Let me know,If it help you..

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