3

Following Problem.

I get customers with an interface in to my Magento 2.1.

They are stored with a placeholder email. In my controller I update the mail address and want to set confirmation, so that the customer has to confirm his email on login.

$customer->setEmail($email);
$customer->setConfirmation(true);
//$customer->setConfirmation("Random-string"); //Did not work too
$this->_customerRepository->save($customer);

The email is updated but the confirmation flag is still NULL in the DB. How can I update it?

1 Answer 1

1

Your code looks good.

I'm pretty sure this is caused by some condition not being filled in the resource model.

As you can see from the following, there's a _beforeSave method in Magento\Customer\Model\ResourceModel\Customer that has some code that might rewrite the value of the confirmation data:

    // set confirmation key logic
    if ($customer->getForceConfirmed() || $customer->getPasswordHash() == '') {
        $customer->setConfirmation(null);
    } elseif (!$customer->getId() && $customer->isConfirmationRequired()) {
        $customer->setConfirmation($customer->getRandomConfirmationKey());
    }
    // remove customer confirmation key from database, if empty
    if (!$customer->getConfirmation()) {
        $customer->setConfirmation(null);
    }

I reckon you should try to debug that bit and see if the method does not go in the first condition (force confirmed or password hash empty).

Try to check the data of your custom before you call the save() method. Hopefully that'll help you narrow down the issue

1
  • hey, thanks that helped. the problem is the password. the user does not have a password yet. it's generated in the next step (/customer/account/createPassword) . if i want to set a default password before the save() method like $customer->setPassword("password"), it stops without an error. how can i set a password correct?
    – Umfi
    Apr 20, 2017 at 11:15

Your Answer

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

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