Tag Archives: Magento 2 code to create customer

Magento 2 Create Customer Programmatically


Magento 2 Create Customer Programmatically Sometimes we need custom code to create customer such as creating custom regirstration page or creating some custom customer import functionality. Here in this article, we are going to explain how you can create customer using custom program.


Magento 2 Create Customer Programmatically Example

Magento 2 Create Customer Programmatically

You can create customer in Magento2 using custom code simply as below-

Magento 2 Create Customer Programmatically Example:

storeManager     = $storeManager;
        $this->customerFactory  = $customerFactory;

        parent::__construct($context);
    }

    public function execute()
    {
        // Get Current Website ID
        $websiteId  = $this->storeManager->getWebsite()->getWebsiteId();
        $customer   = $this->customerFactory->create();
        $customer->setWebsiteId($websiteId);
        $customer->setEmail("john@example.com"); 
        $customer->setFirstname("John");
        $customer->setLastname("Dee");
        $customer->setPassword("123XYZ");
        $customer->save();
        $customer->sendNewAccountEmail();
    }
}
?>

So you can create customer in Magento2 programmatically.