Magento2 – Set Custom Customer Attributes Programmatically

attributescustom-attributescustomercustomer-attributemagento2

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 Path\UserRegistration\Cron;

class RegisterUsers
{
    private $storeManager;
    private $customerFactory;
    private $customerModel;
    private $baseUrl;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager, 
        \Magento\Customer\Model\CustomerFactory $customerFactory, 
        \Magento\Customer\Model\Customer $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.

Best 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();
}