How to add second field in registration form in Magento 2?
Frage
I added one attribute using this code but when I try to add another attribute then it is not working
why?
<?php
namespace Icore\Module\Setup;
use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
private $eavSetupFactory;
private $eavConfig;
private $attributeResource;
public function __construct(
\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
\Magento\Eav\Model\Config $eavConfig,
\Magento\Customer\Model\ResourceModel\Attribute $attributeResource
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
$this->attributeResource = $attributeResource;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->removeAttribute(Customer::ENTITY, "skype");
$attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
$attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);
$eavSetup->addAttribute(Customer::ENTITY, 'skype', [
// Attribute parameters
'type' => 'varchar',
'label' => 'Skype Account',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 990,
'position' => 990,
'system' => 0,
]);
$attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'skype');
$attribute->setData('attribute_set_id', $attributeSetId);
$attribute->setData('attribute_group_id', $attributeGroupId);
$eavSetup->removeAttribute(Customer::ENTITY, "bussiness");
$attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
$attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);
$eavSetup->addAttribute(Customer::ENTITY, 'bussiness', [
// Attribute parameters
'type' => 'varchar',
'label' => 'Check',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
$attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'bussiness');
$attribute->setData('attribute_set_id', $attributeSetId);
$attribute->setData('attribute_group_id', $attributeGroupId);
/*
//You can use this attribute in the following forms
adminhtml_checkout
adminhtml_customer
adminhtml_customer_address
customer_account_create
customer_account_edit
customer_address_edit
customer_register_address
*/
$attribute->setData('used_in_forms', [
'adminhtml_customer',
'customer_account_create',
'customer_account_edit'
]);
$this->attributeResource->save($attribute);
}
}
?>
also added addtional.phtml but not showing in registration form Magento 2 (Only showing "skype account")
<div class="field skype required">
<label class="label" for="skype">
<span><?= $block->escapeHtml(__('Skype Account')) ?></span>
</label>
<div class="control">
<input type="text" name="skype" id="skype" value="" title="<?= $block->escapeHtmlAttr(__('Skype Account')) ?>" class="input-text" data-validate="{required:false}">
</div>
</div>
<div class="field skype required">
<label class="label" for="skype2">
<span><?= $block->escapeHtml(__('Check')) ?></span>
</label>
<div class="control">
<input type="text" name="skype2" id="skype2" value="" title="<?= $block->escapeHtmlAttr(__('Check')) ?>" class="input-text" data-validate="{required:false}">
</div>
</div>
<div class="field tcagreecreateaccount required">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" data-validate="{required:true}" class="input-checkbox checkbox required" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom T&C') ?>
</label>
</div>
</div>
Any Idea? Please help!
Lösung
In your case the second attribute isn't installed in magento table, since because InstallData.php and InstallSchema.php will trigger when the module is installed. So the second time after you modified your InstallData.php for second attribute the file won't trigger out there.
Magento 2 has a feature of UpgradeData.php and there we have to define our module version to upgrade data and the module version has to be declared in module.xml in your module.
For more information visti here
UpgradeData feature you can use when your module went to production and next time when you want to install new attribute in the sense you can use this feature and as of now I hope you extension didn't went to production yet so you can follow below steps to get it install your second attribute.
Steps to uninstall a module from database :
Run the below query in you database,
DELETE from setup_module where module = 'Icore_Module';
then modify your InstallData.php as per your requirement then run below commands to install the module
php bin/magento setup:upgrade
php bin/magento cache:flush
Hope this helps :)