There are 2 ways to reset customer password programmatically in Magento2:
1) By creating a php file on root directory of project:
Create a new php file on root directory of your project and place the following code in that file:
and change these two varaibles in this code:
$passwordhash for new password
$email for customer email
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
class MyEncryptor extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface
{
private $encryptor;
public function __construct(
\Magento\Framework\App\State $state,
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
\Magento\Framework\App\Response\Http $response
) {
$this->_response = $response;
$this->encryptor = $encryptor;
$state->setAreaCode('adminhtml');
}
function launch()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$email = "abc@yopmail.com";
$passwordhash= $this->encryptor->hash("enter_password_here") . PHP_EOL;
$newpass = trim($passwordhash);
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('customer_entity');
$sql = 'Update ' . $tableName . ' Set password_hash = "'.$newpass.'" where email = "'.$email.'"';
$connection->query($sql);
return $this->_response;
}
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('MyEncryptor');
$bootstrap->run($app);
and change these two varaibles in this code:
$passwordhash for new password
$email for customer email
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
class MyEncryptor extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface
{
private $encryptor;
public function __construct(
\Magento\Framework\App\State $state,
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
\Magento\Framework\App\Response\Http $response
) {
$this->_response = $response;
$this->encryptor = $encryptor;
$state->setAreaCode('adminhtml');
}
function launch()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$email = "abc@yopmail.com";
$passwordhash= $this->encryptor->hash("enter_password_here") . PHP_EOL;
$newpass = trim($passwordhash);
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('customer_entity');
$sql = 'Update ' . $tableName . ' Set password_hash = "'.$newpass.'" where email = "'.$email.'"';
$connection->query($sql);
return $this->_response;
}
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('MyEncryptor');
$bootstrap->run($app);
1) By executing the following mysql query on your mysql server:
Note: before executing this query please change these 2 variables with customer password and customer entity id.
`UPDATE `customer_entity`
SET `password_hash` = CONCAT(SHA2('xxxxxxxxWRITE_YOUR_PASSWORD_HERE', 256), ':xxxxxxxx:1') WHERE `entity_id` = WRITE_CUSTOMER_ENTITY_ID_HERE;
Comments
Post a Comment