Magento – Magento 2 – Add Customer Attribute with Select Field

customercustomer-attributeeav-attributesinstalldatamagento2.3

I'm trying to insert a customer attribute in magento 2

This is the InstallData script:

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;


class InstallData implements InstallDataInterface
{
    protected $customerSetupFactory;

    public function __construct(
        \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

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

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

        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'free_catalogue',
            [
                'type'      => 'int',
                'label'     => 'Free Catalogue',
                'input'     => 'select',
                'source'    => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
                'default'   => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::VALUE_NO,
                'required'  => 0,
                'sort_order'=> 99,
                'position'  => 99,
                'system'    => 0,
                'visible'   => 1,
                'global'    => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
            ]
        );

        $freeCatalogAttr = $customerSetup
            ->getEavConfig()
            ->getAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'free_catalogue'
            );

        $forms = [
            'adminhtml_customer',
            'checkout_register',
            'customer_account_create',
            'customer_account_edit',
            'adminhtml_checkout'
        ];

        $freeCatalogAttr->setData('used_in_form', $forms)
            ->setData('is_used_for_customer_segment', true)
            ->setData('is_system', 0)
            ->setData('is_user_defined', 1)
            ->setData('is_visible', 1)
            ->setData('sort_order', 99);

        $freeCatalogAttr->save();

        $setup->endSetup();
    }
}

After launching setup:upgrade and setup:static-content:deploy the attribute is created correctly in the database, but when I go into admin or frontend, I can not find the field in the customer.
I did not understand, if I configure:

$forms = [
            'adminhtml_customer',
            'checkout_register',
            'customer_account_create',
            'customer_account_edit',
            'adminhtml_checkout'
        ];

I should see the new field in adminhtml_customer, checkout_register

Where am I wrong?

Best Answer

you can use my below install script

<?php


namespace Vendor\Customer\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;

class InstallData implements InstallDataInterface
{

    private $customerSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'free_catalogue', [
            'type' => 'int',
            'label' => 'free_catalogue',
            'input' => 'select',
            'source' => 'Vendor\Customer\Model\Customer\Attribute\Source\FreeCatalogue',
            'required' => true,
            'visible' => true,
            'position' => 333,
            'system' => false,
            'backend' => ''
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'free_catalogue')
        ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout',
                'customer_account_create',
                'customer_account_edit'
            ]
        ]);
        $attribute->save();
    }
}
Related Topic