Magento 2 Save Custom Customer Attribute To save custom customer attribute in magento 2, follow the below Sample Code Snippet. For Illustration Purpose the object manager is called directly and the CustomerRepositoryInterface class is instantiated. But in Real time the code structure will differ, the dependencies has to be injected via the constructor.
|
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $objectManager->get('Magento\Framework\App\State')->setAreaCode('frontend'); $customerRepository = $objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface'); $customer = $customerRepository->getById(1); $customer-setCustomAttribute("your_attribute_code_here",'test new'); $customerRepository->save($customer); $customerAttrValue = $customer->getCustomAttribute('your_attribute_code_here'); print_r($customerAttrValue); |