Magento 2: Save custom customer attribute value programmatically












1














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(
MagentoCustomerModelCustomer $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?










share|improve this question





























    1














    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(
    MagentoCustomerModelCustomer $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?










    share|improve this question



























      1












      1








      1







      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(
      MagentoCustomerModelCustomer $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?










      share|improve this question















      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(
      MagentoCustomerModelCustomer $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?







      magento2 customer customer-attribute






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 '17 at 11:09

























      asked Nov 7 '17 at 11:03









      Dinesh Yadav

      3,8201937




      3,8201937






















          4 Answers
          4






          active

          oldest

          votes


















          6














          I have got the solution



          protected $customer;

          protected $customerFactory;

          public function __construct(
          MagentoCustomerModelCustomer $customer
          MagentoCustomerModelResourceModelCustomerFactory $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');





          share|improve this answer

















          • 1




            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



















          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(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
          $customerModel->getResource()->save($customerModel);





          share|improve this answer





















          • 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(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER)->getResource()->save($customer)
            – stetoc
            Aug 30 '18 at 19:43



















          0














          protected $customer;

          protected $customerFactory;

          public function __construct(
          MagentoCustomerModelCustomer $customer
          MagentoCustomerModelResourceModelCustomerFactory $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();





          share|improve this answer































            0














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




            public function __construct(
            MagentoCustomerApiCustomerRepositoryInterface $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);
            }





            share|improve this answer





















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "479"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f200361%2fmagento-2-save-custom-customer-attribute-value-programmatically%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6














              I have got the solution



              protected $customer;

              protected $customerFactory;

              public function __construct(
              MagentoCustomerModelCustomer $customer
              MagentoCustomerModelResourceModelCustomerFactory $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');





              share|improve this answer

















              • 1




                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
















              6














              I have got the solution



              protected $customer;

              protected $customerFactory;

              public function __construct(
              MagentoCustomerModelCustomer $customer
              MagentoCustomerModelResourceModelCustomerFactory $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');





              share|improve this answer

















              • 1




                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














              6












              6








              6






              I have got the solution



              protected $customer;

              protected $customerFactory;

              public function __construct(
              MagentoCustomerModelCustomer $customer
              MagentoCustomerModelResourceModelCustomerFactory $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');





              share|improve this answer












              I have got the solution



              protected $customer;

              protected $customerFactory;

              public function __construct(
              MagentoCustomerModelCustomer $customer
              MagentoCustomerModelResourceModelCustomerFactory $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');






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 7 '17 at 11:08









              Dinesh Yadav

              3,8201937




              3,8201937








              • 1




                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














              • 1




                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








              1




              1




              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




              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




              1




              @Dinesh , Best Solution ever I got. Working for all types of customer attributes.
              – TBS Mage
              Jun 19 '18 at 7:07




              @Dinesh , Best Solution ever I got. Working for all types of customer attributes.
              – TBS Mage
              Jun 19 '18 at 7:07




              2




              2




              @TBSMage, Thank you for your valuable comment :)
              – Dinesh Yadav
              Jun 19 '18 at 7:33




              @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




              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













              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(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
              $customerModel->getResource()->save($customerModel);





              share|improve this answer





















              • 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(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER)->getResource()->save($customer)
                – stetoc
                Aug 30 '18 at 19:43
















              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(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
              $customerModel->getResource()->save($customerModel);





              share|improve this answer





















              • 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(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER)->getResource()->save($customer)
                – stetoc
                Aug 30 '18 at 19:43














              1












              1








              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(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
              $customerModel->getResource()->save($customerModel);





              share|improve this answer












              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(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER);
              $customerModel->getResource()->save($customerModel);






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 24 '18 at 4:48









              user1554046

              111




              111












              • 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(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER)->getResource()->save($customer)
                – stetoc
                Aug 30 '18 at 19:43


















              • 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(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER)->getResource()->save($customer)
                – stetoc
                Aug 30 '18 at 19:43
















              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




              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(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER)->getResource()->save($customer)
              – stetoc
              Aug 30 '18 at 19:43




              short version for working code is something like $customer->setData('my_attribute', 1234) ->setAttributeSetId(MagentoCustomerApiCustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER)->getResource()->save($customer)
              – stetoc
              Aug 30 '18 at 19:43











              0














              protected $customer;

              protected $customerFactory;

              public function __construct(
              MagentoCustomerModelCustomer $customer
              MagentoCustomerModelResourceModelCustomerFactory $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();





              share|improve this answer




























                0














                protected $customer;

                protected $customerFactory;

                public function __construct(
                MagentoCustomerModelCustomer $customer
                MagentoCustomerModelResourceModelCustomerFactory $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();





                share|improve this answer


























                  0












                  0








                  0






                  protected $customer;

                  protected $customerFactory;

                  public function __construct(
                  MagentoCustomerModelCustomer $customer
                  MagentoCustomerModelResourceModelCustomerFactory $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();





                  share|improve this answer














                  protected $customer;

                  protected $customerFactory;

                  public function __construct(
                  MagentoCustomerModelCustomer $customer
                  MagentoCustomerModelResourceModelCustomerFactory $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();






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jun 11 '18 at 11:26

























                  answered Jun 11 '18 at 10:58









                  Sanjay Chauhan

                  697




                  697























                      0














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




                      public function __construct(
                      MagentoCustomerApiCustomerRepositoryInterface $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);
                      }





                      share|improve this answer


























                        0














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




                        public function __construct(
                        MagentoCustomerApiCustomerRepositoryInterface $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);
                        }





                        share|improve this answer
























                          0












                          0








                          0






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




                          public function __construct(
                          MagentoCustomerApiCustomerRepositoryInterface $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);
                          }





                          share|improve this answer












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




                          public function __construct(
                          MagentoCustomerApiCustomerRepositoryInterface $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);
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered yesterday









                          Dallas Clarke

                          71779




                          71779






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Magento Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f200361%2fmagento-2-save-custom-customer-attribute-value-programmatically%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              An IMO inspired problem

                              Management

                              Has there ever been an instance of an active nuclear power plant within or near a war zone?