0

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,
    \Magento\Customer\Model\ResourceModel\Attribute $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(\Magento\Customer\Model\Customer::ENTITY,
    'random_customer', [
        'type' => 'int',
        'label' => 'Attribute Approved',
        'input' => 'select',
        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
        '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(\Magento\Customer\Model\Customer::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.

1

2 Answers 2

0

Try Below Code.

<?php

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    protected $customerSetupFactory;

    private $attributeSetFactory;

    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }   
      
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
          
        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
          
        $customerSetup->addAttribute(Customer::ENTITY, 'approve_account', [
            'type' => 'int',
            'label' => 'Approve Account',
            'input' => 'select',
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'position' => 1,
            'system' => 0,
            'is_used_in_grid'       => true,
            'is_visible_in_grid'    => true,

        ]);
          
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'approve_account')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_account_edit'],//you can use other forms also
        ]);
          
        $attribute->save();
    }
}

THANKS.

1
  • I tried, but didn't work. I even disabled the module and re-enabled it. I have magento 2.3.5 installed
    – nix
    Commented Mar 28, 2021 at 10:10
0

Please use the below code snippet. It will display a boolean switch for Yes/No values.

$this->customerSetup->addAttribute(
    Customer::ENTITY,
    'attribute_approved',
    [
        'label' => 'Attribute approved',
        'required' => 0,
        'type' => 'int',
        'input' => 'boolean',
        'system' => 0,
        'user_defined' => 1,
    ]
);

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.