How to update the customer information in Magento 2 (Code to set data for customer).
Example: Update email_backup
column of customer_entity
table.
-
Within Magento or outside Magento? Which information?– JacksonDec 20, 2016 at 7:49
-
I just updated my question.– MrTo-KaneDec 20, 2016 at 7:53
1 Answer
The email_backup
is the custom customer attribute. We can use Magento\Customer\Api\CustomerRepositoryInterface
to save the customer info.
/**
* @var CustomerRepositoryInterface
*/
protected $customerRepository;
public function __construct(
CustomerRepositoryInterface $customerRepository
)
{
$this->customerRepository = $customerRepository;
}
We can save:
$customer = $this->customerRepository->getById($customerId);
$customer->setCustomAttribute('email_backup', $emailBackupData);
$this->customerRepository->save($customer);