cancel
Showing results for 
Search instead for 
Did you mean: 

How to add 1 new backend field in Customer->AllCustomers->Account Information ...

SOLVED

How to add 1 new backend field in Customer->AllCustomers->Account Information ...

I need a field with the name "ERP-ID" after the field "Vertex customer code"

 

how to get it

 

Thank you ;-)

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to add 1 new backend field in Customer->AllCustomers->Account Information ...

this snippet is working perfectly :-)

 

i just created one file...

my/module/Setup/InstallData.php

 

<?php

namespace my\module\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
{
    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;
    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;
    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /*customersetupfactory instead of eavsetupfactory */
        $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);
        /* create customer phone number attribute */
        $customerSetup->addAttribute(Customer::ENTITY,'erpid',	[
                'type'         => 'varchar', // attribute with varchar type
                'label'        => 'ERP ID',
                'input'        => 'text',  // attribute input field is text
                'required'     => false,  // field is not required
                'visible'      => true,
                'user_defined' => true,
                'position'     => 555,
                'sort_order'  => 555,
                'system'       => 0,
                'is_used_in_grid' => 1,   //setting grid options
                'is_visible_in_grid' => 1,
                'is_filterable_in_grid' => 1,
                'is_searchable_in_grid' => 1,
                'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
                ]
        );
        $sampleAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'erpid')
            ->addData(
                [
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer','customer_account_edit','customer_account_create'],
                ]
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
            );
        $sampleAttribute->save();
    }
}

View solution in original post

8 REPLIES 8

Re: How to add 1 new backend field in Customer->AllCustomers->Account Information ...

Hi @like_mike 

 

I understand question you have asked - for that you will require to create a new customer attribute programatically Name - ERP-ID

 

To get to know how to create customer attribute you will require to refer following links :

 

https://www.mageplaza.com/magento-2-module-development/magento-2-add-customer-attribute-programmatic... 

 

https://magento.stackexchange.com/questions/88245/magento-2-how-to-create-a-customer-custom-attribut... 

 

http://excellencemagentoblog.com/blog/2018/04/17/add-customer-attributes-programmatically-in-magento... 

 

Hope it helps ! 

if issue solved,Click Kudos & Accept as Solution

Re: How to add 1 new backend field in Customer->AllCustomers->Account Information ...

thank you for yr answer .. :-)

 

it helps.. but the method "save()" is not avaiable anymore in v.2.3.2

 

InstallData is'nt possible and i got no changes in the tables in the DBs ..

 

Re: How to add 1 new backend field in Customer->AllCustomers->Account Information ...

Re: How to add 1 new backend field in Customer->AllCustomers->Account Information ...

Hi @like_mike 

 

Yes i am going to share the same link - it will also helps you to achieve the same.

 

Ultimately my solution for your problem is to create a customer attribute that's way you can achieve the functionality.

if issue solved,Click Kudos & Accept as Solution

Re: How to add 1 new backend field in Customer->AllCustomers->Account Information ...

 

 

with the InstallData.php i got the DB-entry in the table "eav_attribute"

 

this works ;-)

 

do you know, how i got the field backend with this (mymodule/etc/adminhtml/iu_component/xy.xml)

 

thank you for your answers :-)

 

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="general">
<field name="er_pid">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Magento\Config\Model\Config\Source\Yesno</item>
<item name="config" xsi:type="array">
<item name="sortOrder" xsi:type="number">60</item>
<item name="dataType" xsi:type="string">string</item>
<item name="formElement" xsi:type="string">select</item>
<item name="label" xsi:type="string" translate="true">My Attribute </item>
</item>
</argument>
</field>
</fieldset>
</form>

 

Re: How to add 1 new backend field in Customer->AllCustomers->Account Information ...

Hi @like_mike 

 

Well the file and code you have placed it is for category attribute not for customer.

 

As you have already created customer attribute - it will be there at backend - you don't require to put this file.

 

If you want the same in backend customer edit form then you will require to create file on this location -  adminhtml/ui_component/customer_form.xml  and put below code in it :

 

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="customer">
        <field name="your_sample_attribute1" sortOrder="5" formElement="checkbox">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="default" xsi:type="number">0</item>
                </item>
            </argument>
        </field>
        <field name="your_sample_attribute2" sortOrder="6" formElement="multiselect">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="default" xsi:type="number">0</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Later you need to adjust that code based on your attribute name and its type.

 

Refer this link for more details - http://www.magesolution.com/blog/how-to-create-customer-attribute-in-magento2/

 

 

if issue solved,Click Kudos & Accept as Solution

Re: How to add 1 new backend field in Customer->AllCustomers->Account Information ...

whats the best way to fill in the parameters..

 

for both files

 

mymodule/etc/adminhtml/ui_component/customer_form.xml 

and

mymodule/Setup/InstallData.php

 

i need only 1 field named "erp_id" as a string or varchar(128)

in the backend form "customer information"

 

aaa.png

 

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Ibnab\CustomAttribute\Setup;
use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Setup\CategorySetupFactory;
 
/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * Category setup factory
     *
     * @var CategorySetupFactory
     */
    private $categorySetupFactory;
 
    /**
     * Init
     *
     * @param CategorySetupFactory $categorySetupFactory
     */
    public function __construct(CategorySetupFactory $categorySetupFactory)
    {
        $this->categorySetupFactory = $categorySetupFactory;
    }
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
         $installer->startSetup();
 
        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
        $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY);
        $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
         $categorySetup->removeAttribute(
        \Magento\Catalog\Model\Category::ENTITY, 'my_attribute' );
        $categorySetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY, 'my_attribute', [
             'type' => 'int',
             'label' => 'My Atrribute ',
             'input' => 'select',
             'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
             'required' => false,
             'sort_order' => 100,
             'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
             'group' => 'General Information',
        ]
    );
$installer->endSetup();
    }
}
 

Re: How to add 1 new backend field in Customer->AllCustomers->Account Information ...

this snippet is working perfectly :-)

 

i just created one file...

my/module/Setup/InstallData.php

 

<?php

namespace my\module\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
{
    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;
    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;
    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /*customersetupfactory instead of eavsetupfactory */
        $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);
        /* create customer phone number attribute */
        $customerSetup->addAttribute(Customer::ENTITY,'erpid',	[
                'type'         => 'varchar', // attribute with varchar type
                'label'        => 'ERP ID',
                'input'        => 'text',  // attribute input field is text
                'required'     => false,  // field is not required
                'visible'      => true,
                'user_defined' => true,
                'position'     => 555,
                'sort_order'  => 555,
                'system'       => 0,
                'is_used_in_grid' => 1,   //setting grid options
                'is_visible_in_grid' => 1,
                'is_filterable_in_grid' => 1,
                'is_searchable_in_grid' => 1,
                'validate_rules' => '{"max_text_length":255,"min_text_length":1}',
                ]
        );
        $sampleAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'erpid')
            ->addData(
                [
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer','customer_account_edit','customer_account_create'],
                ]
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
            );
        $sampleAttribute->save();
    }
}