0

I want to edit a field to the customer edit in admin html.

enter image description here

When you look at the picture I want to add a field below "First Name".

I don't want to create a customer attribute, I just want to add a field there.

Any idea how to do that?

1 Answer 1

0

app/code/VendoreName/ModuleName/Setup

InstallData.php

<?php

namespace VendoreName\ModuleName\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]);


        // for remove attribute
        //$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY,'address_book');

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        /* create customer Address book attribute */



        $customerSetup->addAttribute(
        Customer::ENTITY,'address_book', 
                [
                    'type' => 'varchar', // attribute with varchar type
                    'label' => 'Address Book',
                    'input' => 'text', // attribute input field is text
                    'required' => false, // field is not required
                    'visible' => true,
                    'user_defined' => true,
                    'position' => 50,
                    'sort_order' => 50,
                    '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,
                ]
            );
        $sampleAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'address_book')
            ->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();
    }
}

Before Run setup Upgrade command please delete you module entry form setup_module table and if attribute is already then uncomment remove line and remove addAttribute code. If it's not set proper position then change position and sort_order number.

I Hope This Helps You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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