1

Hello I want to save new customer on checkout page after filling their information but before process to checkout button clicked. Is their any possibility to save new customer using this method?

  • The observer us not working for me. Mag1.7. To have payment methods defined by customer group, having a guest turn into a logged user and having more payment methods offered is my goal, am I in the correct path? Any advice. Thank you very much. – serg Nov 1 '13 at 9:50
5

write an observer to get triggered on save billing or save shipping events and save the customer.

for example:

create Yournamespace_Yourmodulename.xml in app/etc/modules/

<?xml version="1.0"?>
<config>
<modules>
    <Yournamespace_Yourmodulename>
        <active>true</active>
        <codePool>local</codePool>
    </Yournamespace_Yourmodulename>
</modules>

in app/code/local/Yournamespace/Yourmodulename/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
    <Yournamespace_Yourmodulename>
        <version>1.0.0</version>
    </Yournamespace_Yourmodulename>
</modules>
<global>
    <events>
        <controller_action_postdispatch_checkout_onepage_saveBilling>
            <observers>
                <auto_register_shipping>
                    <type>singleton</type>
                    <class>Yournamespace_Yourmodulename_Model_Observer</class>
                    <method>autoRegisterBilling</method>
                </auto_register_shipping>
            </observers>
        </controller_action_postdispatch_checkout_onepage_saveBilling>
    </events>
</global>
</config>

in app/code/local/Yournamespace/Yourmodulename/Model/Observer.php

<?php
class Yournamespace_Yourmodulename_Model_Observer {

    public function autoRegisterBilling($evt){
        if(!Mage::helper('customer')->isLoggedIn()){
            $data = $evt->getEvent()->getControllerAction()->getRequest()->getPost('billing', array());
            $customer = Mage::getModel("customer/customer");
            $email = $data['email'];
            $websiteId = Mage::app()->getWebsite()->getId();
            $store = Mage::app()->getStore();
            $pwd = $data['customer_password'];
            $customer->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($email);

            if (!$customer->getId()) {
                 //Code begins here for new customer registration
                $customer->website_id = $websiteId;
                $customer->setStore($store);
                $customer->firstname = $data['firstname'];
                $customer->lastname = $data['lastname'];
                $customer->setEmail($email);
                $customer->setPassword($pwd);
                $customer->sendNewAccountEmail('confirmed');  
                $customer->save();
                   }
           }
    }
}

note :

  1. this will save a customer immediately after they click continue in billing information tab.

  2. you can do this on save shipping or save payment method steps.

  3. if you wanna automatically login that customer, add the following in Observer.php after

    $customer->save();
    }
    
    Mage::getSingleton('customer/session')->loginById($customer->getId()); // to login that customer.
    
  • i want add observer on save payment method then? – Keyur Shah Jul 30 '13 at 7:53
  • it is not working in save billing means observer not calling – Keyur Shah Jul 30 '13 at 9:15
  • just check whether "autoRegisterBilling()" is executed. – Shathish Jul 31 '13 at 5:40
  • it is calling but getting NULL values only getting POST CODE – Keyur Shah Jul 31 '13 at 5:47
  • i am using one page checkout – Keyur Shah Jul 31 '13 at 5:48
0

Thank you for that great solution. I was looking for that from long time. But I've juste an important a notice regarding your note 3 "auto login". If you add the following line...

Mage::getSingleton('customer/session')->loginById($customer->getId());

...outside the following condition:

if (!$customer->getId()) {

It will result that if you entered an existing email address, you'll be automatically logged with the related account. Do you have the same result from your side?

By the way, I've juste moved the "auto login" instruction Inside the conditon. And it work just like a charm.

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.