How to add Customer custom field then display it in registration form
-
25-01-2021 - |
سؤال
I want to know how to create custom field for customer and display that custom field in the registration page using plugin interpertor.
المحلول
For creating attribute create InstallData.php in you custom module and upgrade you module
namespace Compony\Custom\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
class InstallData implements InstallDataInterface
{
private $customerSetupFactory;
/**
* Constructor
*
* @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* {@inheritdoc}
*/
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_field', [
'type' => 'varchar',
'label' => 'Custom Field',
'input' => 'text',
'source' => '',
'required' => false,
'visible' => true,
'position' => 333,
'system' => false,
'backend' => ''
]);
$attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'custom_field')
->addData(['used_in_forms' => [
'adminhtml_customer',
'adminhtml_checkout',
'customer_account_create',
'customer_account_edit'
]
]);
$attribute->save();
}
}
For Adding field in Dashboard edit page
Using Object in phtml :
../vendor/magento/module-customer/view/frontend/templates/form/edit.phtml
<?php $objm = \Magento\Framework\App\ObjectManager::getInstance(); ?>
<?php $customerSession = $objm->get('Magento\Customer\Model\Session'); ?>
<input type="text" class="input-text" value="<?php echo $customerSession->getCustomer()->getCustomField();?>" name="custom_field" id="custom_field" data-input="custom_field" />
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 :
../Compony/Custom/Block/Magento/Customer/Form/Edit.php
<?php
namespace Compony\Custom\Block\Magento\Customer\Form;
class Edit extends \Magento\Customer\Block\Form\Edit
{
public function getCustomField()
{
return $this->customerSession->getCustomer()->getCustomField();
}
}
../Compony/Custom/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Block\Form\Edit" type="Compony\Custom\Block\Magento\Customer\Form\Edit" />
</config>
../vendor/magento/module-customer/view/frontend/templates/form/edit.phtml
add below code where you want your field
<input type="text" class="input-text" value="<?php echo $block->getCustomField();?>" name="custom_field" id="custom_field" data-input="custom_field" />
نصائح أخرى
to show attribute on dashboard / frontend you have to create layout and template in your module folder (view/layout):
your template will look like that ( i.e view/frontend/templates/additionalinfocustomer.phtml ):
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** @var \Magento\Customer\Block\Form\Register $block */
?>
<fieldset class="fieldset create account" data-hasrequired="<?php /* @escapeNotVerified */ echo __('* Required Fields') ?>">
<legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Additional Information') ?></span></legend>
<div class="field linkedin_profile required">
<label for="linkedin_profile" class="label"><span><?php /* @escapeNotVerified */ echo __('Custom Field') ?></span></label>
<div class="control">
<input type="text"
name="custom_field"
id="custom_field"
title="<?php /* @escapeNotVerified */ echo __('custom field') ?>"
value=""
class="input-text validate-length maximum-length-250"
data-validate="{required:true, 'validate-url':true, 'maxlength':250}"
autocomplete="off" />
</div>
</div>
</fieldset>
call template file in your view/frontend/layout/customer_account_create.xml file :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Framework\View\Element\Template" name="customer_custom_field" template="Vendor_CustomeModule::additionalinfocustomer.phtml"/>
</referenceContainer>
</body>
</page>