cancel
Showing results for 
Search instead for 
Did you mean: 

How to add field in Customer form for Admin and save this field into customer database

How to add field in Customer form for Admin and save this field into customer database

I am trying to add one field in add customer admin form  i am able to this add field but  this field value is not saving in customer database table. so can anyone tell me what i have to do . Please reply me with example as soon as possible . 

8 REPLIES 8

Re: How to add field in Customer form for Admin and save this field into customer database

Please share your code for customer attribute. So User can debug your issue.

 

You can create Customer attribute by the following link,Create Customer attribute 

 

Thanks.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: How to add field in Customer form for Admin and save this field into customer database

Field is add in form but what i have to do to save this field in to customer_entity table

Re: How to add field in Customer form for Admin and save this field into customer database

I added attribute in the customer registration form , data saved in the database but unable to fetch data in the list of customers/grid screen.it shows blank data in grid ..

 

Re: How to add field in Customer form for Admin and save this field into customer database

Could you please share your customer grid code ? So issue can be detected if any mismatch in code happened.

What is your custom customer attribute code?
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: How to add field in Customer form for Admin and save this field into customer database

All are  in vendor files

------------------------------------customer_form.xml---------------------------

     <field name="corporate_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                   <item name="label" xsi:type="string" translate="true">Corporate ID</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">customer</item>
                </item>
            </argument>

 

--------------------/customer_list.xml --------------------------------------


         <column name="corporate_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Corporate ID</item>
                    <item name="sortOrder" xsi:type="number">30</item>
                </item>
            </argument>
        </column>

 

-------------------------------installSchema.xml------------------------------------

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Customer\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        /**
         * Create table 'customer_entity'
         */
        $table = $installer->getConnection()->newTable(
            $installer->getTable('customer_entity')
        )->addColumn(
            'entity_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Entity Id'
        )->addColumn(
            'website_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['unsigned' => true],
            'Website Id'
        )->addColumn(
            'email',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            [],
            'Email'
         )->addColumn(
            'corporate_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            255,
            [],
            'Corporate Id'
        )->addColumn(
            'group_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['unsigned' => true, 'nullable' => false, 'default' => '0'],
            'Group Id'
            ['unsigned' => true, 'nullable' => true, 'default' => null],
            'Default Billing Address'
       )->addIndex(
            $installer->getIdxName(
                'customer_entity',
                ['email', 'website_id'],
                \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
            ),
            ['email', 'website_id'],
            ['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE]
        )->addIndex(
            $installer->getIdxName('customer_entity', ['website_id']),
            ['website_id']
        )->addIndex(
            $installer->getIdxName('customer_entity', ['firstname']),
            ['firstname']
        )->addIndex(
            $installer->getIdxName('customer_entity', ['corporate_id']),
            ['corporate_id']
        )->addIndex(
            $installer->getIdxName('customer_entity', ['lastname']),
            ['lastname']
        )->addForeignKey(
            $installer->getFkName('customer_entity', 'store_id', 'store', 'store_id'),
            'store_id',
            $installer->getTable('store'),
            'store_id',
            \Magento\Framework\DB\Ddl\Table::ACTION_SET_NULL
        )->addForeignKey(
            $installer->getFkName('customer_entity', 'website_id', 'store_website', 'website_id'),
            'website_id',
            $installer->getTable('store_website'),
            'website_id',
            \Magento\Framework\DB\Ddl\Table::ACTION_SET_NULL
        )->setComment(
            'Customer Entity'
        );
        $installer->getConnection()->createTable($table);

        /**
         * Create table 'customer_address_entity'
         */
     
        /**
         * Create table 'customer_eav_attribute_website'
         */
        $table = $installer->getConnection()->newTable(
            $installer->getTable('customer_eav_attribute_website')
        )->addColumn(
            'attribute_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['unsigned' => true, 'nullable' => false, 'primary' => true],
            'Attribute Id'
        )->addColumn(
            'website_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['unsigned' => true, 'nullable' => false, 'primary' => true],
            'Website Id'
        )->addColumn(
            'is_visible',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['unsigned' => true],
            'Is Visible'
        )->addColumn(
            'is_required',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['unsigned' => true],
            'Is Required'
        )->addColumn(
            'default_value',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            '64k',
            [],
            'Default Value'
        )->addColumn(
            'multiline_count',
            \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
            null,
            ['unsigned' => true],
            'Multiline Count'
        )->addIndex(
            $installer->getIdxName('customer_eav_attribute_website', ['website_id']),
            ['website_id']
        )->addForeignKey(
            $installer->getFkName('customer_eav_attribute_website', 'attribute_id', 'eav_attribute', 'attribute_id'),
            'attribute_id',
            $installer->getTable('eav_attribute'),
            'attribute_id',
            \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
        )->addForeignKey(
            $installer->getFkName('customer_eav_attribute_website', 'website_id', 'store_website', 'website_id'),
            'website_id',
            $installer->getTable('store_website'),
            'website_id',
            \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
        )->setComment(
            'Customer Eav Attribute Website'
        );
        $installer->getConnection()->createTable($table);

        /**
         * Create table 'customer_visitor'
         */
        $table = $installer->getConnection()->newTable(
            $installer->getTable('customer_visitor')
        )->addColumn(
            'visitor_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_BIGINT,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Visitor ID'
        )->addColumn(
            'customer_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            null,
            [],
            'Customer Id'
        )->addColumn(
            'session_id',
            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            64,
            ['nullable' => true, 'default' => null],
            'Session ID'
        )->addColumn(
            'last_visit_at',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
            null,
            ['nullable' => false],
            'Last Visit Time'
        )->addIndex(
            $installer->getIdxName('customer_visitor', ['customer_id']),
            ['customer_id']
        )->setComment(
            'Visitor Table'
        );
        $installer->getConnection()->createTable($table);

        $table = $installer->getConnection()
            ->newTable(
                $installer->getTable('customer_log')
            )
            ->addColumn(
                'log_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                [
                    'nullable' => false,
                    'identity' => true,
                    'primary' => true
                ],
                'Log ID'
            )
            ->addColumn(
                'customer_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                [
                    'nullable' => false
                ],
                'Customer ID'
            )
            ->addColumn(
                'last_login_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                [
                    'nullable' => true,
                    'default' => null
                ],
                'Last Login Time'
            )
            ->addColumn(
                'last_logout_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                [
                    'nullable' => true,
                    'default' => null
                ],
                'Last Logout Time'
            )
            ->addIndex(
                $installer->getIdxName(
                    $installer->getTable('customer_log'),
                    ['customer_id'],
                    \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
                ),
                ['customer_id'],
                [
                    'type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
                ]
            )
            ->setComment('Customer Log Table');
        $installer->getConnection()->createTable($table);

        $installer->endSetup();

    }
}
-------------------------------customerSetup.xml----------------------------------

 public function getDefaultEntities()
    {
        $entities = [
            'customer' => [
                'entity_type_id' => \Magento\Customer\Api\CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
                'entity_model' => 'Magento\Customer\Model\ResourceModel\Customer',
                'attribute_model' => 'Magento\Customer\Model\Attribute',
                'table' => 'customer_entity',
                'increment_model' => 'Magento\Eav\Model\Entity\Increment\NumericValue',
                'additional_attribute_table' => 'customer_eav_attribute',
                'entity_attribute_collection' => 'Magento\Customer\Model\ResourceModel\Attribute\Collection',
                'attributes' => [
                    'website_id' => [
                        'type' => 'static',
                        'label' => 'Associate to Website',
                        'input' => 'select',
                        'source' => 'Magento\Customer\Model\Customer\Attribute\Source\Website',
                        'backend' => 'Magento\Customer\Model\Customer\Attribute\Backend\Website',
                        'sort_order' => 10,
                        'position' => 10,
                        'adminhtml_only' => 1,
                    ],
                    'store_id' => [
                        'type' => 'static',
                        'label' => 'Create In',
                        'input' => 'select',
                        'source' => 'Magento\Customer\Model\Customer\Attribute\Source\Store',
                        'backend' => 'Magento\Customer\Model\Customer\Attribute\Backend\Store',
                        'sort_order' => 20,
                        'visible' => false,
                        'adminhtml_only' => 1,
                    ],
                    'corporate_id' => [
                        'type' => 'static',
                        'input' => 'text',
                        'backend' => 'Magento\Customer\Model\Customer\Attribute\Backend\Corporate',
                        'required' => false,
                        'sort_order' => 20,
                        'visible' => false,
                        'position' => 20,
                         'adminhtml_only' => 1,
                    ],]]

}

----------------------------------------------indexer.xml-------------------------

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
    <indexer id="customer_grid" view_id="customer_dummy" class="Magento\Framework\Indexer\Action\Entity" primary="customer">
        <title translate="true">Customer Grid</title>
        <description translate="true">Rebuild Customer grid index</description>

        <fieldset name="customer" source="Magento\Customer\Model\ResourceModel\Customer\Collection"
                  provider="Magento\Customer\Model\Indexer\AttributeProvider">
            <field name="corporate_id" xsi:type="searchable" dataType="varchar"/>       
            <field name="name" xsi:type="searchable" dataType="text" handler="CustomerNameHandler"/>
            <field name="email" xsi:type="searchable" dataType="varchar"/>

            <field name="group_id" xsi:type="filterable" dataType="int"/>
            <field name="created_at" xsi:type="filterable" dataType="timestamp"/>
            <field name="website_id" xsi:type="filterable" dataType="int"/>
            <field name="confirmation" xsi:type="filterable" dataType="varchar"/>
            <field name="created_in" xsi:type="filterable" dataType="text"/>
            <field name="dob" xsi:type="filterable" dataType="date"/>
            <field name="gender" xsi:type="filterable" dataType="int"/>
            <field name="taxvat" xsi:type="searchable" dataType="varchar"/>
            <field name="lock_expires" xsi:type="filterable" dataType="timestamp" />
        </fieldset>

        <fieldset name="shipping" source="Magento\Customer\Model\ResourceModel\Address\Collection">
            <reference fieldset="customer" from="entity_id" to="default_shipping"/>
            <field name="full" xsi:type="searchable" dataType="text" handler="ShippingAddressHandler"/>
        </fieldset>

        <fieldset name="billing" source="Magento\Customer\Model\ResourceModel\Address\Collection"
                  provider="Magento\Customer\Model\Indexer\Address\AttributeProvider">
            <reference fieldset="customer" from="entity_id" to="default_billing"/>
            <field name="full" xsi:type="searchable" dataType="text" handler="BillingAddressHandler"/>
            <field name="firstname" xsi:type="searchable" dataType="varchar"/>
            <field name="lastname" xsi:type="searchable" dataType="varchar"/>
            <field name="telephone" xsi:type="searchable" dataType="varchar"/>
            <field name="postcode" xsi:type="searchable" dataType="varchar"/>
            <field name="country_id" xsi:type="filterable" dataType="varchar"/>
            <field name="region" xsi:type="searchable" dataType="varchar"/>
            <field name="street" xsi:type="searchable" dataType="varchar"/>
            <field name="city" xsi:type="searchable" dataType="varchar"/>
            <field name="fax" xsi:type="searchable" dataType="varchar"/>
            <field name="vat_id" xsi:type="searchable" dataType="varchar"/>
            <field name="company" xsi:type="searchable" dataType="varchar"/>
        </fieldset>

        <saveHandler class="Magento\Framework\Indexer\SaveHandler\Grid"/>
        <structure class="Magento\Framework\Indexer\GridStructure"/>
    </indexer>
</config>

 

 

----------------------------------fieldset.xml------------------------

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:frameworkSmiley Very HappyataObject/etc/fieldset.xsd">
    <scope id="global">
        <fieldset id="customer_account">
            <field name="prefix">
                <aspect name="create" />
                <aspect name="update" />
                <aspect name="name" />
            </field>
            <field name="corporate_id">
                <aspect name="create" />
                <aspect name="update" />
            </field>
            <field name="firstname">
                <aspect name="create" />
                <aspect name="update" />
                <aspect name="name" />
            </field>
            <field name="middlename">
                <aspect name="create" />
                <aspect name="update" />
                <aspect name="name" />
            </field>
            <field name="lastname">
                <aspect name="create" />
                <aspect name="update" />
                <aspect name="name" />
            </field>
            <field name="suffix">
                <aspect name="create" />
                <aspect name="update" />
                <aspect name="name" />
            </field>
            <field name="email">
                <aspect name="create" />
                <aspect name="update" />
            </field>
            <field name="password">
                <aspect name="create" />
            </field>
            <field name="confirmation">
                <aspect name="create" />
            </field>

        </fieldset>
    </scope>
</config>


      

 

Re: How to add field in Customer form for Admin and save this field into customer database

Have you check your value is inserted in customer_entity table with proper customer row?
Have you made all changes inside vendor module, you havent made your custom module.

Please run php bin/magento indexer:reindex command

Remove var folder from root.
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: How to add field in Customer form for Admin and save this field into customer database

No ,corporate_id  is saving into customer_entity Table but there are column of corporate_id in customer_entity so where i should add code for saving corporate_id in customer_entity table , have you any idea.

I  have been executed indexer command many times.

Re: How to add field in Customer form for Admin and save this field into customer database