2

How do we change a customer's email programatically in Magento 2?

1
  • Let me know if you have any issue Oct 2, 2017 at 4:54

1 Answer 1

3

Use \Magento\Customer\Api\CustomerRepositoryInterface to change customer information.

protected $_customerRepositoryInterface;

public function __construct ( 
    ...
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
    ...
) {
    ...
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
    ...
}

public function changeCustomerInfo()
{
    $customerId = '1'; // customer ID
    $customer = $this->_customerRepositoryInterface->getById($customerId);
    $customer->setEmail("[email protected]");  //set customer email
    $customer->setFirstname("First Name"); //set customer First Name
    $customer->setLastname("Last name"); // set customer Last Name
    $this->_customerRepositoryInterface->save($customer);
}
0

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.