Magento 2 Update Customer Programmatically


Magento 2 Update Customer Programmatically Sometimes we need to update customer information programmatically, It is very simple to update customer information in Magento2. Here in this tutorial we are going to explain how you can Update Customer Data programmaticaly.


Magento 2 Update Customer Programmatically | Attribute Value Update Example

Magento 2 Update Customer Programmatically

You can use the dependency injection in controller & update customer information(attribute) programmatically-

Magento 2 Update Customer Attribute & Data Programmatically Example:

protected $_customerRepoInterface;

public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepoInterface
) {
    $this->_customerRepoInterface = $customerRepoInterface;
}

/************Update  Part ********/

public function execute()
  {
$websiteId = 2;
$customer = $this->_customerRepoInterface->get('jhon@example.com', $websiteId);
// UpdateCustomer Info
$customer->setFirstname('Jhon');
$customer->setLastname('Dee');
$customer->setEmail('jhon2@example.com');
$customer->setMyCustomAttribute('abc');
$this->_customerRepoInterface->save($customer);
   }
 

So using the above method you can update customer data such as firstname, lastname, telephone, email or any custom attribute.


Advertisements

Add Comment

📖 Read More