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
Best Answer
Don't forget to flush your cache after all!
direct link to solution
Edit You can create modules from here or here