0

I am working on extension and need to set customer custom attribute value. I tried the following code, but it's not working , can you please help me with this.

$customer = $this->customerFactory->create();
$customer->setId($observer->getEvent()->getCustomer()->getId());
$customerData = $customer->getDataModel();
$customerData->setCustomAttribute('mobilenumber','123465798');
$customer->updateData($customerData);
$customer->save();
1

Try using below code,

//declare repository
    protected $repository;

//inside your function

  $this->repository = $this->_objectManager->create('Magento\Customer\Api\AddressRepositoryInterface');

  $customerId = 5; //get customer id

  $address = $this->repository->getById($customerId);

  $customerData = $address;
  // change phone #
  $customerData->setMobilenumber('12345678');
  $customerDataSave = $this->repository->save($customerData);
|improve this answer|||||
  • Thank you for answer, full solution not work for me, but i can get idea from your code, so Thank you so much. – Bharat Desai - Certified Dev May 17 '16 at 14:17
  • @bharatdesai please post the solution as answer – Sarvagya Dec 30 '16 at 10:36

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.