Confirm magento 2 email programmatically
-
15-04-2021 - |
Pergunta
I set the configuration for Magento 2 customer to do email confirmation after register and change e-mail, is there a way to confirm customer e-mail programmatically only by customer id or customer e-mail?
$customerId = '35';
$customerEmail = 'test@test.com';
$this->confirmCustomerEmailByCustomerId($customerId); //how ?
$this->confirmCustomerEmailByCustomerEmail($customerEmail); //how ?
Solução
You can confirm the customer registration as following
<?php
private $customer;
public function __construct(
...
\Magento\Customer\Model\Customer $customer,
...
) {
...
$this->customer = $customer;
...
}
...
$customerId = '1';
$customer = $this->customer->load($customerId);
$customer->setConfirmation(null);
$customer->save();
Outras dicas
When I created accounts, they were already confirmed, but they were disabled! This fixed it:
$customer = $this->customer->load($customerId);
$customer->setConfirmation(null);
$customer->setStatus(1);
$customer->save();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange