2

How to save customer attribute value in custom script in Magento 2.

The prerequisite is to add first your new attribute programatically.

To do add a customer attribute please review these:

2 Answers 2

17

This is how it can be done with customer save method:

/** @var \Magento\Customer\Model\CustomerFactory $customerFactory */ /** @var \Magento\Customer\Model\Customer $customer */ /** @var \Magento\Customer\Model\Data\Customer $customerData */ $customer = $customerFactory->create() $customerData = $customer->getDataModel(); $customerData->setCustomAttribute('my_attr_code', $val); $customer->updateData($customerData); $customer->save(); 

And this is how it can be done updating only one attribute value:

/** @var \Magento\Customer\Model\CustomerFactory $customerFactory */ /** @var \Magento\Customer\Model\Customer $customer */ $customer = $customerFactory->create(); //$customer->setId($customerId); // This seem redundant /** @var \Magento\Customer\Model\Data\Customer $customerData */ // interface \Magento\Customer\Api\Data\CustomerInterface $customerData = $customer->getDataModel(); $customerData->setId($customerId); $customerData->setCustomAttribute('my_custom_attribute', $value); $customer->updateData($customerData); /** @var \Magento\Customer\Model\ResourceModel\Customer $customerResource */ /** @var \Magento\Customer\Model\ResourceModel\CustomerFactory $customerResourceFactory */ $customerResource = $customerResourceFactory->create(); if ($value != "") { $customerResource->saveAttribute($customer, 'my_custom_attribute'); } 
6
  • I have followed the same, but its not saving. could you please help me. Commented Feb 20, 2017 at 14:53
  • Dunno why but i got my script working with this approach, whereas using $customer->setCustomAttribute('attr','val') was working only on the first entry I was saving. in my dev environment it was working also the other way, but not in my stage environment... really strange. Any clue? Commented Feb 22, 2018 at 9:34
  • I saw this kind of behaviour too. I don't recall if there were products or customers. And Magento 2 was in a kind of infinite loop. The debug trace had Gbs. Commented Feb 22, 2018 at 9:37
  • 1
    It may not be your case, but my workaround to the save issue that took too long, was to use object manager instead of injecting classes in constructor. Commented Feb 23, 2018 at 8:49
  • magento.stackexchange.com/questions/191947/… it working without effecting customer session Commented Jul 13, 2018 at 10:57
0

I have a quite nice solution for this :

protected $customerRepository; public function __construct( \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository ) { $this->customerRepository = $customerRepository; } ... public function updateCustomerAttribute($customerId, $attributeCode, $value) { $customerData = $this->customerRepository->getById($customerId); $customerData->setCustomAttribute($attributeCode, $value); $this->customerRepository->save($customerData); } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.