Magento – Check for Customer's First Order

Is there a quick way to programatically check if the currently logged in customer has ever placed an order? I’m writing an observer and at a point I want to simply say

if ($this->isCustomerLoggedIn() && (has never placed an order))
{ 
do this 
}

possible?
Thanks!

2 thoughts on “Magento – Check for Customer's First Order”

  1. $orders = Mage::getResourceModel('sales/order_collection')
        ->addFieldToSelect('*')
        ->addFieldToFilter('customer_id', $customer->getId());
    
    if ($this->isCustomerLoggedIn() && (!$orders->getSize()))
    { 
        // has never placed an order 
    }
    

    The above assumes that you have a $customer but if it is not accessible from the event you can usually get the currently logged in customer with:

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

  2. Do you need to create an event?

    $session = Mage::getModel('customer/session');
    $order = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_id',$session->getId())->getFirstItem();
    

Leave a Reply

Your email address will not be published. Required fields are marked *