I successfully created custom attribute for customer with input type select/dropdown like this:
$code = 'test';
$customerSetup = $this->customerSetupFactory->create();
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);
$insertData = array(
"type" => "int",
"backend" => "",
"label" => 'test',
"input" => 'select',
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
);
$insertData['option']['values'] = array(0 => 'No', 1 => 'Yes');
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, $code, $insertData);
$attribute = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);
$attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $code);
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 11);
$attribute->save();
the problem is when i try to change the input type to checkbox "input"=>"checkbox", the custom attribute is saved but the values option do not appear when i try to add new customer via admin , here's how it looks:
enter image description here
and i need to add more custom attribute with input type radio and multiselect, but i havent tried it, i'm afraid it will be like the checkbox case
-
did you find the solutionJaisa– Jaisa2018年12月27日 05:51:23 +00:00Commented Dec 27, 2018 at 5:51
1 Answer 1
For Checkbox type customer attribute, Please change the following:
"type" => "int",
"backend" => "",
"label" => "Test",
"input" => "boolean",
"source" => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
"visible" => true,
"required" => true,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
Hope this will help you.
-
what about input radio and multiselect?Idham Choudry– Idham Choudry2017年06月22日 10:12:30 +00:00Commented Jun 22, 2017 at 10:12
-
boolean is only for 2 option input, its not for checkboxesIdham Choudry– Idham Choudry2017年06月22日 10:22:41 +00:00Commented Jun 22, 2017 at 10:22
-
How can we set value for this attribute from controller?Jafar Pinjar– Jafar Pinjar2018年08月28日 08:41:01 +00:00Commented Aug 28, 2018 at 8:41
-
-
Anyone have idea about this?Stack user– Stack user2019年02月27日 11:41:09 +00:00Commented Feb 27, 2019 at 11:41
Explore related questions
See similar questions with these tags.