Magento 2

How to change Customer Password Programmatically in Magento 2

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!

Click to rate this post!
[Total: 12 Average: 4.8]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago