0

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;

<?php

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

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

if(Mage::getSingleton('customer/session')->isLoggedIn()){
    //If logged in, get Customer email
    $customerEmail = $_customer->getEmail();
}else{
    //If not logged in, get Email from Order
    $customerEmail = $order->getCustomerEmail();
};

$_previousOrdersCheck = Mage::getResourceModel('sales/order_collection')
    ->addFieldToSelect('entity_id')
    ->addFieldToFilter('customer_email', $customerEmail)
    ->setCurPage(1)
    ->setPageSize(1)
    ->getFirstItem();

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

?>
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');
$order->load($lastOrderId);
$email = $order->getCustomerEmail();

$collection = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('customer_email', $email)
    ->setPageSize(2)
    ->setCurPage(1);

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. – doubleplusgood Apr 14 '16 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 – Siarhey Uchukhlebau Apr 14 '16 at 14:30

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.