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

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

2 days ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

5 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

7 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

7 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

1 week ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago