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.

Greg
2,7495 gold badges42 silver badges89 bronze badges
asked Jan 19, 2018 at 9:55
1
  • 1
    Before asking question you should accept the previous answer to clean the community 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();
 }
answered Jan 19, 2018 at 10:28
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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.