Magento is one of made for E-commerce CMS that knows for its functionality, flexible code structure and performance. And it uses MySQL as storage backend to store and retrieve data in tabular format. Where it stores all useful information like customer data, order data, and configurations.
But many time it happens that your customer is unable to login in store frontend or they forget their reset details or store emails is not working in such case how to reset the password of your customer account? If you are a newbie it becomes more difficult for you to resolve your customer’s login issue. But if you are Magento expert, you can easily reset password and resume their login few tricks. So, we are back again with a small PHP script using which you can quickly reset the customer account password.
Simply create one “change_password.php” file in your Magento root using the following code. Don’t forget to specify customer id and password that you want to set inside the code.
<?php use Magento\Framework\AppInterface; try { require_once __DIR__ . '/app/bootstrap.php'; } catch (\Exception $e) { echo 'Autoload error: ' . $e->getMessage(); exit(1); } try{ $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $appState = $objectManager->get('\Magento\Framework\App\State'); $customerRepositoryInterface = $objectManager->get('\Magento\Customer\Api\CustomerRepositoryInterface'); $customerRegistry = $objectManager->get('\Magento\Customer\Model\CustomerRegistry'); $encryptor = $objectManager->get('\Magento\Framework\Encryption\EncryptorInterface'); $appState->setAreaCode('frontend'); $customerId = 1; // here assign your customer id $password = "custom_password"; // set your custom password $customer = $customerRepositoryInterface->getById($customerId); // _customerRepositoryInterface is an instance of \Magento\Customer\Api\CustomerRepositoryInterface $customerSecure = $customerRegistry->retrieveSecureData($customerId); // _customerRegistry is an instance of \Magento\Customer\Model\CustomerRegistry $customerSecure->setRpToken(null); $customerSecure->setRpTokenCreatedAt(null); $customerSecure->setPasswordHash($encryptor->getHash($password, true)); // here _encryptor is an instance of \Magento\Framework\Encryption\EncryptorInterface $customerRepositoryInterface->save($customer); echo 'Successfully Changes Your Password.'; } catch(\Exception $e){ print_r($e->getMessage()); }
That’s it. On successful execution of the script, your customer will able to login their account using the defined password. In such an emergency, resetting the password using PHP script becomes a life savior way for the developer.
Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issue while implementing this code.
Happy Resetting!