3

I am trying to add a custom attribute using below code but its not working -

     
    namespace A2bizz\Customer\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)
        {
                    $setup->startSetup();


            /** @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, 'a2bizz_attribute2', [
                'type' => 'varchar',
                'label' => 'Custom Attribute2',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 1000,
                'position' => 1000,
                'system' => 0,
            ]);

            $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'a2bizz_attribute2')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer'],
            ]);

            $attribute->save();
            $setup->endSetup();
        }
    }

I check the backend entries in database , the entry of attribute is added to the tables , eav_attribute and customer_eav_attribute

but still it is not showing in edit form in admin section.

I also run below commands -

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
php bin/magento cache:flush
1

Check with following. This is working for me to add custom attribute -

A2bizz/Example/Setup/InstallData.php

namespace A2bizz\Example\Setup;

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

class InstallData implements InstallDataInterface {

    private $_eavSetupFactory;
    private $_attributeRepository;

    public function __construct(
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        \Magento\Eav\Model\AttributeRepository $attributeRepository
    )
    {
        $this->_eavSetupFactory = $eavSetupFactory;
        $this->_attributeRepository = $attributeRepository;
    }

    public function install( ModuleDataSetupInterface $setup, ModuleContextInterface $context )
    {

        $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);

        // add customer_attribute to customer
        $eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_attribute');
        $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY, 'customer_attribute', [
            'type' => 'varchar',
            'label' => 'Custom Customer Attribute',
            'input' => 'text',
            'required' => false,
            'system' => 0,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'sort_order' => '200'
        ]
    );

    // allow customer_attribute attribute to be saved in the specific areas
    $attribute = $this->_attributeRepository->get('customer', 'customer_attribute');
    $setup->getConnection()
    ->insertOnDuplicate(
        $setup->getTable('customer_form_attribute'),
        [
            ['form_code' => 'adminhtml_customer', 'attribute_id' => $attribute->getId()]
        ]
    );
    }
}
  • @amit_game did it work ? – zed Blackbeard Aug 4 '18 at 10:04
  • yes, it worked for me – amit_game Sep 28 '18 at 13:57

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.