
In this article, we will discuss how to create new customers using CLI in Magento 2.
Currently, you can’t create a new Customer like an Admin user using the CLI command in Magento 2. You can create a custom module in Magento to create your own CLI command for creating a new Customer.
Steps on How to Create New Customers Using CLI in Magento 2
1) Create a di.xml file in an etc folder in the default area:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="customer_user_create" xsi:type="object">Vendor\Module\Console\Command\CustomerUserCreateCommand</item>
</argument>
</arguments>
</type>
</config>
2) Create a file named CustomerUserCreateCommand.php at location app/code/Vendor/Module/Console/Command/CustomerUserCreateCommand.php
<?php
namespace Vendor\Module\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Vendor\Module\Helper\Customer;
class CustomerUserCreateCommand extends Command
{
protected $customerHelper;
public function __construct(Customer $customerHelper)
{
$this->customerHelper = $customerHelper;
parent::__construct();
}
protected function configure()
{
$this
->setName('customer:user:create')
->setDescription('Create new customer')
->setDefinition($this->getOptionsList());
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>Creating new user...</info>');
$this->customerHelper->setData($input);
$this->customerHelper->execute();
$output->writeln('');
$output->writeln('<info>User created with the following data:</info>');
$output->writeln('<comment>Customer ID: ' . $this->customerHelper->getCustomerId());
$output->writeln('<comment>Customer Website ID ' . $input->getOption(Customer::KEY_WEBSITE));
$output->writeln('<comment>Customer First Name: ' . $input->getOption(Customer::KEY_FIRSTNAME));
$output->writeln('<comment>Customer Last Name: ' . $input->getOption(Customer::KEY_LASTNAME));
$output->writeln('');
$output->writeln('<comment>Customer Email: ' . $input->getOption(Customer::KEY_EMAIL));
$output->writeln('<comment>Customer Password: ' . $input->getOption(Customer::KEY_PASSWORD));
}
protected function getOptionsList()
{
return [
new InputOption(Customer::KEY_FIRSTNAME, null, InputOption::VALUE_REQUIRED, '(Required) Customer first name'),
new InputOption(Customer::KEY_LASTNAME, null, InputOption::VALUE_REQUIRED, '(Required) Customer last name'),
new InputOption(Customer::KEY_EMAIL, null, InputOption::VALUE_REQUIRED, '(Required) Customer email'),
new InputOption(Customer::KEY_PASSWORD, null, InputOption::VALUE_REQUIRED, '(Required) Customer password'),
new InputOption(Customer::KEY_WEBSITE, null, InputOption::VALUE_REQUIRED, '(Required) Website ID'),
new InputOption(Customer::KEY_SENDEMAIL, 0, InputOption::VALUE_OPTIONAL, '(1/0) Send email? (default 0)')
];
}
}
3) Now create a helper file named Customer.php as app/code/Vendor/Module/Helper/Customer.php
<?php
namespace Vendor\Module\Helper;
use \Magento\Framework\App\Helper\Context;
use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Framework\App\State;
use \Magento\Customer\Model\CustomerFactory;
use \Symfony\Component\Console\Input\Input;
class Customer extends \Magento\Framework\App\Helper\AbstractHelper
{
const KEY_EMAIL = 'customer-email';
const KEY_FIRSTNAME = 'customer-firstname';
const KEY_LASTNAME = 'customer-lastname';
const KEY_PASSWORD = 'customer-password';
const KEY_WEBSITE = 'website';
const KEY_SENDEMAIL = 'send-email';
protected $storeManager;
protected $state;
protected $customerFactory;
protected $data;
protected $customerId;
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
State $state,
CustomerFactory $customerFactory
) {
$this->storeManager = $storeManager;
$this->state = $state;
$this->customerFactory = $customerFactory;
parent::__construct($context);
}
public function setData(Input $input)
{
$this->data = $input;
return $this;
}
public function execute()
{
$this->state->setAreaCode('frontend');
$customer = $this->customerFactory->create();
$customer
->setWebsiteId($this->data->getOption(self::KEY_WEBSITE))
->setEmail($this->data->getOption(self::KEY_EMAIL))
->setFirstname($this->data->getOption(self::KEY_FIRSTNAME))
->setLastname($this->data->getOption(self::KEY_LASTNAME))
->setPassword($this->data->getOption(self::KEY_PASSWORD));
$customer->save();
$this->customerId = $customer->getId();
if($this->data->getOption(self::KEY_SENDEMAIL)) {
$customer->sendNewAccountEmail();
}
}
public function getCustomerId()
{
return (int)$this->customerId;
}
}
Now run the php bin/magento setup:upgrade CLI command in the terminal.
On running php bin/magento you will find customer:user:create CLI command listed in the existing command lists of Magento 2. Now use the below example to create a Customer in Magento and change the appropriate variables required.
Example:
php bin/magento customer:user:create –customer-firstname=”John” –customer-lastname=”Doe” –customer-email=”john@example.com” –customer-password=”john123″ –website=”1″
After running the above CLI command you will find the Customer is created in your Magento 2 with the credentials provided by you in the above command.
You can also check more on creating a CLI commands in Magento 2 from Magento 2 official documentation https://developer.adobe.com/commerce/php/development/cli-commands/custom/
Please share if you find this article useful. Thanks and happy coding!
Also Read: https://techurbane.com/must-have-magento-2-extensions-supercharge-your-store/