0

How to add a new field to this section and save DB: enter image description here

Please help me.

0

To create an customer address attribute you need to create a module, I am assuming that you know hot create a module in Magento2. If not please follow some blogs or this links or this

after creating module, you can use this code in your InstallData or UpgradeData

use Magento\Customer\Setup\CustomerSetupFactory;

  /**
     * Customer setup factory
     *
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Init
     *
     * @param CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }

Now you can use this like,

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

    $setup->startSetup();


    $attributesInfo = [
        'YOUR_ATTRIBUTE)ID' => [
            'label' => 'YOUR ATTRIBUTE LABEL',
            'type' => 'static',
            'input' => 'text',
            'position' => 140,
            'visible' => true,
            'required' => false,
        ]
    ];

    foreach ($attributesInfo as $attributeCode => $attributeParams) {
        $customerSetup->addAttribute('customer_address', $attributeCode, $attributeParams);
    }
    $setup->endSetup();
}
8
  • I have try. And add new field to front-end , but can not save DB. Can you describe more?
    – baovic
    Aug 23 '17 at 8:06
  • I think in question you asked to how to create a new field in customer address, @baovic
    – Keyur Shah
    Aug 23 '17 at 8:09
  • and If you are facing an Issue with saving its data then try to clear cache and di:compile @baovic
    – Keyur Shah
    Aug 23 '17 at 8:09
  • Please see the answer below.
    – baovic
    Aug 23 '17 at 8:17
  • Thank Keyur Shah, I have try to clear cache and di, but not change.
    – baovic
    Aug 23 '17 at 9:33

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.