I followed several tutorials like https://marketplace.magento.com/ followed the step but no luck for me. how to add 2 extra field in customer or user registration for frontend ,admin and database . any help would be appreciated.
1 Answer
For Example you need to insert the required field(telephone) into the table. I will always encourage to accomplish this using a custom module, although it is time taking, it is a standard and cleanest way.
- Create a standard module with minimal required files.
- Create InstallData.php in the following path NameSpace\ModuleName\Setup
namespace NameSpace\ModuleName\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
/**
* CustomerSetupFactory
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* $attributeSetFactory
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* initiate object
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
)
{
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* install data method
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
/**
* customer registration form default field mobile number
*/
$customerSetup->addAttribute(Customer::ENTITY, 'telephone', [
'type' => 'varchar',
'label' => 'Telephone',
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
//add attribute to attribute set
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'telephone')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'customer_account_create'],
]);
$attribute->save();
}
}
- Add the field to the frontend by copying module-
customer/view/frontend/templates/form/register.phtml
toapp/design/frontend/namespace/theme/Magento_Customer/templates/form/register.phtml
and add
<div class="field required">
<label for="telephone" class="label"><span><?= __('Telephone') ?></span></label>
<div class="control">
<input type="text" name="telephone" id="telephone" value="<?php echo $block->escapeHtml($block->getFormData()->getTelephone()) ?>" title="<?php echo __('Telephone') ?>" class="input-text" data-validate="{required:true, 'validate-phoneStrict':true}">
</div>
</div>
php bin/magento setup:upgrade
php bin/magento c:c
php bin/magento c:f
FOR MAGENTO 1.x PLEASE REFER TO THE LINK BELOW
-
thanks to reply but my question was for magento1.9. can you please do same for Magento 1– irshadAug 30, 2019 at 5:34
-
-
@irshad I'm updating my Question with a reference link, if u'll find it helpfull please accept is as answer. Aug 30, 2019 at 13:15
-
-