Programmatically adding new customers to the Magento store

Programmatically adding new customers to the Magento store

Us developers love adding things programmatically. Even though you can create a new customer through a signup form, or via admin interface, in some cases, that might take too long.

If you need a bunch of customers assigned to different groups, from different countries, you might be better off doing this from the code.

For starters, let’s add a customer with some basic information.

$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$customer ->setWebsiteId($websiteId)
catch (Exception $e) {

Example 1

As we can see, the code above adds a customer with only first and last names, email and password set. This sometimes might be enough, but we can do more. You could add middle name, assign the customer to a specific customer group, even add prefixes and suffixes to his/hers name.

$customer ->setWebsiteId($websiteId)

The result:
Example 3

For the customer to be a complete entity able to complete orders, we need to add an address and assign it to a customer. Let’s do just that.

$address = Mage::getModel("customer/address");
//->setRegionId('1') //state/province, only needed if the country is USA
catch (Exception $e) {

Example 2

Note that the setCountryId needs country code as value (you could find out these values by inspecting ‘Country’ input field on customer creation page in administration). Same goes for setGroupId method above, you need an ID of a customer group, which is easiest to find out by inspecting elements.

I hope most of the code is self-explanatory, since we know what fields are required when adding a customer from the administration. The thing you should keep an eye on are required fields.
In case you would need multiple customers to be added, you could place the part of the code in a loop, and create them within seconds.

Note: This is a revamp of an article originally written in July 2012.

You made it all the way down here so you must have enjoyed this post! You may also like:

How to programmatically create customers in Magento 2.3.x Deni Pesic
Deni Pesic, | 2

How to programmatically create customers in Magento 2.3.x

Programmatically create upsell, cross sell and related products in Magento Damir Serfezi
Damir Serfezi, | 3

Programmatically create upsell, cross sell and related products in Magento

Managing My Account Navigation Links in Magento 2 Matej Djakovic
Matej Djakovic, | 10

Managing My Account Navigation Links in Magento 2


  1. I believe this code is for Magento 1 – is that correct? Is there an example for Magento 2? This code gets an error on Magento 2.

    PHP Fatal error: Uncaught Error: Class 'Mage' not found in /var/www/html/bin/m-change-password.php:3
    Stack trace:
    #0 {main}
    thrown in /var/www/html/bin/m-change-password.php on line 3

  2. Thanks for the great tutorial. is a great resource for magento tutorials and I hope here on someone definitely help me regarding my query. Please, find it below:

    I am working on a project where I need to add a custom user from the magento admin panel. This user cannot purchase the products. He can only login to his section and write reviews on products or update his/her profile information. Magento admin should be responsible for creating this user with his username and password and other details like profile image, social media pages links etc. Please, help regarding this I really have no idea of the best practices when it comes to creating entity that is managed from the admin panel. I have created an admin managable custom entity which works fine to add/update/delete records of this user but I am confused how I can add this to custom who can only review and update his profile. Thanks in advance.

    1. You can create a php file anywhere and just run it with:

       php MyScriptFile.php 

      directly on your server

  3. Not expecting further response to this article but just in case, I’ll ask. What is the “best practice” opinion here? Would it be better (not just easier) to ask customers to create their own new password or to process the existing password prior to importing?
    Also, has anyone tried to encrypt a password directly in the SQL database of the legacy ecommerce app (a copy or export of course)?
    I realize this article was intended to demonstrate a use case for development but it certainly has some application to migrations.

  4. Hello Petar,

    Great and useful article. I have a question , I have checkbox for newsletter subscription of my registeration form. How can I save this into db?

    Appreciate for any help. Thanks

  5. Nice article, only when trying to import / migrate between the 150.000 – 250.000 customers, this approach will take a couple of days to run, not to mention the memory load it will have.

  6. Nice idea,

    Just a concern, sending a password request to over 65k customers (probably 20% of them are spam) is a big issue over shared servers. And handling that alot traffic once they are ready to change their password will be another concerning issue.

    I am reminding myself that I saw a tool built by Magento in a little past for OSCommerce migration which uses a method to import the existing customers in full to Magento (not sure how they were doing)

    I think I should better see their approach and share my experience with you. Hopefully that helps.

  7. Hi, Farhan.

    To add salt to an already hashed password, you would need to append the hash to MD5 password, and hash it again with MD5.

    Thing is, this presents a problem during login, since now you have to hash the password twice to check against your database.

    The best approach here would be to not do anything with passwords. Once imported, send each an email to each user to reset their password. They might even buy something, once they visit your store again. 🙂

  8. Hello Petar,

    Great article.

    Just a question I was wondering about to is if we have a MD5 encrypted password value of our customer, is it possible to use that in some possible way?

    Basically I faces some situation in migration process of customers from existing website databases like OSCommerce, ZenCarts etc which is used to save the password in md5 encrypted way (without using any salt).

    The default Magento pattern what I know is CONCAT(MD5(‘qXpassword’), ‘:qX’) where :qX is the salt.

    any relevant help is appreciated.

    1. Maybe is late but I think you could use an empty SALT… nothing after “:”
      I think that you could put in Magento DB a password as


Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.