0

I want to know how to create custom field for customer and display that custom field in the registration page using plugin interpertor.

0

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"  />
  • I'm done this, its inserting and updating into database but i'm not getting how to display value of that field in dashboard and text box of edit page – Ravindra Sep 7 '18 at 6:58
  • try with updated answer – Ravi Oza Sep 7 '18 at 7:29
  • I'm getting this error -> Call to a member function getCustomAttribute() on null after adding this code-> <input type="text" class="input-text" value=" <?= $block->getCustomer()->getCustomAttribute('custom_field')->getValue() ?> " name="custom_field" id="custom_field" data-input="custom_field" /> – Ravindra Sep 7 '18 at 8:01
  • @Ravindra maybe there is no value in your custom_field, Please first check your custom field save value properly ? – Nikunj Vadariya Sep 7 '18 at 8:24
  • value inserting in customer_entity_varchar – Ravindra Sep 7 '18 at 8:29
0

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>
  • I'm done this all, its inserting and updating into database but i'm not getting how to display value of that field in dashboard and text box of edit page – Ravindra Sep 7 '18 at 7:09
  • have you added templates and layout files ? did you also create block to get value and render on template ? if yes then do update your question and add code there. That way will be easy to help and understand what is wrong. – Naveed Asim Sep 7 '18 at 7:11
  • I'm not created block – Ravindra Sep 7 '18 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.