Magento 2 Create Customers Using CLI

Here I am creating a module PHPCodez_Customers that will help us to create customers from commandline.

Create folders for the modul app/code/PHPCodez/Customers

Declare the module – Create module.xml

File : app/code/PHPCodez/Customers/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
 <module name="PHPCodez_Customer" setup_version="0.0.1"/>
</config>

Register the module – Create registration.php

File : app/code/PHPCodez/Customers/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 'PHPCodez_Customer',
 __DIR__
);

Add new command – Create di.xml file

File : app/code/PHPCodez/Customers/etc/di.xml

<?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="createPHPCodezCustomers" xsi:type="object">PHPCodez\Customer\Console\CustomerCreateCommand</item>
 <item name="createPHPCodezMultipleCustomers" xsi:type="object">PHPCodez\Customer\Console\CreateMutipleCustomersCommand</item>
 </argument>
 </arguments>
 </type>
</config>

Here I am adding two new command one for single customer account creation phpcodez:customers:create and another one for mutiple customer account cretaion phpcodez:customers:account:create

Create class for the the command phpcodez:customers:account:create

File : app/code/PHPCodez/Customers/Console/CreateMutipleCustomersCommand.php

<?php
namespace PHPCodez\Customer\Console;

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 PHPCodez\Customer\Helper\Customer;

class CreateMutipleCustomersCommand extends Command
{
 protected $customerHelper;

public function __construct(Customer $customerHelper)
 {
 $this->customerHelper = $customerHelper;
 parent::__construct();
 }

protected function configure()
 {
 $this ->setName('phpcodez:customers:account:create')
 ->setDescription('Create new customer')
 ->setDefinition($this->getOptionsList());
 }

protected function execute(InputInterface $input, OutputInterface $output) {
 $output->writeln('<info>Creating customers account</info>');
 $this->customerHelper->setData($input);
 $this->customerHelper->createMultipleCustomerAccount();
 }

protected function getOptionsList()
 {
 return [
 new InputOption(Customer::ARG_COUNT, null, InputOption::VALUE_REQUIRED, '(Required) Count is required'),
 new InputOption(Customer::ARG_WEBSITE, null, InputOption::VALUE_REQUIRED, '(Required) Website ID')
 ];
 }
}

Create class for the the command phpcodez:customers:create

File : app/code/PHPCodez/Customers/Console/CustomerCreateCommand.php

<?php
namespace PHPCodez\Customer\Console;

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 PHPCodez\Customer\Helper\Customer;

class CustomerCreateCommand extends Command
{
 protected $customerHelper;

public function __construct(Customer $customerHelper)
 {
 $this->customerHelper = $customerHelper;
 parent::__construct();
 }

protected function configure()
 {
 $this ->setName('phpcodez:customers: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('');
 $output->writeln('<comment>Customer ID: ' . $this->customerHelper->getCustomerId());
 $output->writeln('<comment>Customer Website ID ' . $input->getOption(Customer::ARG_WEBSITE));
 $output->writeln('<comment>Customer First Name: ' . $input->getOption(Customer::ARG_FIRSTNAME));
 $output->writeln('<comment>Customer Last Name: ' . $input->getOption(Customer::ARG_LASTNAME));
 $output->writeln('<comment>Customer Email: ' . $input->getOption(Customer::ARG_EMAIL));
 $output->writeln('<comment>Customer Password: ' . $input->getOption(Customer::ARG_PASSWORD));
 $output->writeln('');
 }

protected function getOptionsList()
 {
 return [
 new InputOption(Customer::ARG_FIRSTNAME, null, InputOption::VALUE_REQUIRED, '(Required) Customer first name'),
 new InputOption(Customer::ARG_LASTNAME, null, InputOption::VALUE_REQUIRED, '(Required) Customer last name'),
 new InputOption(Customer::ARG_EMAIL, null, InputOption::VALUE_REQUIRED, '(Required) Customer email'),
 new InputOption(Customer::ARG_PASSWORD, null, InputOption::VALUE_REQUIRED, '(Required) Customer password'),
 new InputOption(Customer::ARG_WEBSITE, null, InputOption::VALUE_REQUIRED, '(Required) Website ID'),
 new InputOption(Customer::ARG_SENDEMAIL, 0, InputOption::VALUE_OPTIONAL, '(1/0) Send email? (default 0)')
 ];
 }
}

Create helper for account creation.

File : app/code/PHPCodez/Customers/Helper/Customer.php

<?php
namespace PHPCodez\Customer\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 ARG_COUNT = 'count';
 const ARG_EMAIL = 'email';
 const ARG_FIRSTNAME = 'firstname';
 const ARG_LASTNAME = 'lastname';
 const ARG_PASSWORD = 'password';
 const ARG_WEBSITE = 'website';
 const ARG_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 createMultipleCustomerAccount(){
 $this->state->setAreaCode('frontend');
 $customer = $this->customerFactory->create();
 
 $website = $this->data->getOption(self::ARG_WEBSITE)?$this->data->getOption(self::ARG_WEBSITE):1;
 $count = $this->data->getOption(self::ARG_COUNT)?$this->data->getOption(self::ARG_COUNT):1;
 $count = $count > 100? 100:$count;
 
 for($i =1;$i<=$count;$i++){
 
 $customerEmail = 'Test.User'.time().'@phpcodez.com';
 $customerFirstName = "PHP";
 $customerLastName = "Codez";
 $customerPassword = "phpcodez";
 
 $customer
 ->setWebsiteId($website)
 ->setEmail($customerEmail)
 ->setFirstname($customerFirstName)
 ->setLastname($customerLastName)
 ->setPassword($customerPassword);
 $customer->save();
 
 echo $i.". Account is created for ".$customerEmail."\n";
 }
 }
 public function execute() {
 $this->state->setAreaCode('frontend');

$customer = $this->customerFactory->create();
 $customer
 ->setWebsiteId($this->data->getOption(self::ARG_WEBSITE))
 ->setEmail($this->data->getOption(self::ARG_EMAIL))
 ->setFirstname($this->data->getOption(self::ARG_FIRSTNAME))
 ->setLastname($this->data->getOption(self::ARG_LASTNAME))
 ->setPassword($this->data->getOption(self::ARG_PASSWORD));
 $customer->save();

$this->customerId = $customer->getId();

if($this->data->getOption(self::ARG_SENDEMAIL)) {
 $customer->sendNewAccountEmail();
 } 
 }


 public function getCustomerId()
 {
 return (int)$this->customerId;
 }
}

Install the module.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean

Issue the following command to create single account

php bin/magento phpcodez:customer:create --firstname="PHP" --lastname="Codez" --email="PHPCodez@gmail.com" --password="phpcodez17" --website="1"

Issue the following command to create multiple accounts(Max is 100)

php bin/magento phpcodez:customers:account:create

Leave a Reply

Your email address will not be published. Required fields are marked *