无法以编程方式设置自定义客户属性

时间:2022-10-25 10:34:46

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.

我有一个每天运行一次的cron作业,它读取上传到服务器的文件,并从该文件注册用户。代码工作正常,但现在我向客户添加了一个自定义属性(让我们称之为customId),我也想在注册用户时以编程方式设置,但问题是,它似乎只是添加了自定义属性第一个用户,其他人将该字段清空。

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?

奇怪的是,如果我在$ customer-> save()之后调用$ customer-> getCustomId(),我会得到正确的值。所以它被保存到数据库中,但随后被删除了?

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

我也可以手动将customId添加到我想要的任何用户,它工作正常。

Any help would be appreciated.

任何帮助,将不胜感激。

2 个解决方案

#1


3  

Change you register function with 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);
    $customerResource = $this->customerFactory->create();
    $customerResource->saveAttribute($customer);
}

#2


1  

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

尝试这样,然后检查日志,检查$ customId是否始终可用而不是空

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

#1


3  

Change you register function with 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);
    $customerResource = $this->customerFactory->create();
    $customerResource->saveAttribute($customer);
}

#2


1  

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

尝试这样,然后检查日志,检查$ customId是否始终可用而不是空

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