0

I wish to create customer custom attributes with following types: 1.radio buttons 2.checkbox 3.dropdowns

Please provide me a solution to create customer custom attribute.

2 Answers 2

0

Create Extension and your setup file below
here customer=>namespace & Attribute=>Extension name

namespace Customer\Attribute\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;

class CustomerSetup extends EavSetup {

    protected $eavConfig;

    public function __construct(
        ModuleDataSetupInterface $setup,
        Context $context,
        CacheInterface $cache,
        CollectionFactory $attrGroupCollectionFactory,
        Config $eavConfig
        ) {
        $this -> eavConfig = $eavConfig;
        parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
    } 

    public function installAttributes($customerSetup) {
        $this -> installCustomerAttributes($customerSetup);
        $this -> installCustomerAddressAttributes($customerSetup);
    } 

    public function installCustomerAttributes($customerSetup) {


        $customerSetup -> addAttribute(\Magento\Customer\Model\Customer::ENTITY,
            'dropdown',
            [
            'label' => 'Radio Button',
            'system' => 0,
            'position' => 100,
            'sort_order' =>100,
            'visible' =>  true,
            'note' => '',


                        'type' => 'int',
                        'input' => 'select',
                        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',

            ]
            );

        $customerSetup -> getEavConfig() -> getAttribute('customer', 'dropdown')->setData('is_user_defined',1)->setData('is_required',0)->setData('default_value','')->setData('used_in_forms', ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout']) -> save();



        $customerSetup -> addAttribute(\Magento\Customer\Model\Customer::ENTITY,
            'multiple',
            [
            'label' => 'Multiple',
            'system' => 0,
            'position' => 100,
            'sort_order' =>100,
            'visible' =>  true,
            'note' => '',


                        'type' => 'varchar',
                        'input' => 'multiselect',
                        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                        'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',

            ]
            );

        $customerSetup -> getEavConfig() -> getAttribute('customer', 'multiple')->setData('is_user_defined',1)->setData('is_required',0)->setData('default_value','')->setData('used_in_forms', ['adminhtml_customer', 'checkout_register', 'customer_account_create', 'customer_account_edit', 'adminhtml_checkout']) -> save();


    } 

    public function installCustomerAddressAttributes($customerSetup) {

    } 

    public function getEavConfig() {
        return $this -> eavConfig;
    } 
} 
2
  • ,thanks for the response. In source file, shall i use my custom php file with more than two options
    – Jaisa
    Jan 3, 2019 at 4:49
  • Yes For multiple options how can we set the source attribute?
    – Stack user
    Feb 27, 2019 at 11:42
-1

For Checkbox:

"type"     => "int",
"backend"  => "",
"label"    => "Test",
"input"    => "boolean",
"source"   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
"visible"  => true,
"required" => true,
"default" => "",
"frontend" => "",
"unique"     => false,
"note"       => ""
2
  • 1
    boolean is for only true or false right?
    – Jaisa
    Jan 2, 2019 at 12:48
  • I have multiple select checkboxes
    – Jaisa
    Jan 2, 2019 at 12:48

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.