小编Mr.*_*etz的帖子

以编程方式Magento 2设置自定义Customer属性值

我正在进口一些客户:

$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();
Run Code Online (Sandbox Code Playgroud)

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

$customer->setCustomAttribute('Attr1','value');
Run Code Online (Sandbox Code Playgroud)

但这也行不通.

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

import magento magento2 customer

8
推荐指数
1
解决办法
5101
查看次数

标签 统计

customer ×1

import ×1

magento ×1

magento2 ×1