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

Mr.*_*etz 8 import magento magento2 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后台显示相关,如果手动创建客户,也会正确保存值.

小智 3

你有没有尝试过:

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

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

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