Magento – Trigger New Customer Registration Event on Onepage Checkout

checkoutcustomeronepage-checkout

When an unregistered customer goes to the checkout page and registers for an account there, on the final stage when the customer presses "Place order" Magento adds the new customer to the database.

I want to catch that event and get the new customer's details.

Problems

  1. The event customer_register_success doesn't work on the onepage checkout page
  2. The event checkout_type_onepage_save_order_after works every time a customer presses "Place order", and I want to catch only the first time

I want to catch the exact event that adds the new customer after they've registered in the onepage checkout page, and get the details of that new customer (after the event).

Best Answer

I've only had a quick look through the Onepage model but it looks like you should be able to use the checkout_type_onepage_save_order_after event, just apply some extra conditions.

As you can see here the core model uses the checkout method to find out what "type" of checkout process it should be.

Knowing what is in the event, you can apply the same logic using that event, i.e.:

/**
 * Event: checkout_type_onepage_save_order_after
 */
public function getNewCustomer(Varien_Event_Observer $observer)
{
    /** @var Mage_Sales_Model_Quote $quote */
    $quote = $observer->getEvent()->getQuote();

    // Don't continue unless it's a new customer registration
    if ($quote->getCheckoutMethod !== Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER) {
        return $this;
    }

    // You know it's a NEW customer now...

    /** @var Mage_Customer_Model_Customer $customer */
    $customer = $quote->getCustomer();
}