Dec 31, 2011

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;

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

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

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


  • 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');
        $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 


            try {
                //the save the data and send the new account email.
                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');
        try {
            if ($_isGuest === 1):
                echo 'customer group changed';
            echo 'customer address saved';
        } catch (Exception $ex) {
  • which observer file paste this code please explain clearly

  • Do you have this code for magento 2?

  • Excellent article. I absolutely love this site.

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!




Magento top 50 contributors

Magento top 50 contributors