Hi all,
I'm trying to do something relatively simple.. I'm just programmatically creating customers (which works as long as I don't try to generate a password for them). If I assign a plaintext password, it also works fine.
$newCustomer = $this->customerFactory->create() ->setWebsiteId($this->websiteId) ->setEmail($customerEmail) ->setFirstname(ucwords(strtolower($netsuiteCustomer->firstName))) ->setMiddleName(ucwords(strtolower($netsuiteCustomer->middleName))) ->setLastname(ucwords(strtolower($netsuiteCustomer->lastName))) ->setCreatedAt($netsuiteCustomer->dateCreated) ->setPassword($newCustomer->generatePassword()) ->save();
The error I'm getting is that the generatePassword method is invalid. Any idea how I can assign the new customer an automatically generated password?
Thanks in advance.
Hi @Jrb1x
I am also facing similar issue. Were you able to generate the passwords for the customers ?
You can just set password programatically as below way,
public function createCustomer(){ $newpassword = $this->rand_string(12); $newCustomer = $this->customerFactory->create() ->setWebsiteId($this->websiteId) ->setEmail($customerEmail) ->setFirstname(ucwords(strtolower($netsuiteCustomer->firstName))) ->setMiddleName(ucwords(strtolower($netsuiteCustomer->middleName))) ->setLastname(ucwords(strtolower($netsuiteCustomer->lastName))) ->setCreatedAt($netsuiteCustomer->dateCreated) ->setPassword($newpassword) ->save(); } public function rand_string($length) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; return substr(str_shuffle($chars), 0, $length); }
You can generate random string with your password length.
This solutions is working fine for new customer programatically.
if you have any issue let me know.
if issue resolved, click kudos/accept as solutions.
I used following code to generate random passwords for the customers.
<?php namespace Muk\Common\Helper; class CustomerPasswordGeneration extends \Magento\Framework\App\Helper\AbstractHelper { /** * @var \Magento\Framework\Math\Random */ protected $mathRandom; public function __construct( Context $context, \Magento\Framework\Math\Random $mathRandom ) { $this->mathRandom = $mathRandom; parent::__construct($context); } /** * Retrieve random password * * @param int $length * @return string */ public function generatePassword($length = 10) { $chars = \Magento\Framework\Math\Random::CHARS_LOWERS . \Magento\Framework\Math\Random::CHARS_UPPERS . \Magento\Framework\Math\Random::CHARS_DIGITS; return $password = $this->mathRandom->getRandomString($length, $chars); }