Magento 2 customer Custom field

by Ravindra   Last Updated September 07, 2018 07:09 AM

How to create custom field for customer and display that custom field in the page using plugin interpertor

Tags : custom-field


Answers 2


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();
    }
}
Ravi Oza
Ravi Oza
September 07, 2018 06:55 AM

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>
Naveed
Naveed
September 07, 2018 07:07 AM

Related Questions






Custom Field in Product Review Form, Magento 1.9

Updated May 20, 2017 19:09 PM