Programmatically Create New Customer Account in Magento

In this article, I am going to show you how to create new customer account programmatically in Magento.

Here are the steps that I followed in the code below:

  • Create new PHP file in Magento root folder i.e create_new_customer.php
  • Include Mage.php from the app folder which contains all the Magento class.
  • Check whether the email address is already registered or not.
  • If the email address already registered, return the error message.
  • If the email address is not registered, set the customer information (Firstname, Lastname, Email, and Address) and create the new customer account.
  • While creating a new customer account, can put a custom password or let Magento generate the password.
  • The email will be sent to the customer about the account creation.
  • Used the custom log file to save the log message.

Here is the full source code to create new customer account programmatically in Magento:

<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('default');
 
$store = Mage::app()->getStore();
$website = Mage::app()->getWebsite();
$websiteId = $website->getId();
  
$firstName = 'Thomas';
$lastName = 'Raj';
$email = 'thomas.raj@example.com';
$logFile = 'my_custom_log_file.log';
  
$address = array(
    'customer_address_id' => '',
    'prefix' => '',
    'firstname' => $firstName,
    'middlename' => '',
    'lastname' => $lastName,
    'suffix' => '',
    'company' => '', 
    'street' => array(
        '0' => 'Customer Address 1', // this is mandatory
        '1' => 'Customer Address 2' // this is optional
    ),
    'city' => 'New York',
    'country_id' => 'US', // two letters country code
    'region' => 'New York', // can be empty '' if no region
    'region_id' => '43', // can be empty '' if no region_id
    'postcode' => '10450',
    'telephone' => '123-456-7890',
    'fax' => '',
    'save_in_address_book' => 1
);
  
// check whether the email address is already registered or not
$customer = Mage::getModel('customer/customer')
                ->setWebsiteId($websiteId)
                ->loadByEmail($email);
  
/** 
 * if email address already registered, return the error message
 * else, create new customer account
 */
if ($customer->getId()) {
 
    Mage::log('Customer with email '.$email.' is already registered.', null, $logFile);
    echo 'Customer with email '.$email.' is already registered.';
  
} else {
   
    $customer = Mage::getModel('customer/customer');
     
    $password = $customer->generatePassword(); // you can write your own password here instead of magento generated password
  
    $customer->setWebsiteId($websiteId)
             ->setStore($store)
             ->setFirstname($firstName)
             ->setLastname($lastName)
             ->setEmail($email)
             ->setPassword($password);
  
    try {
        // set the customer account as confirmed
        $customer->setForceConfirmed(true);
  
        // save customer
        $customer->save();
  
        $customer->setConfirmation(null);
        $customer->save();
  
        // set customer address
        $customerId = $customer->getId(); 
        $customAddress = Mage::getModel('customer/address');
        $customAddress->setData($address)
                      ->setCustomerId($customerId)
                      ->setIsDefaultBilling('1')
                      ->setIsDefaultShipping('1')
                      ->setSaveInAddressBook('1');
  
        // save customer address
        $customAddress->save();
  
        // send new account email to customer
        $storeId = $customer->getSendemailStoreId();
        $customer->sendNewAccountEmail('registered', '', $storeId);
  
        // set password remainder email if the password is auto generated by magento
        $customer->sendPasswordReminderEmail();
  
        Mage::log('Customer with email '.$email.' is successfully created.', null, $logFile);
        echo 'Customer with email '.$email.' is successfully created.';
  
    } catch (Mage_Core_Exception $e) {
  
        Zend_Debug::dump($e->getMessage());
        Mage::log($e->getMessage(), null, $logFile);
 
    } catch (Exception $e) {
  
        Zend_Debug::dump($e->getMessage());
        Mage::log($e->getMessage(), null, $logFile);
    }
}

Note: This code has been tested in Magento 1.9.X. 

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *