0

I have created the custom field in customer address section in magento admin and attribute created successfully and visible in customer admin area using below code:-

 <?php
    namespace Vendor\Module\Setup;

   use Magento\Customer\Setup\CustomerSetupFactory;
    use Magento\Customer\Model\Customer;
    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
    /**
     * @codeCoverageIgnore
     */
    class InstallData implements InstallDataInterface
    {
        /**
         * Customer setup factory
         *
         * @var CustomerSetupFactory
         */
        private $customerSetupFactory;
        /**
         * @var AttributeSetFactory
         */
        private $attributeSetFactory;
        /**
         * Init
         *
         * @param CustomerSetupFactory $customerSetupFactory
         */
        public function __construct(
            CustomerSetupFactory $customerSetupFactory,
            AttributeSetFactory $attributeSetFactory
        ) {
            $this->customerSetupFactory = $customerSetupFactory;
            $this->attributeSetFactory = $attributeSetFactory;
        }
        /**
         * {@inheritdoc}
         * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
         */
        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        {
            /** @var CustomerSetup $customerSetup */
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
            $setup->startSetup();

            $kcaddressidInfo = [
                    'kc_address_id' => [
                    'label' => 'Custom address id',
                    'type' => 'int',
                    'input' => 'text',
                    'position' => 1000,
                    'visible' => true,
                    'required' => false,
                    'system' => 0,
                    'user_defined' => true,
                    'position' => 1000,
                ]
            ];
            $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer_address');
            $attributeSetId = $customerEntity->getDefaultAttributeSetId();
            /** @var $attributeSet AttributeSet */
            $attributeSet = $this->attributeSetFactory->create();
            $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

            foreach ($kcaddressidInfo as $attributeCode => $attributeParams) {
                $customerSetup->addAttribute('customer_address', $attributeCode, $attributeParams);
            }

            $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'kc_address_id');

            $attribute->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer'],
            ]);

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

but after created the attribute I am not able to save value in this attribute.

Any help would be appreciated.

1
  • 1
    Before asking question you should accept the previous answer to clean the community
    – Keyur Shah
    Commented Jan 19, 2018 at 10:50

1 Answer 1

1

Looks like you missed few parameters which is required to save its value like Global.

Try below code :

/**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $setup->startSetup();

        $kcaddressidInfo = [
                'kc_address_id' => [
                'label' => 'Custom address id',
                'type' => 'int',
                'input' => 'text',
                'backend' =>' ',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'position' => 1000,
                'visible' => true,
                'required' => false,
                'system' => 0,
                'user_defined' => true,
                'position' => 1000,
            ]
        ];
        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer_address');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        foreach ($kcaddressidInfo as $attributeCode => $attributeParams) {
            $customerSetup->addAttribute('customer_address', $attributeCode, $attributeParams);
        }

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'kc_address_id');

        $attribute->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);

        $attribute->save();
        $setup->endSetup();
    }
1
  • When I use your code it give me error: Something went wrong while saving the customer. Commented Jan 19, 2018 at 11:36

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.