Skip to main content

Magento 2: Reset customer password programatically

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);


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

Popular posts from this blog

Magento 2: How to add custom script to head tag?

There are two ways to add custom script to head tag in Magento 2: add custom script through admin-panel add custom script through XML If you want to add custom script in head tag through admin-panel, just login to the admin and browse the navigation Content => Design => Configuration , Now click on edit link of current theme and Go to HTML Head tab and inside scripts and style sheets text-area you can enter your custom script.  If you want to add custom script through XML, just create a XML file on following location: app/design/frontend/packagename/themename/Magento_Theme/layout/default_head_blocks.xml and add the following code in this XML to call template file: <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body>         <referenceBlock name="head.additional">    ...

Magento 2: How to add menu to the admin grid?

To add new menu in admin grid, first create an menu.xml file to your module on following location: Packagename/Modulename/etc/adminhtml/menu.xml then place the following code in menu.xml: <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Backend/etc/menu.xsd">     <menu>          <add id="Packagename_Modulename::urlkey" title="Menu Title" module="Packagename_Modulename"  sortOrder="10" resource="Packagename_Modulename::urlkey"/>        <add id="Packagename_Modulename::urlkey_controller_action" title="Sub Menu Title" module="Packagename_Modulename"  sortOrder="10" action="urlkey/controller/action" resource="Packagename_Modulename::urlkey_controller_action" parent="Packagename_Modulename::urlkey" />     </menu> </config> ...

Magento 2: Print Invoice PDF or Shipment PDF is not working : sh: 1: xvfb-run: not found

Some times in Magento 2, while we try to print Invoice PDF or Shipment PDF or any other PDF, then it displays following message: sh: 1: xvfb-run: not found To resolve this issue, first login to your project through SSH and run following commands on the root location of your project: sudo apt-get install --fix-missing -y xvfb sudo apt-get update sudo apt-get install xvfb libfontconfig wkhtmltopdf