5

I'm trying to create customer account manually in my custom module. Customer account is getting created successfully and welcome email is sent to the new customer using $customer->sendNewAccountEmail();. But On clicking the forgot password link I found the rp_token is missing in the template.

If we're programmatically creating customer_entity table is not updating. If the customer is registered via Magento default create account page in the frontend the rp_token is updating successfully.

How to create and insert rp_token on creating customer account programmatically? See my customer creation code below

        $orderArray = $this->getRequest()->getPost('order');
        $sellerComment = $orderArray['comment']['customer_note'];
        $id = $this->convert->_getSession()->getCustomerId();
        $websiteId = $this->_storeManager->getStore()->getId();
        $customerEmail = $orderArray['account']['email'];
        $customerFirstName = $orderArray['billing_address']['firstname'];
        $customerLastName = $orderArray['billing_address']['lastname'];
        $customerCompany = $orderArray['billing_address']['company'];
        $customerCity = $orderArray['billing_address']['city'];
        $customerCountryId = $orderArray['billing_address']['country_id'];
        $customerPostcode = $orderArray['billing_address']['postcode'];
        $customerTelephone = $orderArray['billing_address']['telephone'];
        $customerFax = $orderArray['billing_address']['fax'];
        $customerStreetArray = $orderArray['billing_address']['street'];
        $customerStreet = implode(',', $customerStreetArray);
        $customerID = '';

        /* if new customer */
        if ($id == 0) {
            $chars = \Magento\Framework\Math\Random::CHARS_LOWERS
                    . \Magento\Framework\Math\Random::CHARS_UPPERS
                    . \Magento\Framework\Math\Random::CHARS_DIGITS;

            $password = $this->mathRandom->getRandomString(10, $chars);
            $customer = $this->_customerFactory->create();
            $customer->setWebsiteId($websiteId);
            $customer->setEmail($customerEmail);
            $customer->setFirstname($customerFirstName);
            $customer->setLastname($customerLastName);
            $customer->setPassword($password);
            $customer->setForceConfirmed(true);
            $customer->save();
            $customerID = $customer->getId();
            $address = $this->_addressFactory->create();
            $address->setCustomerId($customer->getId())
                    ->setFirstname($customerFirstName)
                    ->setLastname($customerLastName)
                    ->setCountryId($customerCountryId)
                    ->setPostcode($customerPostcode)
                    ->setCity($customerCity)
                    ->setTelephone($customerTelephone)
                    ->setFax($customerFax)
                    ->setCompany($customerCompany)
                    ->setStreet($customerStreet)
                    ->setIsDefaultBilling('1')
                    ->setIsDefaultShipping('1')
                    ->setSaveInAddressBook('1');

            $address->save();
            $customer->sendNewAccountEmail();
        }
2
  • please post your code for customer account creation
    – Keyur Shah
    Commented Jan 11, 2019 at 9:06
  • @KeyurShah Please have a look at my updated question Commented Jan 11, 2019 at 9:11

1 Answer 1

3

This is how Magento itself does it:

// This line create a new token :
$newPasswordToken = $this->mathRandom->getUniqueHash();

// Get the secure data from the customer
$customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId());

// Those lines update the rp_token and rp_token_created_at
$customerSecure->setRpToken($newPasswordToken);
$limitTokenDate = $this->dateTimeFactory->create()->format(DateTime::DATETIME_PHP_FORMAT);

$customerSecure->setRpTokenCreatedAt($limitTokenDate);
$this->customerRepository->save($customer);

Of course, this snippet need some dependencies :

Magento\Framework\Intl\DateTimeFactory    
Magento\Framework\Math\Random    
\Magento\Customer\Model\CustomerRegistry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.