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
You can create customer in Magento2 using custom code simply as below-
Magento 2 Create Customer Programmatically Example:
<?php namespace MyCompany\Module_Name\Controller\Index; class Index extends \Magento\Framework\App\Action\Action { /** * @var \Magento\Store\Model\StoreManagerInterface */ protected $storeManager; /** * @var \Magento\Customer\Model\CustomerFactory */ protected $customerFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Customer\Model\CustomerFactory $customerFactory ) { $this-?>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.