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.

$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.

10 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

    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

  • Do you have this code for magento 2?

  • Excellent article. I absolutely love this site.
    Thanks!

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

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions