How to Create Customers Programmatically in Magento 2

How to Create Customers Programmatically in Magento 2

In Magento 2, a new customer account is created via sign up. Also, customer registration is possible via admin panel. But, as a store owner, what if you want to add a huge number of new customers manually? The task becomes tedious as you have to create the customer accounts having different addresses and need to assign them in different groups. It is not feasible to manually create each customer in such case. The smarter way is to create customers programmatically in Magento 2.

The method discussed here allows to programmatically create customers and assign to a customer group. Save time and get your task done quickly!

Method to create customers programmatically in Magento 2:

 

Run the above code in a loop and create multiple customers at a time, changing the email ID every time.

I’d be happy to assist you in the implementation of the code if you are stuck in the process. Please mention your doubts in the comments section below.

Flash 5 stars if the post is helpful!

Thank you 🙂

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

5
(based on 23 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

10 comments On How to Create Customers Programmatically in Magento 2

  • I have used this code, but its not working..??Customer is not saving

    • Hey Gayathri,
      I’ve used the above code repeatedly and works fine each time.
      The issue might be something else.
      Thank you.

  • where should is add this code?

  • I am using the ‘save()’ result in $result and then fetching user id by ‘$result->getId()’. That works ok. One more thing. Let say there comes an error, let say “main.ERROR: A customer with the same email address already exists in an associated website. [] []”, this entry goes into system.log.
    Though I am unable to check this error in the $result. Neither it falls into catch block. May you help

    • Please use the below code to check if the email address already exists:
      $customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create();
      if ($customerFactory->loadByEmail($email)->getId()) {
      $errMesg = 'There is already an account with this email address ' . $email;
      }

  • So does the ‘create()’ method will only create the customer or ‘loadByEmail($email)’ is also necessary? What is the purpose of loadByEmail here? Does ‘setWebsiteId($websiteId)’ is necessary if we are running this code from admin end.

    • ‘create()’ is used for creating Objects and not for customers.
      ‘loadByEmail($email)’ is not necessary but you can use it if you want to get the customer information using email id.
      ‘setWebsiteId($websiteId)’ is necessary because you won’t be able to select the website if it’s a Multi-website store.

  • How can i add event after this … i want to add customer save after event after

Leave a reply:

Your email address will not be published.