4

I want to add a field in customer address create and save into customer information how can i add this. enter image description here i try this http://www.extensions.sashas.org/blog/magento-2-make-customer-attribute.html but it make a field in Account information.

  • You want to display it on fronted customer address field right ? – Suresh Chikani Mar 31 '17 at 8:12
  • yes, but it for later, now i want add a custom field in customer addresses in above image. – Nguyễn Hồng Quân Mar 31 '17 at 8:51
  • Check below my answer for reference. – Suresh Chikani Mar 31 '17 at 8:54
  • let my try. :)) – Nguyễn Hồng Quân Mar 31 '17 at 8:57
  • Have you try below solution ? – Suresh Chikani Mar 31 '17 at 13:43
7

As per your reference example, attribute field will display in account information.

You have used Customer::ENTITY instead of 'customer_address'

You can use below code for create customer address attribute, it will display in customer address.

$customerSetup->addAttribute('customer_address', 'magento_username', [
            'type' => 'varchar',
            'label' => 'Magento Username',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);

$attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'magento_username')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'],
        ]);

It is use for display in admin and fronted customer address form.

'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'],

Let me know if you have any query from this.

  • can we show this directly in checkout too? What parameter should I add to used_in_forms to do so? – Vivek Kumar Jan 7 '18 at 8:02
  • can we show this directly in checkout only shipping address? – Magento2 Devloper Mar 1 '18 at 5:58
1

Adding a field to the customer address involves creating a new customer address attribute (see eav_entity_type table), assigning that attribute to a form, and adding the eld to the frontend.

Magento 2 does not automatically update the frontend customer fields.

Again, keep in mind that customer and customer address attributes must be included in a form (customer_form_attribute table) so they can be saved.

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.