Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Customer Login Password in Magento

Tags:

php

magento

I want to Change Login Password of a customer in Magento, I Uses the below Code to Update the Password But It doesn't work for me

 $customerid = 46;
 $oldpassword = 12345678;
 $newpassword = 87654321;
 $customer = Mage::getModel('customer/customer')->load($customerid);
 $passwordhash = $customer['password_hash'];
 $phasharray = explode(":",$passwordhash);
 $passpostfix = $phasharray[1];
 $completeOldPassword =  $oldpassword.":".$passpostfix;
 if($completeOldPassword==$passwordhash){
    $customer->setPassword($newpassword);
    $customer->save();
 }
like image 462
Shashi Avatar asked Dec 27 '13 14:12

Shashi


2 Answers

Use below code to update user's password, you need username and storeid additional variable to update. :-

$validate = 0; $result = '';
$customerid = 46;
$username = 'YOUR_USERNAME';
$oldpassword = 12345678;
$newpassword = 87654321;
$storeid = 'YOUR_STORE_ID';

$websiteId = Mage::getModel('core/store')->load($storeid)->getWebsiteId();
try {
     $login_customer_result = Mage::getModel('customer/customer')->setWebsiteId($websiteId)->authenticate($username, $oldpassword);
     $validate = 1;
}
catch(Exception $ex) {
     $validate = 0;
}
if($validate == 1) {
     try {
          $customer = Mage::getModel('customer/customer')->load($customerid);
          $customer->setPassword($newpassword);
          $customer->save();
          $result = 'Your Password has been Changed Successfully';
     }
     catch(Exception $ex) {
          $result = 'Error : '.$ex->getMessage();
     }
}
else {
     $result = 'Incorrect Old Password.';
}
echo $result;
like image 53
Aarush Avatar answered Nov 05 '22 00:11

Aarush


If you echo $completeOldPassword and $passwordhash you see they can't match because your $oldpassword is supposed to be a password hash with salt. Now it's just plaintext

If you only want to change the password without checking old password try this

$customerid = 46;
$newpassword = 87654321;
$customer = Mage::getModel('customer/customer')->load($customerid);
$customer->setPassword($newpassword);
$customer->save();
like image 40
Jompper Avatar answered Nov 04 '22 23:11

Jompper