I'm trying to do a lookup on the success page to see if a customer has placed an order before. Many of the solutions I have found were checking against a customer ID, which is fine if you're only interested in logged in customers.

However, I want to check on the email address because we also use a guest checkout so some customers may have previously ordered.

I'm using the following code to set the cust_type value that I want to send through to some order tracking script to show whether someone has placed an order before or not.

I'm also noticing (I think), that if someone is placing their first order, then by the time we get to the success page the Order Collection would return 1 for getSize because it would include the order they have just placed, which I need to ignore to ascertain if they have ordered with us before;


$_customer = Mage::getSingleton('customer/session')->getCustomer();
$_customerId = Mage::getSingleton('customer/session')->getCustomerId();

$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$_orderData = $order->getData();
$_grand_total = number_format((float)$_orderData['grand_total'], 2, '.', '');
$_qty_total = round($_orderData['total_qty_ordered']);

    //If logged in, get Customer email
    $customerEmail = $_customer->getEmail();
    //If not logged in, get Email from Order
    $customerEmail = $order->getCustomerEmail();

$_previousOrdersCheck = Mage::getResourceModel('sales/order_collection')
    ->addFieldToFilter('customer_email', $customerEmail)

if ($_previousOrdersCheck->getSize()) {
    $cust_type = 2;
    $cust_type = 1;


1 Answer 1


You don't need check is customer logged-in or not, just use e-mail from the last order, like this:

$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$email = $order->getCustomerEmail();

$collection = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('customer_email', $email)

if ($collection->count() >= 2) {
    // Customer has 2 or more orders
} else {
    // Customer has 1 order
  • Thanks Siarhey, I will give this a try. Regarding the customer email it was just a personal preference to make sure that if someone is logged in, that we're using the email address they registered - just in case they change the email address during checkout. Apr 14, 2016 at 14:28
  • @doubleplusgood so, if your script doesnt work, I think the error is in ->setPageSize(1). Try to replace it with the ->setPageSize(2) and change $_previousOrdersCheck->getSize() to $_previousOrdersCheck->getSize() >= 2 Apr 14, 2016 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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