Magento 2 Customer Confirmation – Confirm Magento 2 Email Programmatically

confirmationcustomermagento2

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 ?

Best Answer

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();