How to know if an order is from existing customer or new customer?

In following event call:

Mage_Sales_Model_Service_Quote::submitOrder

Mage::dispatchEvent('checkout_type_onepage_save_order', array('order'=>$order, 'quote'=>$quote));

We have two entities which have customer_id value. When we have existing customer - then $order->getCustomerId() will return it's real ID. If customer is just created - then it returns true.

So if you like to apply something to order based on customer (is new/old) run in observer:

if ($observer->getOrder()->getCustomerId() === true) {
    //here goes code for customer, which was just created
} elseif ($observer->getOrder()->getCustomerId() > 0) {
    //here goes code for old customer
} elseif ($observer->getOrder()->getCustomerId() === null) {
    //here goes code for guest
}

Explanation:

Mage_Checkout_Model_Type_Onepage::saveOrder

/**
 * Create order based on checkout type. Create customer if necessary.
 *
 * @return Mage_Checkout_Model_Type_Onepage
 */
public function saveOrder()
{
    $this->validate();
    $isNewCustomer = false;
    switch ($this->getCheckoutMethod()) {
        case self::METHOD_GUEST:
            $this->_prepareGuestQuote();
            break;
        case self::METHOD_REGISTER:
            $this->_prepareNewCustomerQuote();
            $isNewCustomer = true;
            break;
        default:
            $this->_prepareCustomerQuote();
            break;
    }

Let's have a look at three protected methods: _prepareGuestQuote(), _prepareNewCustomerQuote(), _prepareCustomerQuote()

/**
 * Prepare quote for guest checkout order submit
 *
 * @return Mage_Checkout_Model_Type_Onepage
 */
protected function _prepareGuestQuote()
{
    $quote = $this->getQuote();
    $quote->setCustomerId(null)

As you can see guest customer has null value


/**
 * Prepare quote for customer registration and customer order submit
 *
 * @return Mage_Checkout_Model_Type_Onepage
 */
protected function _prepareNewCustomerQuote()
{
    ...
    $quote->setCustomer($customer)
        ->setCustomerId(true);
}

For new customer we are setting true


/**
 * Prepare quote for customer order submit
 *
 * @return Mage_Checkout_Model_Type_Onepage
 */
protected function _prepareCustomerQuote()
{
    $quote      = $this->getQuote();
    $billing    = $quote->getBillingAddress();
    $shipping   = $quote->isVirtual() ? null : $quote->getShippingAddress();

    $customer = $this->getCustomerSession()->getCustomer();
    ...
    $quote->setCustomer($customer);
}

For existing customer we are taking it's object from session. As you understand - this object will have customer_id value.


you can diff new customer by created time of order:

$LastOrder=Mage::getModel('sales/order')->load($orderId);
$LastOrdertime= $LastOrder->getCreatedAt();

$OrderCollection=Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email',$LastOrder->getData('customer_email'))
            ->setOrder('created_at','asc');
$fstoredetime= $OrderCollection->getFirstItem()->getCreatedAt();    

if($fstoredetime==$LastOrdertime):
// new customer
else:
//old customer
endif;  

Tags:

Php