magento - 以编程方式设置自定义客户属性值 Magento 2

标签 magento import magento2

我正在导入一些客户:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $customerFactory = $objectManager->create('\Magento\Customer\Model\CustomerFactory');


        $customer = $objectManager->create('Magento\Customer\Model\Customer')->setWebsiteId(1)->loadByEmail('customrr@custom.com');


        try {
            if(!empty($customer->getData('email')))
            {
                $customer->setAttr1(1); // Attr1 = Name of the custom Attribute 
                $customer->setAttr2(2); // Attr2 = Name of the custom Attribute 
            }
            else
            {
                $customer = $customerFactory->create()->setWebsiteId(1);
            }


            $customer->setLastname("Lastname");

            $customer->setFirstname("Firsty");

            .....

            $customer->save();

客户的所有标准属性均已正确保存,但我的新属性无论如何都不会保存。我也尝试过:

$customer->setCustomAttribute('Attr1','value');

但这也不起作用。

自定义属性在 Magentos 2 后台显示正确,并且如果手动创建客户,值也会正确保存。

最佳答案

你尝试过吗:

$customer-> setData('Attr1','value');

并且不要忘记保存并记录信息:

try {
    $customer->save();
} catch (\Exception $e) {
    // log exception so you can debug the issue if there is one
}

关于magento - 以编程方式设置自定义客户属性值 Magento 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39739905/

相关文章:

mysql - 在bash脚本中输出MySQL进程

java - 在Java中导入相邻的包

magento - 创建发货不会更新商品发货数量且订单未完成

swift - 如何在 iOS swift 4 中访问 Magento Rest API

swift - 文件 'BluetoothController.swift' 是模块 'CoreBluetooth' 的一部分;忽略导入

javascript - magento2 javascript 文件中的更改不适用于我的店面

Magento:外部脚本中的 addSuccess() 无法正确保存

flash - htpasswd、Magento 和图像 uploader HTTP 错误

magento - 如何在 Magento 中使用多个商店 ID 获取商店名称?

object - Magento:具有数千个值/选项的属性