Magento 2: Getting Error When Adding Customer Address Attribute

by Saif Zakir   Last Updated July 12, 2019 13:09 PM - source

I'm using Magento 2.2.3, i need to add Customer Address Attribute, but when i run setup:upgrade I'm getting error of Wrong Entity ID.

Here is My code: CustomerAddress\Attribute\Setup\InstallData.php

<?php

namespace CustomerAddress\Attribute\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class InstallData implements InstallDataInterface
{
    /**
     * @var Config
     */
    private $eavConfig;

     /**
     * @var EavSetupFactory
     */
    private $_eavSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * @param Config $eavConfig
     * @param EavSetupFactory $eavSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        Config $eavConfig,
        EavSetupFactory $eavSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->eavConfig            = $eavConfig;
        $this->_eavSetupFactory     = $eavSetupFactory;
        $this->attributeSetFactory  = $attributeSetFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

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

        $eavSetup->addAttribute('customer_tax_exempt_number_', 'custom_address_attribute', [
            'type' => 'varchar',
            'input' => 'text',
            'label' => 'Customer Tax Exempt Number',
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'system'=> false,
            'group'=> 'General',
            'global' => true,
            'visible_on_front' => true,
        ]);

        $customAttribute = $this->eavConfig->getAttribute('customer_tax_exempt_number', 'custom_address_attribute');

        $customAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer_address','customer_address_edit','customer_register_address'] //list of forms where you want to display the custom attribute
        );
        $customAttribute->save();

        $setup->endSetup();
    }
}

enter image description here



Answers 2


You have to include your dependencies in your ModuleName/etc/module.xml file.

<sequence>
    <module name="Magento_Customer" />
</sequence>
Faisal Sheikh
Faisal Sheikh
July 12, 2019 13:05 PM

Sorry I'm making a mistake by switching parameters, $eavSetup->addAttribute('customer_address', 'customer_tax_exempt_number', []

Here is the right code:

<?php

namespace CustomerAddress\Attribute\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class InstallData implements InstallDataInterface
{
    /**
     * @var Config
     */
    private $eavConfig;

     /**
     * @var EavSetupFactory
     */
    private $_eavSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * @param Config $eavConfig
     * @param EavSetupFactory $eavSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        Config $eavConfig,
        EavSetupFactory $eavSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->eavConfig            = $eavConfig;
        $this->_eavSetupFactory     = $eavSetupFactory;
        $this->attributeSetFactory  = $attributeSetFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

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

        $eavSetup->addAttribute('customer_address', 'customer_tax_exempt_number', [
            'type' => 'varchar',
            'input' => 'text',
            'label' => 'Customer Tax Exempt Number',
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'system'=> false,
            'group'=> 'General',
            'global' => true,
            'visible_on_front' => true,
        ]);

        $customAttribute = $this->eavConfig->getAttribute('customer_address', 'customer_tax_exempt_number');

        $customAttribute->setData(
            'used_in_forms',
            ['adminhtml_customer_address','customer_address_edit','customer_register_address'] //list of forms where you want to display the custom attribute
        );
        $customAttribute->save();

        $setup->endSetup();
    }
}
Saif Zakir
Saif Zakir
July 12, 2019 13:06 PM

Related Questions



InstallData not working

Updated October 05, 2017 07:09 AM

Need to save data while custom module installation

Updated November 26, 2018 09:09 AM


Magento2: Setup Script not loaded

Updated November 29, 2017 10:09 AM