Automatically detect custom Magento 2 client attribute value

I import some clients using:

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

The client is saved with all the standard attributes, but my new attributes will not be saved. I also tried:

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

but that didn't work either.

The user attribute is displayed in the Magentos 2 context menu, and the values ​​are saved correctly when creating the client manually.

+7
import magento magento2
source share
1 answer

You tried:

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

and do not forget to save and register information:

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

All Articles