How to Reset Customer Password using Console Command in Magento 2
Step 1: First, we need to create a “di.xml” file inside the extension etc directory of our extension.
app\code\Vendor\Extension\etc
Then add the below 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="Vendor\Extension\Command\Changepasswordcommand">
<arguments>
<argument name="resource" xsi:type="object">Magento\Customer\Model\ResourceModel\Customer\Proxy</argument>
</arguments>
</type>
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="customer-change-password-console-command"
xsi:type="object">Vendor\Extension\Command\Changepasswordcommand
</item>
</argument>
</arguments>
</type>
</config>
Step 2: After that, we need to create a “Changepasswordcommand.php” file inside the extension at the following path.
app\code\Vendor\Extension\Command\
Now add the below-mentioned code
<?php
namespace Vendor\Extension\Command;
use Magento\Customer\Model\Customer;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\ResourceModel\Customer as CustomerResource;
use Magento\Framework\App\Area;
use Magento\Framework\App\State as AppState;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\StoreManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Changepasswordcommand extends Command
{
private $customerFactory;
private $customerResource;
private $storeManager;
private $input;
private $appState;
public function __construct(
CustomerFactory $customerFactory,
StoreManagerInterface $storeManager,
CustomerResource $resource,
AppState $state
) {
parent::__construct();
$this->customerFactory = $customerFactory;
$this->customerResource = $resource;
$this->storeManager = $storeManager;
$this->appState = $state;
}
protected function configure()
{
$this->setName('customer:change-password');
$this->setDescription('Set a customers password');
$this->addOption(
'website',
'w',
InputOption::VALUE_OPTIONAL,
'Website code if customer accounts are website scope'
);
$this->addArgument('email', InputArgument::REQUIRED, 'Customer Email');
$this->addArgument('password', InputArgument::REQUIRED, 'Password to set');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
try {
$this->appState->setAreaCode(Area::AREA_ADMINHTML);
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
} catch (LocalizedException $exception) {
}
$customer = $this->getCustomerByEmail($this->getEmail());
$customer->setPassword($this->getPassword());
$this->customerResource->save($customer);
$output->writeln(sprintf('Updated password for customer "%s".', $this->getEmail()));
}
private function getEmail(): string
{
return $this->input->getArgument('email') ?? '';
}
private function getPassword(): string
{
return $this->input->getArgument('password') ?? '';
}
private function getWebsiteCode(): string
{
return $this->input->getOption('website') ?? '';
}
private function getWebsiteIdByCode(string $code): int
{
$website = $this->storeManager->getWebsite($code);
if (!$website->getId()) {
throw new \InvalidArgumentException(sprintf('No website with ID "%s" found.', $code));
}
return (int)$website->getId();
}
private function getCustomerByEmail(string $email): Customer
{
$customer = $this->customerFactory->create();
if ($this->getWebsiteCode()) {
$websiteId = $this->getWebsiteIdByCode($this->getWebsiteCode());
$customer->setWebsiteId($websiteId);
}
$this->customerResource->loadByEmail($customer, $email);
if (!$customer->getId()) {
throw new \InvalidArgumentException(sprintf('No customer with email "%s" found.', $this->getEmail()));
}
return $customer;
}
}
Comments
Post a Comment