I want to know how to create custom field for customer and display that custom field in the registration page using plugin interpertor.
Magento 2 – How to Add Customer Custom Field in Registration Form
custom-fieldcustomer-attributemagento2register
Related Solutions
You need to create a module and here is installData.php
namespace Custom\Module\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
/**
* Customer setup factory
*
* @var \Magento\Customer\Setup\CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* Init
*
* @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
*/
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* Installs DB schema for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "custom_field");
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "custom_field", array(
"type" => "varchar",
"backend" => "",
"label" => "Custom Field",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$custom_field = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "custom_field");
$custom_field = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_field');
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$custom_field->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 1002);
$custom_field->save();
$installer->endSetup();
}
}
Note : If you apply this script in existing module than, First you need to remove your module from Setup table from DB than run below commands to make it work.
setup:upgrade
setup:static-content:deploy
cache:clean
1) app/code/Vendor/Module/Setup/UpgradeData.php
<?php
namespace Vendor\Module\Setup;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Framework\Setup\ModuleContextInterface;
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){
$setup->startSetup();
if (version_compare($context->getVersion(), '1.0.1', '<')) {
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(
Customer::ENTITY,
'corporation_proproetorship',
[
'type' => 'int',
'label' => 'Corporation Or Proproetorship',
'input' => 'select',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
'required' => true,
'default' => '0',
'sort_order' => 160,
'system' => false,
'position' => 160,
'adminhtml_only' => 1,
'option' =>
array (
'values' =>
array (
0 => 'Corporation',
1 => 'Proproetorship',
),
),
]
);
$myAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'corporation_proproetorship');
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$myAttribute->setData(
'used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']);
$myAttribute->save();
}
$setup->endSetup();
}
}
2) app/design/frontend/{vendor}/{theme}/Magento_Customer/templates/form/register.phtml
Copy the content from Vendor/Magento/module-customer/view/frontend/templates/form/registration.phtml
then add this:
<div class="field required">
<label for="extra1" class="label"><span><?= $block->escapeHtml(__('Corporation Or Proproetorship')) ?></span></label>
<div class="control">
<select name="corporation_proproetorship" id="corporation_proproetorship" title="<?= $block->escapeHtmlAttr(__('Corporation or Proproetorship')) ?>" class="input-select">
<option disabled selected value> -- select an option -- </option>
<option value="0">Corporation</option>
<option value="1">Proproetorship</option>
</select>
</div>
</div>
</div>
Best Answer
For creating attribute create InstallData.php in you custom module and upgrade you module
For Adding field in Dashboard edit page
Using Object in phtml :
You can also do by overriding Magento\Customer\Block\Form\Edit if you don't want to use Object in phtml file
Using Overriding Block :
add below code where you want your field