2

I need to create a custom attribute for customer entity with the Yes/No.

I tried to use boolean type and also select method. But nothing has worked.

Below are my work:

Try 1: (Type as select)

$customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'is_mobile_verified',
            [
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Is Mobile Verified?',
                'input' => 'select',
                'class' => '',
                'source' => 'Ucs\Reson8\Model\Config\Source\Options',
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => 0,
                'system'       => 0,
                'position'     => 1002,
            ]
        );


        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'is_mobile_verified')
        ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => [
                    'adminhtml_customer',
                ]
        ]);
        $attribute->save();

Ucs\Reson8\Model\Config\Source\Options.php as below

<?php

namespace Ucs\Reson8\Model\Config\Source;

class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    /**
    * Get all options
    *
    * @return array
    */
    public function getAllOptions()
    {
        $this->_options = [
                ['label' => __('No'), 'value'=>'0'],
                ['label' => __('Yes'), 'value'=>'1'],
            ];

    return $this->_options;

    }

}

Try 2: (Type as boolean)

$customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'is_mobile_verified',
            [
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Is Mobile Verified?',
                'input' => 'boolean',
                'class' => '',
                'source' => '',
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => 0,
                'system'       => 0,
                'position'     => 1002,
            ]
        );
$attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'is_mobile_verified')
        ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => [
                    'adminhtml_customer',
                ]
        ]);

Can someone help me?

1

Give a try with below:

$customerSetup->addAttribute(Customer::ENTITY, 'is_mobile_verified', [
                'label' => 'Is Mobile Verified ?',
                'input' => 'select',
                'required' => false,
                'sort_order' => 100,
                'visible' => false,
                'system' => false,
                'type' => 'static',
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
                'option' => ['values' => ['Yes', 'No']],
            ]);
1

Try this..

InstallData.php

<?php

namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var \Magento\Eav\Setup\EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Customer setup factory
     *
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Constructor
     *
     * @param EavSetupFactory $eavSetupFactory
     * @param CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        EavSetupFactory $eavSetupFactory,
        CustomerSetupFactory $customerSetupFactory
    ) 
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $setup->startSetup();

        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        /**
         * Create a select box attribute
         */
        $attributeCode = 'my_customer_type';

        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY, 
            $attributeCode, 
            [
                'type' => 'int',
                'label' => 'My Customer Type',
                'input' => 'select',
                'source' => 'Vendor\Module\Model\Config\Source\MyCustomerType',
                'required' => false,
                'visible' => true,
                'position' => 300,
                'system' => false,
                'backend' => ''
            ]
        );

        // show the attribute in the following forms
        $attribute = $customerSetup
                        ->getEavConfig()
                        ->getAttribute(
                            \Magento\Customer\Model\Customer::ENTITY,
                            $attributeCode
                        )
                        ->addData(
                            ['used_in_forms' => [
                                'adminhtml_customer',
                                'adminhtml_checkout',
                                'customer_account_create',
                                'customer_account_edit'
                            ]
                        ]);

        $attribute->save();

        $setup->endSetup();
    }
}

enter image description here

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.