Dec 31, 2011
kalpesh

Magento: Register guest user to website if email provided

If your Magento website have feature where guest user can place an order without registering themself in your site, you can add them as a customer in your database. It helps in grouping the orders w.r.t email address in backend sales order. The only condition is that he should provide his email address while placing an order, which is quite obvious.

So here is a snippet of code you can insert where you are asking user to enter his email address. Just check if the user is already not registered in your site.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$e = $email; //provided by guest user

$store = Mage::app()->getStore();
$websiteId = Mage::app()->getWebsite()->getId();

$customerObj = Mage::getModel('customer/customer');
$customerObj->website_id = $websiteId;
$customerObj->setStore($store);

$prefix = "mag";
$pwd = uniqid($prefix);

$session = Mage::getSingleton('checkout/session');
$fname = $session->getFirstname();
$lname = $session->getLastname();

$customerObj->setEmail($e);
$customerObj->setFirstname('Guest');
$customerObj->setLastname('Guest');
$customerObj->setPassword($pwd);
$customerObj->save();
$customerObj->sendNewAccountEmail('confirmed'); //auto confirmed

UPDATE: It depends where you want to keep this functionality on your site. I have put it in saveShippingMethodAction() function of Mage/Checkout/controllers/OnepageController.php file after extending. Don’t forget, this code should only be run if customer is guest.

8 Comments

  • Which file to update for this functionality?

  • Great thanks!

  • Which file to update for this functionality?

  • where to put this code ?
    in which file ?

  • Please check my update in the post to know where to place the code.

  • still i am getting issue in registering the guest user ,

    can you please explain in details.or if you have alternative to register the guest customer after they place order

  • u can use this code in observer

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    
    public function createUser() {
            
            
            
           $ids = Mage::getSingleton('checkout/session')->getLastOrderId();
    $order = Mage::getModel('sales/order')->load($ids);
    if ($order->getCustomerId() === NULL) {
        $_billingAddress = $order->getBillingAddress();
        $_isGuest = $order->getCustomerIsGuest();
    
    
        $customer_fname = $order->getCustomerFirstname();
        $customer_lname = $order->getCustomerLastname();
        $customer_email = $order->getCustomerEmail();
    
        $customer = Mage::getModel('customer/customer');
        $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
        $customer->loadByEmail($customer_email);
        $street = $_billingAddress->getStreet();
        /*
         * Check if the email exist on the system.
         * If YES,  it will not create a user account. 
         */
    
    
        if (!$customer->getId()) {
    
            //setting data such as email, firstname, lastname, and password 
    
            $customer->setEmail($customer_email);
            $customer->setFirstname($customer_fname);
            $customer->setLastname($customer_lname);
            $customer->setPassword($customer->generatePassword());
    
            try {
                //the save the data and send the new account email.
                $customer->save();
                $customer->setConfirmation(null);
                $customer->save();
                $customer->sendNewAccountEmail();
                echo 'donneeee';
            } catch (Exception $ex) {
                
            }
        }
    //Build billing and shipping address for customer, for checkout 
        $_custom_address = array(
            'firstname' => $customer_fname,
            'lastname' => $customer_lname,
            'street' => array(
                '0' => $street[0],
    //            '1' => $street[1],
            ),
            'city' => $_billingAddress->getCity(),
            'region_id' => $_billingAddress->getRegionId(),
            'region' => $_billingAddress->getRegion(),
            'postcode' => $_billingAddress->getPostcode(),
            'country_id' => $_billingAddress->getCountryId(),
            'telephone' => $_billingAddress->getTelephone(),
            'fax' => $_billingAddress->getFax(),
        );
        $customAddress = Mage::getModel('customer/address');
        $customAddress->setData($_custom_address)
                ->setCustomerId($customer->getId())
                ->setIsDefaultBilling('1')
                ->setIsDefaultShipping('1')
                ->setSaveInAddressBook('1');
        try {
            if ($_isGuest === 1):
                $order->setCustomerGroupId(1);
                $order->save();
                echo 'customer group changed';
            endif;
            $customAddress->save();
            echo 'customer address saved';
        } catch (Exception $ex) {
            
        }
        Mage::getSingleton('checkout/session')->getQuote()->setBillingAddress(Mage::getSingleton('sales/quote_address')->importCustomerAddress($customAddress));
    }
  • which observer file paste this code please explain clearly

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications