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

  • did you find the solution – Jaisa Dec 27 '18 at 5:51
3

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 Jun 22 '17 at 10:12
  • boolean is only for 2 option input, its not for checkboxes – Idham Choudry Jun 22 '17 at 10:22
  • How can we set value for this attribute from controller? – jafar pinjar Aug 28 '18 at 8:41
  • did yiu found the solution – Jaisa Dec 27 '18 at 5:55
  • Anyone have idea about this? – Stack user Feb 27 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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