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