Magento 2 : Unable to set custom customer attributes programmatically

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












I have a cron job that runs once a day, that reads a file uploaded to the server, and registers users from that file. The code works fine, but now I added a custom attribute to the customer (lets call it customId), which I also want to set programatically, when registering the users, but the problem is, that it only seems to add the custom attribute to the first user, and the others have that field empty.



Here is my code:



namespace PathUserRegistrationCron;

class RegisterUsers

private $storeManager;
private $customerFactory;
private $customerModel;
private $baseUrl;

public function __construct(
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCustomerModelCustomerFactory $customerFactory,
MagentoCustomerModelCustomer $customerModel)

$this->storeManager = $storeManager;
$this->customerFactory = $customerFactory;
$this->customerModel = $customerModel;
$this->baseUrl = $this->storeManager->getStore()->getBaseUrl();



public function register($customId, $email, $password, $name, $websiteId, $storeId)

$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->setStoreId($storeId);
$customer->setEmail($email);
$customer->setFirstname($name);
$customer->setPassword($password);
$customer->setCustomId($customId);
$customer->save();


public function execute()

$handle = fopen($this->baseUrl . "upload/NewUsers.csv", "r");

if ($handle)
$lineNum = 0;

while (($line = fgets($handle)) !== false)
$lineNum++;

// Ignore first line of csv file
if ($lineNum > 1)
$lineItems = explode(';', $line);

$customId = trim($lineItems[1]);
$email = trim($lineItems[4]);
$password = trim($lineItems[2]);
$name = trim($lineItems[3]);

$websiteId = $this->storeManager->getWebsite()->getId() == 0 ? '1' : $this->storeManager->getWebsite()->getId();
$storeId = $this->storeManager->getStore()->getId() == 0 ? '1' : $this->storeManager->getStore()->getId();

if ($email && $name && $password)
$customerExisting = $this->customerModel->setWebsiteId($storeId)->loadByEmail($email);

$customerExists = $customerExisting->getId() ?? false;

if ($customerExists === false)
$this->register($customId, $email, $password, $name, $websiteId, $storeId);





fclose($handle);





The strange thing is, if I call $customer->getCustomId() right after the $customer->save(), i get back the correct value. So it is saved into the db, but then deleted?



I can also manually add the customId to any user I want, and it works fine.



Any help would be appreciated.










share|improve this question





























    up vote
    4
    down vote

    favorite












    I have a cron job that runs once a day, that reads a file uploaded to the server, and registers users from that file. The code works fine, but now I added a custom attribute to the customer (lets call it customId), which I also want to set programatically, when registering the users, but the problem is, that it only seems to add the custom attribute to the first user, and the others have that field empty.



    Here is my code:



    namespace PathUserRegistrationCron;

    class RegisterUsers

    private $storeManager;
    private $customerFactory;
    private $customerModel;
    private $baseUrl;

    public function __construct(
    MagentoStoreModelStoreManagerInterface $storeManager,
    MagentoCustomerModelCustomerFactory $customerFactory,
    MagentoCustomerModelCustomer $customerModel)

    $this->storeManager = $storeManager;
    $this->customerFactory = $customerFactory;
    $this->customerModel = $customerModel;
    $this->baseUrl = $this->storeManager->getStore()->getBaseUrl();



    public function register($customId, $email, $password, $name, $websiteId, $storeId)

    $customer = $this->customerFactory->create();
    $customer->setWebsiteId($websiteId);
    $customer->setStoreId($storeId);
    $customer->setEmail($email);
    $customer->setFirstname($name);
    $customer->setPassword($password);
    $customer->setCustomId($customId);
    $customer->save();


    public function execute()

    $handle = fopen($this->baseUrl . "upload/NewUsers.csv", "r");

    if ($handle)
    $lineNum = 0;

    while (($line = fgets($handle)) !== false)
    $lineNum++;

    // Ignore first line of csv file
    if ($lineNum > 1)
    $lineItems = explode(';', $line);

    $customId = trim($lineItems[1]);
    $email = trim($lineItems[4]);
    $password = trim($lineItems[2]);
    $name = trim($lineItems[3]);

    $websiteId = $this->storeManager->getWebsite()->getId() == 0 ? '1' : $this->storeManager->getWebsite()->getId();
    $storeId = $this->storeManager->getStore()->getId() == 0 ? '1' : $this->storeManager->getStore()->getId();

    if ($email && $name && $password)
    $customerExisting = $this->customerModel->setWebsiteId($storeId)->loadByEmail($email);

    $customerExists = $customerExisting->getId() ?? false;

    if ($customerExists === false)
    $this->register($customId, $email, $password, $name, $websiteId, $storeId);





    fclose($handle);





    The strange thing is, if I call $customer->getCustomId() right after the $customer->save(), i get back the correct value. So it is saved into the db, but then deleted?



    I can also manually add the customId to any user I want, and it works fine.



    Any help would be appreciated.










    share|improve this question

























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I have a cron job that runs once a day, that reads a file uploaded to the server, and registers users from that file. The code works fine, but now I added a custom attribute to the customer (lets call it customId), which I also want to set programatically, when registering the users, but the problem is, that it only seems to add the custom attribute to the first user, and the others have that field empty.



      Here is my code:



      namespace PathUserRegistrationCron;

      class RegisterUsers

      private $storeManager;
      private $customerFactory;
      private $customerModel;
      private $baseUrl;

      public function __construct(
      MagentoStoreModelStoreManagerInterface $storeManager,
      MagentoCustomerModelCustomerFactory $customerFactory,
      MagentoCustomerModelCustomer $customerModel)

      $this->storeManager = $storeManager;
      $this->customerFactory = $customerFactory;
      $this->customerModel = $customerModel;
      $this->baseUrl = $this->storeManager->getStore()->getBaseUrl();



      public function register($customId, $email, $password, $name, $websiteId, $storeId)

      $customer = $this->customerFactory->create();
      $customer->setWebsiteId($websiteId);
      $customer->setStoreId($storeId);
      $customer->setEmail($email);
      $customer->setFirstname($name);
      $customer->setPassword($password);
      $customer->setCustomId($customId);
      $customer->save();


      public function execute()

      $handle = fopen($this->baseUrl . "upload/NewUsers.csv", "r");

      if ($handle)
      $lineNum = 0;

      while (($line = fgets($handle)) !== false)
      $lineNum++;

      // Ignore first line of csv file
      if ($lineNum > 1)
      $lineItems = explode(';', $line);

      $customId = trim($lineItems[1]);
      $email = trim($lineItems[4]);
      $password = trim($lineItems[2]);
      $name = trim($lineItems[3]);

      $websiteId = $this->storeManager->getWebsite()->getId() == 0 ? '1' : $this->storeManager->getWebsite()->getId();
      $storeId = $this->storeManager->getStore()->getId() == 0 ? '1' : $this->storeManager->getStore()->getId();

      if ($email && $name && $password)
      $customerExisting = $this->customerModel->setWebsiteId($storeId)->loadByEmail($email);

      $customerExists = $customerExisting->getId() ?? false;

      if ($customerExists === false)
      $this->register($customId, $email, $password, $name, $websiteId, $storeId);





      fclose($handle);





      The strange thing is, if I call $customer->getCustomId() right after the $customer->save(), i get back the correct value. So it is saved into the db, but then deleted?



      I can also manually add the customId to any user I want, and it works fine.



      Any help would be appreciated.










      share|improve this question















      I have a cron job that runs once a day, that reads a file uploaded to the server, and registers users from that file. The code works fine, but now I added a custom attribute to the customer (lets call it customId), which I also want to set programatically, when registering the users, but the problem is, that it only seems to add the custom attribute to the first user, and the others have that field empty.



      Here is my code:



      namespace PathUserRegistrationCron;

      class RegisterUsers

      private $storeManager;
      private $customerFactory;
      private $customerModel;
      private $baseUrl;

      public function __construct(
      MagentoStoreModelStoreManagerInterface $storeManager,
      MagentoCustomerModelCustomerFactory $customerFactory,
      MagentoCustomerModelCustomer $customerModel)

      $this->storeManager = $storeManager;
      $this->customerFactory = $customerFactory;
      $this->customerModel = $customerModel;
      $this->baseUrl = $this->storeManager->getStore()->getBaseUrl();



      public function register($customId, $email, $password, $name, $websiteId, $storeId)

      $customer = $this->customerFactory->create();
      $customer->setWebsiteId($websiteId);
      $customer->setStoreId($storeId);
      $customer->setEmail($email);
      $customer->setFirstname($name);
      $customer->setPassword($password);
      $customer->setCustomId($customId);
      $customer->save();


      public function execute()

      $handle = fopen($this->baseUrl . "upload/NewUsers.csv", "r");

      if ($handle)
      $lineNum = 0;

      while (($line = fgets($handle)) !== false)
      $lineNum++;

      // Ignore first line of csv file
      if ($lineNum > 1)
      $lineItems = explode(';', $line);

      $customId = trim($lineItems[1]);
      $email = trim($lineItems[4]);
      $password = trim($lineItems[2]);
      $name = trim($lineItems[3]);

      $websiteId = $this->storeManager->getWebsite()->getId() == 0 ? '1' : $this->storeManager->getWebsite()->getId();
      $storeId = $this->storeManager->getStore()->getId() == 0 ? '1' : $this->storeManager->getStore()->getId();

      if ($email && $name && $password)
      $customerExisting = $this->customerModel->setWebsiteId($storeId)->loadByEmail($email);

      $customerExists = $customerExisting->getId() ?? false;

      if ($customerExists === false)
      $this->register($customId, $email, $password, $name, $websiteId, $storeId);





      fclose($handle);





      The strange thing is, if I call $customer->getCustomId() right after the $customer->save(), i get back the correct value. So it is saved into the db, but then deleted?



      I can also manually add the customId to any user I want, and it works fine.



      Any help would be appreciated.







      magento2 attributes customer custom-attributes customer-attribute






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 17 at 11:40









      twinkle_systematix

      656




      656










      asked Sep 17 at 6:47









      Bawsi

      838




      838




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          This is because you need to save your customer object before setting the attribute



          Change you register function to the below one:



          public function register($customId, $email, $password, $name, $websiteId, $storeId) 

          $customer = $this->customerFactory->create();
          $customer->setWebsiteId($websiteId);
          $customer->setStoreId($storeId);
          $customer->setEmail($email);
          $customer->setFirstname($name);
          $customer->setPassword($password);
          $customer->save();

          $customerData = $customer->getDataModel();
          $customerData->setCustomAttribute('custom_id', $customId);
          $customer->updateData($customerData);
          $customer->save();






          share|improve this answer






















          • I get an exception: Invalid method MagentoCustomerModelCustomerInterceptor::saveAttributeInvalid method %1::%2Invalid method %1::%2theme
            – Bawsi
            Sep 17 at 8:30










          • Calling $customer->save(); right after updateData() worked. Thanks!!
            – Bawsi
            Sep 17 at 8:35










          • Welcome. You can update my answer as well.
            – Sukumar Gorai
            Sep 17 at 8:41

















          up vote
          2
          down vote













          Try like this, then check you logs, check also that $customId is always available and not empty



          public function register($customId, $email, $password, $name, $websiteId, $storeId) 

          $customer = $this->customerFactory->create();
          $customer->setWebsiteId($websiteId);
          $customer->setStoreId($storeId);
          $customer->setEmail($email);
          $customer->setFirstname($name);
          $customer->setPassword($password);
          $customer->setCustomId($customId); // or $customer->setCustomAttribute('custom_id', $customId);
          try
          $customer->save();
          cach (Exception $e)
          $e->getMessage();







          share|improve this answer






















          • The thing is , that customId does get saved, since I can access it right after $customer->save() with $customer->getCustomId(), but then it gets deleted or something, and I cant figure out what happens, However, If I only try and register one user at a time (only one user in csv file), it works. So its only a problem when trying to register multiple users.
            – Bawsi
            Sep 17 at 8:17










          • Try to add all the sets in foreach()
            – Prince
            Sep 17 at 8:21










          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          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%2f242466%2fmagento-2-unable-to-set-custom-customer-attributes-programmatically%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          This is because you need to save your customer object before setting the attribute



          Change you register function to the below one:



          public function register($customId, $email, $password, $name, $websiteId, $storeId) 

          $customer = $this->customerFactory->create();
          $customer->setWebsiteId($websiteId);
          $customer->setStoreId($storeId);
          $customer->setEmail($email);
          $customer->setFirstname($name);
          $customer->setPassword($password);
          $customer->save();

          $customerData = $customer->getDataModel();
          $customerData->setCustomAttribute('custom_id', $customId);
          $customer->updateData($customerData);
          $customer->save();






          share|improve this answer






















          • I get an exception: Invalid method MagentoCustomerModelCustomerInterceptor::saveAttributeInvalid method %1::%2Invalid method %1::%2theme
            – Bawsi
            Sep 17 at 8:30










          • Calling $customer->save(); right after updateData() worked. Thanks!!
            – Bawsi
            Sep 17 at 8:35










          • Welcome. You can update my answer as well.
            – Sukumar Gorai
            Sep 17 at 8:41














          up vote
          4
          down vote



          accepted










          This is because you need to save your customer object before setting the attribute



          Change you register function to the below one:



          public function register($customId, $email, $password, $name, $websiteId, $storeId) 

          $customer = $this->customerFactory->create();
          $customer->setWebsiteId($websiteId);
          $customer->setStoreId($storeId);
          $customer->setEmail($email);
          $customer->setFirstname($name);
          $customer->setPassword($password);
          $customer->save();

          $customerData = $customer->getDataModel();
          $customerData->setCustomAttribute('custom_id', $customId);
          $customer->updateData($customerData);
          $customer->save();






          share|improve this answer






















          • I get an exception: Invalid method MagentoCustomerModelCustomerInterceptor::saveAttributeInvalid method %1::%2Invalid method %1::%2theme
            – Bawsi
            Sep 17 at 8:30










          • Calling $customer->save(); right after updateData() worked. Thanks!!
            – Bawsi
            Sep 17 at 8:35










          • Welcome. You can update my answer as well.
            – Sukumar Gorai
            Sep 17 at 8:41












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          This is because you need to save your customer object before setting the attribute



          Change you register function to the below one:



          public function register($customId, $email, $password, $name, $websiteId, $storeId) 

          $customer = $this->customerFactory->create();
          $customer->setWebsiteId($websiteId);
          $customer->setStoreId($storeId);
          $customer->setEmail($email);
          $customer->setFirstname($name);
          $customer->setPassword($password);
          $customer->save();

          $customerData = $customer->getDataModel();
          $customerData->setCustomAttribute('custom_id', $customId);
          $customer->updateData($customerData);
          $customer->save();






          share|improve this answer














          This is because you need to save your customer object before setting the attribute



          Change you register function to the below one:



          public function register($customId, $email, $password, $name, $websiteId, $storeId) 

          $customer = $this->customerFactory->create();
          $customer->setWebsiteId($websiteId);
          $customer->setStoreId($storeId);
          $customer->setEmail($email);
          $customer->setFirstname($name);
          $customer->setPassword($password);
          $customer->save();

          $customerData = $customer->getDataModel();
          $customerData->setCustomAttribute('custom_id', $customId);
          $customer->updateData($customerData);
          $customer->save();







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 17 at 14:00









          zack6849

          12212




          12212










          answered Sep 17 at 7:46









          Sukumar Gorai

          4,9603424




          4,9603424











          • I get an exception: Invalid method MagentoCustomerModelCustomerInterceptor::saveAttributeInvalid method %1::%2Invalid method %1::%2theme
            – Bawsi
            Sep 17 at 8:30










          • Calling $customer->save(); right after updateData() worked. Thanks!!
            – Bawsi
            Sep 17 at 8:35










          • Welcome. You can update my answer as well.
            – Sukumar Gorai
            Sep 17 at 8:41
















          • I get an exception: Invalid method MagentoCustomerModelCustomerInterceptor::saveAttributeInvalid method %1::%2Invalid method %1::%2theme
            – Bawsi
            Sep 17 at 8:30










          • Calling $customer->save(); right after updateData() worked. Thanks!!
            – Bawsi
            Sep 17 at 8:35










          • Welcome. You can update my answer as well.
            – Sukumar Gorai
            Sep 17 at 8:41















          I get an exception: Invalid method MagentoCustomerModelCustomerInterceptor::saveAttributeInvalid method %1::%2Invalid method %1::%2theme
          – Bawsi
          Sep 17 at 8:30




          I get an exception: Invalid method MagentoCustomerModelCustomerInterceptor::saveAttributeInvalid method %1::%2Invalid method %1::%2theme
          – Bawsi
          Sep 17 at 8:30












          Calling $customer->save(); right after updateData() worked. Thanks!!
          – Bawsi
          Sep 17 at 8:35




          Calling $customer->save(); right after updateData() worked. Thanks!!
          – Bawsi
          Sep 17 at 8:35












          Welcome. You can update my answer as well.
          – Sukumar Gorai
          Sep 17 at 8:41




          Welcome. You can update my answer as well.
          – Sukumar Gorai
          Sep 17 at 8:41












          up vote
          2
          down vote













          Try like this, then check you logs, check also that $customId is always available and not empty



          public function register($customId, $email, $password, $name, $websiteId, $storeId) 

          $customer = $this->customerFactory->create();
          $customer->setWebsiteId($websiteId);
          $customer->setStoreId($storeId);
          $customer->setEmail($email);
          $customer->setFirstname($name);
          $customer->setPassword($password);
          $customer->setCustomId($customId); // or $customer->setCustomAttribute('custom_id', $customId);
          try
          $customer->save();
          cach (Exception $e)
          $e->getMessage();







          share|improve this answer






















          • The thing is , that customId does get saved, since I can access it right after $customer->save() with $customer->getCustomId(), but then it gets deleted or something, and I cant figure out what happens, However, If I only try and register one user at a time (only one user in csv file), it works. So its only a problem when trying to register multiple users.
            – Bawsi
            Sep 17 at 8:17










          • Try to add all the sets in foreach()
            – Prince
            Sep 17 at 8:21














          up vote
          2
          down vote













          Try like this, then check you logs, check also that $customId is always available and not empty



          public function register($customId, $email, $password, $name, $websiteId, $storeId) 

          $customer = $this->customerFactory->create();
          $customer->setWebsiteId($websiteId);
          $customer->setStoreId($storeId);
          $customer->setEmail($email);
          $customer->setFirstname($name);
          $customer->setPassword($password);
          $customer->setCustomId($customId); // or $customer->setCustomAttribute('custom_id', $customId);
          try
          $customer->save();
          cach (Exception $e)
          $e->getMessage();







          share|improve this answer






















          • The thing is , that customId does get saved, since I can access it right after $customer->save() with $customer->getCustomId(), but then it gets deleted or something, and I cant figure out what happens, However, If I only try and register one user at a time (only one user in csv file), it works. So its only a problem when trying to register multiple users.
            – Bawsi
            Sep 17 at 8:17










          • Try to add all the sets in foreach()
            – Prince
            Sep 17 at 8:21












          up vote
          2
          down vote










          up vote
          2
          down vote









          Try like this, then check you logs, check also that $customId is always available and not empty



          public function register($customId, $email, $password, $name, $websiteId, $storeId) 

          $customer = $this->customerFactory->create();
          $customer->setWebsiteId($websiteId);
          $customer->setStoreId($storeId);
          $customer->setEmail($email);
          $customer->setFirstname($name);
          $customer->setPassword($password);
          $customer->setCustomId($customId); // or $customer->setCustomAttribute('custom_id', $customId);
          try
          $customer->save();
          cach (Exception $e)
          $e->getMessage();







          share|improve this answer














          Try like this, then check you logs, check also that $customId is always available and not empty



          public function register($customId, $email, $password, $name, $websiteId, $storeId) 

          $customer = $this->customerFactory->create();
          $customer->setWebsiteId($websiteId);
          $customer->setStoreId($storeId);
          $customer->setEmail($email);
          $customer->setFirstname($name);
          $customer->setPassword($password);
          $customer->setCustomId($customId); // or $customer->setCustomAttribute('custom_id', $customId);
          try
          $customer->save();
          cach (Exception $e)
          $e->getMessage();








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 17 at 7:54

























          answered Sep 17 at 7:44









          Prince

          6,5212934




          6,5212934











          • The thing is , that customId does get saved, since I can access it right after $customer->save() with $customer->getCustomId(), but then it gets deleted or something, and I cant figure out what happens, However, If I only try and register one user at a time (only one user in csv file), it works. So its only a problem when trying to register multiple users.
            – Bawsi
            Sep 17 at 8:17










          • Try to add all the sets in foreach()
            – Prince
            Sep 17 at 8:21
















          • The thing is , that customId does get saved, since I can access it right after $customer->save() with $customer->getCustomId(), but then it gets deleted or something, and I cant figure out what happens, However, If I only try and register one user at a time (only one user in csv file), it works. So its only a problem when trying to register multiple users.
            – Bawsi
            Sep 17 at 8:17










          • Try to add all the sets in foreach()
            – Prince
            Sep 17 at 8:21















          The thing is , that customId does get saved, since I can access it right after $customer->save() with $customer->getCustomId(), but then it gets deleted or something, and I cant figure out what happens, However, If I only try and register one user at a time (only one user in csv file), it works. So its only a problem when trying to register multiple users.
          – Bawsi
          Sep 17 at 8:17




          The thing is , that customId does get saved, since I can access it right after $customer->save() with $customer->getCustomId(), but then it gets deleted or something, and I cant figure out what happens, However, If I only try and register one user at a time (only one user in csv file), it works. So its only a problem when trying to register multiple users.
          – Bawsi
          Sep 17 at 8:17












          Try to add all the sets in foreach()
          – Prince
          Sep 17 at 8:21




          Try to add all the sets in foreach()
          – Prince
          Sep 17 at 8:21

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f242466%2fmagento-2-unable-to-set-custom-customer-attributes-programmatically%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?