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

时间:2016-09-28 06:46:13

标签: magento import 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();

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

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

但这也行不通。

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

1 个答案:

答案 0 :(得分:3)

你试过了吗?

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

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

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