3

Context:

Account Confirmation is set to required from backend.

I'm creating a new customer account using \Magento\Customer\Model\AccountManagementFactory::createAccount() method.

        $email = "[email protected]";
        $firstName = "Jiro";
        $lastName = "Ono";

        $customer = $this->customerFactoryData->create();

        $customer->setEmail($email);
        $customer->setFirstname($firstName);
        $customer->setLastname($lastName);

        // Make sure we have a storeId to associate this customer with.
        $storeId = $this->storeManager->getStore()->getId();
        $customer->setStoreId($storeId);


        // Associate website_id with customer
        $websiteId = $this->storeManager->getStore($customer->getStoreId())->getWebsiteId();
        $customer->setWebsiteId($websiteId);

        $this->accountManagement->createAccount($customer);

Problem:

This successfully creates an user account. And send an email to the user to set their password. Problem is that I need confirmation on this client as well, but they are shown in grid as confirmed by default even if the user never sets is password or never opens mail or never enters the website.

I can't seem to find the logic that sets the client as unconfirmed. I suspect is a plugin or an observer.

Any tips or suggestions are highly appreciated. Thanks.

EDIT https://github.com/magento/magento2/issues/14492

1 Answer 1

0

Go to Stores -> Configuration -> Customer Configuration -> Create New Account Options

Now Set Require Emails Confirmation to Yes

Clear cache.

[Update]

Overwrite Magento/Customer/Model/AccountManagement.php class. Modify following method


/**
 * Send either confirmation or welcome email after an account creation
 *
 * @param CustomerInterface $customer
 * @param string $redirectUrl
 * @return void
 */
protected function sendEmailConfirmation(CustomerInterface $customer, $redirectUrl)
{
    try {
        $hash = $this->customerRegistry->retrieveSecureData($customer->getId())->getPasswordHash();
        $templateType = self::NEW_ACCOUNT_EMAIL_REGISTERED;
        if ($this->isConfirmationRequired($customer) && $hash != '') {
            $templateType = self::NEW_ACCOUNT_EMAIL_CONFIRMATION;
        } elseif ($hash == '') {
            $templateType = self::NEW_ACCOUNT_EMAIL_REGISTERED_NO_PASSWORD;
        }
        $this->getEmailNotification()->newAccount($customer, $templateType, $redirectUrl, $customer->getStoreId());
    } catch (MailException $e) {
        // If we are not able to send a new account email, this should be ignored
        $this->logger->critical($e);
    }
}

In your case $hash is empty. So change that logic.

3
  • Please read question carefully. Specially this part 'Account Confirmation is set to required from backend.' Commented Sep 21, 2017 at 16:01
  • Check updated answer.
    – Sohel Rana
    Commented Sep 21, 2017 at 16:03
  • Overriding the logic of this method will not solve the problem. Sure a confirmation email will be sent but the confirmation token will be null as no confirmation token was generated.The general behavior will not change. Customers will still appear as confirmed even if they never confirm their account. Commented Sep 21, 2017 at 16:12

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.