I created a custom customer attribute using an install script.
$customerSetup -> addAttribute(\Magento\Customer\Model\Customer::ENTITY,
'is_mpowersync',
[
'label' => 'Is Mpowersync',
'system' => false,
'position' => 120,
'sort_order' =>120,
'visible' => true,
'type' => 'int',
'input' => 'select',
"source" => "\Magento\Eav\Model\Entity\Attribute\Source\Boolean",
'required' => false,
'default' => '0',
'group' => 'Account Information',
]
);
$customerSetup -> getEavConfig() -> getAttribute('customer', 'is_mpowersync')->setData('is_user_defined',1)->setData('is_required',0)->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']) -> save();
I want to set a value the above created custom attribute programmatically creating the customer.
I tried below 3 methods, but data is not saving.
1 . $newcustomer->setData('is_mpowersync','1');
2 . $newcustomer->setCustomAttribute('is_mpowersync',1);
3 . $newcustomer->setIsMpowersync(1);
Both method not saving the data, if I save from backend admin customer creation it saving the data for the custom attribute.
How can I save custom customer attribute in Magento 2 programmatically?
Best Answer
Here in this example, I have tried to save custom attribute value using the Controller file in custom extension. To fix this issue, firstly we need to create ‘controller.php’ file at below location.
For more refrance click here
Note : Above code is not tested. you have to check.
I hope it helps!