
Magento 2 customer accounts are mostly created on the frontend by customers themselves. However, there may be cases when store managers need to create customer accounts from the admin panel.
Nonetheless, creating customer accounts manually may be time-consuming if you need to create a few. Thus, it may be more convenient to create customer in Magento 2 programmatically.
It's exactly what you'll learn today.
Post Contents [hide]
Create Customer Using Dependency Injection
To begin with, you can create a customer programmatically using the dependency injection method. Just use the code below:
<?php
declare(strict_types=1);
namespace Vendor\Module\Model;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Encryption\EncryptorInterface;
class CreateCustomerService
{
/**
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var CustomerInterfaceFactory
*/
protected $customerFactory;
/**
* @var CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* @var EncryptorInterface
*/
protected $encryptor;
/**
* @param StoreManagerInterface $storeManager
* @param CustomerInterfaceFactory $customerFactory
* @param CustomerRepositoryInterface $customerRepository
* @param EncryptorInterface $encryptor
*/
public function __construct(
StoreManagerInterface $storeManager,
CustomerInterfaceFactory $customerFactory,
CustomerRepositoryInterface $customerRepository,
EncryptorInterface $encryptor
){
$this->storeManager = $storeManager;
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
$this->encryptor = $encryptor;
}
/**
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(): bool
{
// Get Website ID
$websiteId = $this->storeManager->getWebsite()->getWebsiteId();
// Instantiate object (this is the most important part)
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
// Preparing data for new customer
$customer->setEmail("email@domainnq.com");
$customer->setFirstname("First Name");
$customer->setLastname("Last name");
$hashedPassword = $this->encryptor->getHash('MyNewPass', true);
// Save data
try {
$this->customerRepository->save($customer, $hashedPassword);
} catch (\Exception $e) {
//log the error
return false;
}
return true;
}
}
Create Customer Using Object Manager
The object manager method is another option for you to consider. Though seemingly more straightforward, object manager tends to hide the real dependencies of the class. So, we suggest not using it directly.
<?php
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeId = $storeManager->getStore()->getId();
$websiteId = $storeManager->getStore($storeId)->getWebsiteId();
try {
$customer = $objectManager->get('\Magento\Customer\Api\Data\CustomerInterfaceFactory')->create();
$customer->setWebsiteId($websiteId);
$email = 'test11@example.com';
$customer->setEmail($email);
$customer->setFirstname("test first");
$customer->setLastname("test last");
$hashedPassword = $objectManager->get('\Magento\Framework\Encryption\EncryptorInterface')->getHash('MyNewPass', true);
$objectManager->get('\Magento\Customer\Api\CustomerRepositoryInterface')->save($customer, $hashedPassword);
$customer = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create();
$customer->setWebsiteId($websiteId)->loadByEmail($email);
} catch (Exception $e) {
echo $e->getMessage();
}
There you go! You can choose the method you're most comfortable working with and create customers in Magento programmatically much faster.
Once you create the customers, you might also need to create customer groups programmatically and assign customers to them.