Skip to content

Magento 2 Custom Address Select Attribute Place Order Issue in Checkout

I added a custom address attribute using the EavSetupFactory.

/**
 * Class AddressAttribute
 */
class AddAddressClassificationAttribute implements DataPatchInterface
{
    const ATTRIBUTE_CODE = 'address_classification';

    /**
     * @param Config $eavConfig
     * @param EavSetupFactory $eavSetupFactory
     * @param AttributeResourceModel $attributeResourceModel
     */
    public function __construct(
        private Config $eavConfig,
        private EavSetupFactory $eavSetupFactory,
        private AttributeResourceModel $attributeResourceModel
    ) {
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies(): array
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases(): array
    {
        return [];
    }

    /**
     * @return void
     * @throws LocalizedException
     * @throws Zend_Validate_Exception
     * @throws Exception
     */
    public function apply(): void
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create();

        $eavSetup->addAttribute(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, self::ATTRIBUTE_CODE, [
            'type' => 'int',
            'input' => 'select',
            'label' => 'Address Type',
            'visible' => true,
            'source' => AddressClassification::class,
            'required' => true,
            'user_defined' => true,
            'system' => false,
            'group' => 'General',
            'global' => true,
            'visible_on_front' => true,
        ]);

        $customAttribute = $this->eavConfig->getAttribute(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, self::ATTRIBUTE_CODE);

        $customAttribute->setData(
            'used_in_forms',
            [
                'adminhtml_customer_address',
                'customer_address_edit',
                'customer_register_address'
            ]
        );

        $this->attributeResourceModel->save($customAttribute);
    }
}

with a custom source that extends AbstractSource instead of MagentoEavModelEntityAttributeSourceTable

class AddressClassification extends AbstractSource
{
    /**
     * @return array[]
     */
    public function getAllOptions(): array
    {
        if ($this->_options === null) {
            $this->_options = [
                ['label' => __('Select Address Type'), 'value' => ''],
                ['label' => __('Residential'), 'value' => 1],
                ['label' => __('Business'), 'value' => 2]
            ];
        }

        return $this->_options;
    }
}

Saving the custom attribute in address is working as expected but when I am creating a new address on checkout, the field is getting flagged as empty even though it is filled.

enter image description here

I assume this is because of the custom source. Do you guys have any idea what causes this or how to fix this?