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.
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;
}
}
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" => ""