4

How can I save a custom Customer Attribute value programmatically? I have tried below code but it didn't work.

protected $customer;

public function __construct(
    \Magento\Customer\Model\Customer $customer
)
{
    $this->customer = $customer;
}

...
...

$customerId = "1";
$customer = $this->customer->load($customerId);
$data = "customer attribute value";
$customerData = $customer->getDataModel();
$customerData->setCustomAttribute('customer_attribute_code',$data);
$customer->updateData($customerData);
$customer->save();

Is there any other method to save the customer attribute?

11

I have got the solution

protected $customer;

protected $customerFactory;

public function __construct(
    \Magento\Customer\Model\Customer $customer
    \Magento\Customer\Model\ResourceModel\CustomerFactory $customerFactory
)
{
    $this->customer = $customer;
    $this->customerFactory = $customerFactory;
}

...
...

$customerId = "1";
$customer = $this->customer->load($customerId);
$data = "customer attribute value";
$customerData = $customer->getDataModel();
$customerData->setCustomAttribute('customer_attribute_code',$data);
$customer->updateData($customerData);
$customerResource = $this->customerFactory->create();
$customerResource->saveAttribute($customer, 'customer_attribute_code');
  • 2
    This code not working in magento 2.2.3, please suggest if you have any alternate solution please share as I want to save custom customer address attribute value programmatically. – Purushotam Sharma Mar 28 '18 at 13:26
  • 1
    @Dinesh , Best Solution ever I got. Working for all types of customer attributes. – TBS Mage Jun 19 '18 at 7:07
  • 2
    @TBSMage, Thank you for your valuable comment :) – Dinesh Yadav Jun 19 '18 at 7:33
  • I have created attribute of type int, that is not saving if i use below code $customerData->setCustomAttribute('customer_check', 1); – jafar pinjar Aug 28 '18 at 8:50
  • @DineshYadav, how to save customer custom address values? – jafar pinjar Feb 1 at 12:14
1

Some times, you may need to save multiple attributes at the same time. After go through the Magento code, I endup with this code. Here medicare_number and medicare_reference are custom attributes.

        $customerModel = $this->_customerFactory->create();
        $customerModel->getResource()->load($customerModel, $customerId);
        $customerModel->setData('dob', $this->getRequest()->getParam('dob'))
                    ->setData('gender', $this->getRequest()->getParam('gender'))
                    ->setData('medicare_number', $this->getRequest()->getParam('medicare_number'))
                    ->setData('medicare_reference', $this->getRequest()->getParam('medicare_reference'))
                    ->setAttributeSetId(\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
        $customerModel->getResource()->save($customerModel);
  • this is working in 2.2.5, tried dozens of other answers but none of them work, thank you! – stetoc Aug 30 '18 at 19:42
  • short version for working code is something like $customer->setData('my_attribute', 1234) ->setAttributeSetId(\Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER)->getResource()->save($customer) – stetoc Aug 30 '18 at 19:43
0
protected $customer;

protected $customerFactory;

public function __construct(
    \Magento\Customer\Model\Customer $customer
    \Magento\Customer\Model\ResourceModel\CustomerFactory $customerFactory
)
{
    $this->customer = $customer;
    $this->customerFactory = $customerFactory;
}

...
...
    $mobile="1234567890";
    $custom = $this->_customerFactory ->create();
    $custom = $custom->setWebsiteId($helperData->getWebsiteId());
    $custom = $custom->loadByEmail("abc123@xyz.com");

    $customerData = $custom->getDataModel();
    $customerData->setCustomAttribute('custom_attribute code', $mobile);
    $custom->updateData($customerData);

    $custom->save();
0

You need to do it the strange Magento 2 way:-

public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
) {
    $this->_customerRepository = $customerRepository;
}

public function execute()
{
    $customer = $this->_customerRepository->getById($customerId);
    $customer->setDob($data['dob'])
             ->setCustomAttribute('medicare_number',$data['medicare_number'])
             ->setCustomAttribute('medicare_reference',$data['medicare_reference'])
             ->setCustomAttribute('medicare_exp',$data['medicare_exp']);
    $this->_customerRepository->save($customer);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.