Categories: How ToMagento 2

How to Create Customer Programmatically in Magento 2

Normally in Magento, new customers are created through sign up form on website or via admin panel. It’s time consuming task especially when you want to create numerous customers and assign to some customer groups from different countries, migrating to Magento, it’s better to do with code rather creating them manually.

Detailed code to create customers programmatically in Magento 2:

  1. Create New Customer:
  2. $objectManager = $bootstrap->getObjectManager();
     
    $url = \Magento\Framework\App\ObjectManager::getInstance();
     
    $storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
     
    $state = $objectManager->get('\Magento\Framework\App\State');
     
    $state->setAreaCode('frontend');
     
    // Customer Factory to Create Customer
     
    $customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');
     
    $websiteId = $storeManager->getWebsite()->getWebsiteId()
     
    $store = $storeManager->getStore();  // Get Store ID
     
    $storeId = $store->getStoreId();
     
    // Instantiate object (this is the most important part)
     
    $customer = $customerFactory->create();
     
    $customer->setWebsiteId($websiteId);
     
    $customer->setEmail("hello@example.com");
     
    $customer->setFirstname("John");
     
    $customer->setLastname("Doe");
     
    $customer->setPassword(‘152452652’);
     
    $customer->save();
     
    echo 'Create customer successfully'.$customer->getId();
    
  3. Add Address to Customer:
  4. $addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');
    
    $address = $addresss->create();
    
    $address->setCustomerId($customer->getId())
    
    ->setFirstname(FirstName)
    
    ->setLastname(LastName)
    
    ->setCountryId(US)
    
    ->setPostcode(85001)
    
    ->setCity(Pheonix)
    
    ->setTelephone(1234567890)
    
    ->setFax(123456789)
    
    ->setCompany(Company)
    
    ->setStreet(Street)
    
    ->setIsDefaultBilling('1')
    
    ->setIsDefaultShipping('1')
    
    ->setSaveInAddressBook('1')
    
    $address->save();
    

With the above code, I hope you can create the new customers more comfortably saving a lot of time. Here the most important thing is to keep your eye on required fields and pass them with your code. In case of adding multiple customers together, place the same code in a loop and create them within the blink of an eye. Let me know if you require my assistance or help while implementing, your comments are always welcomed.

Click to rate this post!
[Total: 11 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

8 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

6 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

6 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago