Programmatically update customer data in Magento 2
Using Object Manager
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); $websiteId = $storeManager->getStore()->getWebsiteId(); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerRepository = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface'); // load customer info by email $customer = $customerRepository->get('[email protected]', $websiteId); // load customer info by id $customer = $customerRepository->getById(1); // Update customer Info $customer->setFirstname('John'); $customer->setLastname('Smith'); $customer->setEmail('[email protected]'); $customer->setCustomAttribute('attribute_code', 'attribute value'); $customerRepository->save($customer);
To get the attribute value you can use this code
$customer->getCustomAttribute('attribute_code')->getValue()