4

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

1
  • did you find the solution
    – Jaisa
    Commented Dec 27, 2018 at 5:51

1 Answer 1

5

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.

5
  • what about input radio and multiselect? Commented Jun 22, 2017 at 10:12
  • boolean is only for 2 option input, its not for checkboxes Commented Jun 22, 2017 at 10:22
  • How can we set value for this attribute from controller? Commented Aug 28, 2018 at 8:41
  • did yiu found the solution
    – Jaisa
    Commented Dec 27, 2018 at 5:55
  • Anyone have idea about this?
    – Stack user
    Commented Feb 27, 2019 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.