1

This question already has an answer here:

How to create a customer custom attribute in magento2?

marked as duplicate by Rakesh Jesadiya, Arunendra, Siarhey Uchukhlebau, Khoa TruongDinh, Rajeev K Tomy Mar 1 '17 at 7:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

Create the file Test\CustomAttribute\Setup\InstallData.php

<?php
namespace test\CustomAttribute\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;

/**
 * @codeCoverageIgnore
 */
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)
    {

        /** @var CustomerSetup $customerSetup */
        $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);

        $customerSetup->addAttribute(Customer::ENTITY, 'custom_attribute', [
            'type' => 'varchar',
            'label' => 'Custom Attribute',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'position' =>999,
            'system' => 0,
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_attribute')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
        ]);

        $attribute->save();
    }
}
  • using this i have created two attributes that are dispaly in "eav_attribute" table but not display in admin customer form .. can you plz tell me what is the problem behind that.? – Sarfaraj Sipai Jun 14 '18 at 9:05
0

Create a custom module and then put the setup file InstallData.php in app\code\YourPackageName\YourModuleName\Setup as shown bellow:

    <?php
    namespace YourPackageName\YourModuleName\Setup;

    use Magento\Framework\Module\Setup\Migration;
    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;



    class InstallData implements InstallDataInterface
    {
        /**
         * Customer setup factory
         *
         * @var \Magento\Customer\Setup\CustomerSetupFactory
         */
        private $customerSetupFactory;
        /**
         * Init
         *
         * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
         */
        public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
        {
            $this->customerSetupFactory = $customerSetupFactory;
        }
        /**
         * Installs DB schema for a module
         *
         * @param ModuleDataSetupInterface $setup
         * @param ModuleContextInterface $context
         * @return void
         */
        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        {

            $installer = $setup;
            $installer->startSetup();

            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
            $entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
            $customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "mobile");

            $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "mobile",  array(
                "type"     => "varchar",
                "backend"  => "",
                "label"    => "Mobile No",
                "input"    => "text",
                "source"   => "",
                "visible"  => true,
                "required" => true,
                "default" => "",
                "frontend" => "",
                "unique"     => false,
                "note"       => ""

            ));

            $mobile   = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "mobile");

            $mobile = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'mobile');
            $used_in_forms[]="adminhtml_customer";
            $used_in_forms[]="checkout_register";
            $used_in_forms[]="customer_account_create";
            $used_in_forms[]="customer_account_edit";
            $used_in_forms[]="adminhtml_checkout";
            $mobile->setData("used_in_forms", $used_in_forms)
                ->setData("is_used_for_customer_segment", true)
                ->setData("is_system", 0)
                ->setData("is_user_defined", 1)
                ->setData("is_visible", 1)
                ->setData("sort_order", 100);

            $mobile->save();



            $installer->endSetup();
        }
    }