1

I have created the multi-select attribute for customer address using below code.

Setup/UpgradeData.php

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer_address');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        $customerSetup->addAttribute('customer_address', 'address_purpose', [
            'type' => 'text',
            'label' => 'Address Purpose',
            'input' => 'multiselect',
            'required' => false,
            'visible' => false,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'source' => 'Vendor\Module\Model\Entity\Attribute\Source\AddressPurpose'
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'address_purpose')
            ->setData([
                'used_in_forms' =>  ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address','customer_address']
            ]);
        $attribute->save();

Here is my AddressPurpose.php

  use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

 class AddressPurpose extends AbstractSource
  {
   public function getAllOptions()
    {
        return [
           'option1' => [
            'label' => 'Delivery',
            'value' => 'delivery'
        ],
        'option2' => [
            'label' => 'General',
            'value' => 'general'
           ]
        ];
     }
   }

Using above code customer address attribute created successfully.

When I open any customer in admin and selected the option and click Save, The customer getting saved but the selected options are not getting saved.

Again I edit the same customer, Address purpose attribute values empty with none selected.

Have anyone face this issue? Please help me on this. Thanks

2

1 Answer 1

8

When creating the customer attribute, please take care about following things:

Attribute is added to the attribute set, group

customer_eav_attribute
eav_entity_attribute

Attribute is assigned to customer forms

customer_form_attribute

And the last one the most important as somehow, people just skip over it and wondering why the customer attribute does not want to save from the backend: Please make sure to set "is_system" flag in "customer_eav_attribute" table to 0, otherwise the attribute will not be saved.

It can be done by setting the attribute option to "system" => 0 in attribute parameters inside the install/upgrade script.

Don't forget to flush your cache after all!

direct link to solution

Edit You can create modules from here or here

13
  • hi @Ketan, yes I am setting 'system' => 0, in Upgrade script, please check I have posted full code over there, is any mistake Commented Feb 1, 2019 at 7:22
  • have you checked all tbles i asked for check? Commented Feb 1, 2019 at 7:28
  • yes Is_system flag is 0 in the table, I have checked it Commented Feb 1, 2019 at 7:31
  • 1
    can you find your attribute in customer_eav_attribute,eav_entity_attribute and customer_form_attribute table? Commented Feb 1, 2019 at 9:13
  • yes, @Ketan, It is there on all other tables except this one table, eav_entity_attribute Commented Feb 1, 2019 at 9:24

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.