search
top

Get customer order count in Magento

Hello All,

I am writing here a script snippet for getting the orders count for given 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';
?>

12 Responses to “Get customer order count in Magento”

  1. jaimin says:

    if i want to check this for all customer id’s than how can i?

  2. Vladimir says:

    You get all orders collection just for a count number.
    Amazing!

  3. Bijal says:

    hi
    how do I use this piece of code to retrieve the order count and put it on the order email sent out. The order email has the Order ID and next to it I’d like to put the order count for that customer i.e.

    Customer Name: John Smith
    Order ID: 0000000000012340
    Order Count: 8 ( this would include this particular order and the 7 before this)

    Thanks guys.

    • Decrypt Web says:

      You need to customize the email functionality for the same, the point where the email template HTML is generated dynamically.

      I am curious about guest customer, though.

      • Bijal says:

        Will have to modify the code snippet to either show this only for registered user or will need to look for all orders by that particular email address

        its trick to say the least… currently we do not allow guest checkout… but we’re working towards that

        thanks for help

        • Decrypt Web says:
          $customer_email = 'abc@abc.com';
          $_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email',$customer_email);                
          

          Use the above code, it will work for registered as well as guest.

  4. Kasmin says:

    Thanks, this code is very simple.

  5. Operations says:

    Thanks a million!

  6. Ravi says:

    Hi,

    We have started our new magento webstore recently. On the sales reports if along with the order id, customer name, customer email, order value & other attributes, we want to get the count of orders placed by that particular customer.

    Is there anything that you can help us in achieving this?

    Please advice

    Thanks,
    Ravi

Leave a Reply to Operations

top