Programmatically change customer password after validating old password using magento

Programmatically change customer password after validating old password using magento

In this post we will show you, however you’ll Programmatically change customer password after validating old password.

we have written the code snipping to allow you to understand the method, what you wish is basic client/customer knowledge like its entity id, email, recent secret, new secret and magento’s store id.

So lets get begin with the below easy snipplet code, I hope you may know it as a result of it’s thus easy.

$password_change_status = array();
	// default magento's store id
    $current_store_id = 1; 
	// customer entity id
    $customer_id = 116; 
	// email of customer / user name
    $user_name = "ingo@onlinecode"; 
	// replace "ADD_OLD_PASSWORD" with old password
    $old_password = base64_decode("ADD_OLD_PASSWORD");
	// replace "ADD_NEW_PASSWORD" with old password	
    $new_password = base64_decode("ADD_NEW_PASSWORD"); 
    // assume old password is incorrect
    $vali_date = 0;
	
    $website_id = Mage::getModel('core/store')->load($current_store_id)->getWebsiteId();
    // now validate old password is correct or not
    try 
	{
        $login_customer_result = Mage::getModel('customer/customer')->setWebsiteId($website_id)->authenticate($user_name, $oldpassword);
        $vali_date = 1;
    }
    catch(Exception $exception) 
	{
        $vali_date = 0;
    }
    // now change the password
    if($vali_date == 1) 
	{
        try 
		{
            $customer_data = Mage::getModel('customer/customer')->load($customer_id);
            $customer_data->setPassword($new_password);
            $customer_data->save();   
				
			// pass result data (sucess data)	
            $password_change_status = array (
                "customerid"    => $customer_id,
                "oldpassword"   => $old_password,
                "newpassword"   => $new_password,
                "change_status" => 1,
                "message"       => 'Password has been Changed Successfully.'
            );
        }
        catch(Exception $exception) {
			// pass result data (exception data)
             $password_change_status = array (
                 "customerid"    => $customer_id,
                 "oldpassword"   => $old_password,
                 "newpassword"   => $new_password,
                 "change_status" => -1,
                 "message"       => 'Error In password : '.$exception->getMessage
             );
        }
    }
    else 
	{
		// pass result data
        $password_change_status = array (
             "customerid"    => $customer_id,
             "oldpassword"   => $old_password,
             "newpassword"   => $new_password,
             "change_status" => 0,
             "message"       => 'Old Password Is Incorrect .'
        );
    }
    echo "<pre>"; 
    // print result (result on array)
    print_r($password_change_status);
    echo "</pre>";

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  13  =  18

We accept paid advance and paid guest Posting on our Site : Contact US