Change Customer Password Programmatically Magento 2

Add This Code In Controller


  
<?php

namespace Custom\Module\Controller\Index;
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
class Customerpassword extends \Magento\Framework\App\Action\Action
{

 /**
     * @var \Magento\Framework\App\Cache\TypeListInterface
     */
    protected $_cacheTypeList;

    /**
     * @var \Magento\Framework\App\Cache\StateInterface
     */
    protected $_cacheState;

    /**
     * @var \Magento\Framework\App\Cache\Frontend\Pool
     */
    protected $_cacheFrontendPool;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Action\Context $context
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\Framework\App\Cache\StateInterface $cacheState
     * @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
        /**
     * Customer Repository.
     *
     * @var \Magento\Customer\Api\CustomerRepositoryInterface
     */
    protected $_customerRepository;

    /**
     * Encryptor.
     *
     * @var \Magento\Framework\Encryption\Encryptor
     */
    protected $_encryptor;

    /**
     * Customer registry.
     *
     * @var \Magento\Customer\Model\CustomerRegistry
     */
    protected $_customerRegistry;

    /**
     * AccountManagementPlugin constructor.
     *
     * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
     * @param \Magento\Framework\Encryption\Encryptor           $encryptor
     * @param \Magento\Customer\Model\CustomerRegistry          $customerRegistry
     */
    protected $_customerRepositoryInterface;
    protected $_customer;
    public function __construct(
       \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
        \Magento\Framework\App\Cache\StateInterface $cacheState,
        \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
         \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        \Magento\Framework\Encryption\Encryptor $encryptor,
        \Magento\Customer\Model\CustomerRegistry $customerRegistry,
        \Magento\Customer\Model\Customer $customer
         
    ) {
        parent::__construct($context);
        $this->_cacheTypeList = $cacheTypeList;
        $this->_cacheState = $cacheState;
        $this->_cacheFrontendPool = $cacheFrontendPool;
        $this->resultPageFactory = $resultPageFactory;
        $this->_customerRepository = $customerRepository;
        $this->_encryptor          = $encryptor;
        $this->_customerRegistry   = $customerRegistry;
         $this->_customer = $customer;
         $this->encryptor = $encryptor;
    }
 
    /**
     * Flush cache storage
     *
     */
    public function execute()
    {   
         
         $customerId="10";
         $pass="magento@123";

        //$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        //$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);
                     
           $customer=$this->_customer->load($customerId);       //You Can load by Factory Methord or By ObjectManger
          
           
          $encrypt = $this->encryptor->getHash($pass);
        
          $customer2 = $customer->setPasswordHash($encrypt)->save();
    


       echo "you have sucessfully changed the password .you can login with this Email address"; echo "<br>";

       echo "Email==".$Emailid=$customer->getEmail(); echo "<br>";
      

        
        
    }

}

Comments

Popular posts from this blog

Through Postman Api send data through JSON and create simple product in Magento2

Getting product Image in PHTML in Magento2

programmaticlly Create Order in Magento2 with custom options in order