Skip to content

Magento 2.3.5 create boolean custom Customer Attribute

I am trying to create a Yes/No attribute for Customer in the admin panel, but its not working. I have tried several available solutions but didn’t seem to work. My code is given below.

/Setup/InstallData.php

class InstallData implements InstallDataInterface {
protected $customerSetupFactory;

private $eavSetupFactory;
private $eavConfig;
private $attributeResource;

public function __construct(
    CustomerSetupFactory $customerSetupFactory,
    EavSetupFactory $eavSetupFactory,
    Config $eavConfig,
    MagentoCustomerModelResourceModelAttribute $attributeResource
)
{
    $this->eavSetupFactory = $eavSetupFactory;
    $this->eavConfig = $eavConfig;
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeResource = $attributeResource;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();

    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customerSetup->addAttribute(MagentoCustomerModelCustomer::ENTITY,
    'random_customer', [
        'type' => 'int',
        'label' => 'Attribute Approved',
        'input' => 'select',
        'source' => 'MagentoEavModelEntityAttributeSourceBoolean',
        'default' => '0',
        'required' => false,
        'visible' => true,
        'user_defined' => true,
        'system' => 0,
        'position' => 999,
        'is_used_in_grid' => true,
        'is_visible_in_grid' => true,
    ]);

    $myAttribute = $customerSetup->getEavConfig()->getAttribute(MagentoCustomerModelCustomer::ENTITY,
 'random_customer');

    $myAttribute->setData('used_in_forms', ['adminhtml_customer']);

    $this->attributeResource->save($myAttribute);

    $setup->endSetup();
} }

I upgraded and regenerated code & static files but I do not see the option. However, if I try to add the text attribute, it works fine. But I am trying to create Boolean (Yes/No) attribute.