I was trying to add new custom attribute in customer registration. Field's value do not save in database.
Please can anyone help me?
I was trying to add new custom attribute in customer registration. Field's value do not save in database.
Please can anyone help me?
Customer Registration Custom attribute value create in admin and save
1. Text Field
2. Drop Down Field
3. Date Field
Using UpgradeSchema.php
<?php
namespace {CompanyName}\{ModuleName}\Setup;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
/* irrelevant */
#use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
/* irrelevant */
#use Magento\Framework\Setup\SchemaSetupInterface;
/* add this */
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class UpgradeData implements UpgradeDataInterface
{
private $customerSetupFactory;
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
if (version_compare($context->getVersion(), '1.0.1', '<'))
{
// For Text field
$customerSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'attribute_title',
[
'type' => 'text',
'input' => 'text',
'label' => 'Attribute Title',
'required' => false,
'visible' => true,
'user_defined' => false,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]
);
$attribute_title = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'attribute_title')
->addData(
['used_in_forms' => ['adminhtml_customer']
]);
$attribute_title->save();
//Add field Drop Down for Yes/No
$customerSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'is_attribute',
[
'type' => 'int',
'input' => 'select',
'label' => 'Is Attribute',
'frontend' => '',
'default' => '1',
'class' => '',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'required' => false,
'visible' => true,
'user_defined' => false,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]
);
$is_attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_attribute')
->addData(
['used_in_forms' => ['adminhtml_customer']
]);
$is_attribute->save();
// For Date And Time field
$customerSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'custom_date',
[
'label' => 'Custom Date',
'type' => 'datetime',
'input' => 'date',
'frontend' => 'Magento\Eav\Model\Entity\Attribute\Frontend\Datetime',
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\Datetime',
'validate_rules' => '{"input_validation":"date"}',
'user_defined' => false,
'required' => false,
'visible' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]
);
$custom_date = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_date')
->addData(
['used_in_forms' => ['adminhtml_customer']
]);
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$custom_date->save();
}
}
}
You can try to add the new attribute to an attribute set and attribute group when install or upgrade data:
<?php
namespace Custom\Attribute\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Eav\Model\AttributeRepository;
class InstallData implements InstallDataInterface
{
/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* @var GroupFactory
*/
protected $groupFactory;
/**
* @var AttributeRepository
*/
protected $attributeRepository;
/**
* @var AttributeSetFactory
*/
protected $attributeSetFactory;
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeRepository $attributeRepository,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeRepository = $attributeRepository;
$this->attributeSetFactory = $attributeSetFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var \Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute(Customer::ENTITY, 'anniversary_year', [
'label' => 'Anniversary Year',
'input' => 'text',
'type' => 'varchar',
'required' => false,
'visible' => true,
'system' => false,
'is_used_in_grid' => false,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false,
'is_searchable_in_grid' => false
]);
$newAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'anniversary_year')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['customer_account_create', 'checkout_register' , 'adminhtml_customer'],
]);
$newAttribute->save();
$this->attributeRepository->save($newAttribute);
$setup->endSetup();
}
}
Note: Here I am showing you how to create a customer custom attribute using simple steps.
Please follow the below steps using a tool called "Silk Software module creator" http://www.silksoftware.com/magento-module-creator/magento2x.php
Screen shot:
Step 1: Create a Custom module by the below screen shot
Step 2: Next select the 'Need to Add Customer Attribute' option and mention the details as shown in the below screen shot
Step 3: Now Download the module
Step 4: Follow the install module steps Clear flush cache and check the same in the admin end
Output: