Is there a possibility to create a customer and also to create a password for him, which he can then change if he wants to?
You can create only a customer without a password. After creating a customer, you need to send a "Reset Password" link to the customer. If you want to create a customer with a password then you need to create a custom module for the same. Please follow the steps below for the same:
1) Create your own module in this path: /app/code/Vendor/Module/
2) Create a registration.php file in this path: /registration.php using the following code:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
3) Create a module.xml file in this path: /etc/module.xml using the following code:
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="2.0.1"></module>
</config>
4) Create a di.xml file in this path: /etc/di.xml using the following code:
<?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>
5) Create a CustomerUserCreateCommand.php file in this path: /Console/Command/CustomerUserCreateCommand.php using the following code:
<?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 customer...</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_NEW_CUST_WEBSITE));
$output->writeln('<comment>Customer First Name: '.$input->getOption(Customer::KEY_NEW_CUST_FIRSTNAME));
$output->writeln('<comment>Customer Last Name: '.$input->getOption(Customer::KEY_NEW_CUST_LASTNAME));
$output->writeln('');
$output->writeln('<comment>Customer Email: '.$input->getOption(Customer::KEY_NEW_CUST_EMAIL));
$output->writeln('<comment>Customer Password: '.$input->getOption(Customer::KEY_NEW_CUST_PASSWORD));
}
protected function getOptionsList()
{
return [
new InputOption(Customer::KEY_NEW_CUST_FIRSTNAME, null, InputOption::VALUE_REQUIRED, '(Required) Customer First Name'),
new InputOption(Customer::KEY_NEW_CUST_LASTNAME, null, InputOption::VALUE_REQUIRED, '(Required) Customer Last Name'),
new InputOption(Customer::KEY_NEW_CUST_EMAIL, null, InputOption::VALUE_REQUIRED, '(Required) Customer Email'),
new InputOption(Customer::KEY_NEW_CUST_PASSWORD, null, InputOption::VALUE_REQUIRED, '(Required) Customer Password'),
new InputOption(Customer::KEY_NEW_CUST_WEBSITE, null, InputOption::VALUE_REQUIRED, '(Required) Website ID'),
new InputOption(Customer::KEY_NEW_CUST_SENDEMAIL, 0, InputOption::VALUE_OPTIONAL, '(1/0) Send Email? (default 0)')
];
}
}
6) Create a Customer.php file in this path: /Helper/Customer.php using the following code:
<?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_NEW_CUST_EMAIL = 'customer-email';
const KEY_NEW_CUST_FIRSTNAME = 'customer-firstname';
const KEY_NEW_CUST_LASTNAME = 'customer-lastname';
const KEY_NEW_CUST_PASSWORD = 'customer-password';
const KEY_NEW_CUST_WEBSITE = 'website';
const KEY_NEW_CUST_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_NEW_CUST_WEBSITE))
->setEmail($this->data->getOption(self::KEY_NEW_CUST_EMAIL))
->setFirstname($this->data->getOption(self::KEY_NEW_CUST_FIRSTNAME))
->setLastname($this->data->getOption(self::KEY_NEW_CUST_LASTNAME))
->setPassword($this->data->getOption(self::KEY_NEW_CUST_PASSWORD));
$customer->save();
$this->customerId = $customer->getId();
if($this->data->getOption(self::KEY_NEW_CUST_SENDEMAIL)) {
$customer->sendNewAccountEmail();
}
}
public function getCustomerId()
{
return (int)$this->customerId;
}
}7) Login to putty and goto the Magento root directory
8) Please run the following commands:
php bin/magento customer:user:create --customer-firstname="James" --customer-lastname="Ontario" --customer-email="jameso@gmail.com" --customer-password="james@123" --website="1" --send-email="1"
If you find our reply helpful, please give us kudos.
A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.
WebDesk Solution Support Team
Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Thank You,
WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Location: 150 King St. W. Toronto, ON M5H 1J9