1

I've added a custom field to the Customer Addresses with the following UpgradeData:

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    /** @var CustomerSetup $customerSetup */
    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $params = [
        'label' => 'House Number',
        'type' => 'varchar',
        'input' => 'text',
        'required' => true,
        'visible' => true,
        'user_defined' => true,
        'position' =>999,
        'system' => 0,
    ];

    $customerSetup->addAttribute('customer_address', 'house_number', $params);
    $customAttribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'house_number');
    $customAttribute->setData(
        'used_in_forms',
        ['adminhtml_checkout', 'adminhtml_customer', 'adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'],
    );
    $customAttribute->save();
}

The field properly displays when logging into the Adminhtml area and adding a new Addresses to a customer. However, the New Address form cannot be submitted, the following error appears:

"House Number" is a required value.

Here is a screenshot:

"House Number" is a required value.

I do believe that it is clear that a value for House Number was submitted.

What else must I do to enable this field on this form, and on other forms such as the Checkout and user's own Account Information?

1 Answer 1

2

When adding a custom customer attribute or custom customer address attribute, to allow Magento to save it you need to assign it to the customer attribute set.

So, in your code, after $customerSetup->addAttribute('customer_address', 'house_number', $params); you should add the following code:

$customerSetup->addAttributeToSet(
    AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
    AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
    null,
    'house_number'
);

where AddressMetadataInterface is imported by
use Magento\Customer\Api\AddressMetadataInterface;

But... since Magento 2.3, Magento marked upgrade script as old script, you should use Data patch for data modification.

The below is the data patch to add house_number custom customer address attribute:
File path: app/code/Vendor/Module/Setup/Patch/Data/AddHouseNumberAttribute.php

<?php

namespace Vendor\Module\Setup\Patch\Data;

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AddHouseNumberAttribute implements DataPatchInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * @inheritdoc
     */
    public function apply(): void
    {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $houseNumberAttributeCode = 'house_number';

        $customerSetup->addAttribute(
            AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
            $houseNumberAttributeCode, [
            'label' => 'House Number',
            'type' => 'varchar',
            'input' => 'text',
            'required' => true,
            'visible' => true,
            'user_defined' => true,
            'position' => 999,
            'system' => false,
        ]);

        $customerSetup->addAttributeToSet(
            AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
            AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
            null,
            $houseNumberAttributeCode
        );

        $houseNumberAttribute = $customerSetup->getEavConfig()->getAttribute(
            AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
            $houseNumberAttributeCode
        );
        $houseNumberAttribute->addData([
            'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
        ]);
        $houseNumberAttribute->save();
    }

    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }
}

7
  • Thank you Tu Van! I added the $customerSetup->addAttributeToSet() line, and made the other changes that you suggest such as the use of AddressMetadataInterface instead of the strings which they represent. However, the field still is not saved, and throws the error. Any other ideas?
    – dotancohen
    Commented Dec 28, 2022 at 14:22
  • @dotancohen As you already add house_number custom customer address attribute into your database, you have to remove it from database, if you still use UpgradeData script, you have to increase setup_version in module.xml and run bin/magento setup:upgrade command. If you choose use Data Patch, you don't need to do anything with setup_version in module.xml. Let me know if you need help with that, I'll tell you how.
    – Tu Van
    Commented Dec 28, 2022 at 15:09
  • Yes, of course, I removed it with $customerSetup->removeAttribute(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, 'house_number') first.
    – dotancohen
    Commented Dec 28, 2022 at 15:11
  • Yeah, removeAttribute function should remove your attribute. Run SELECT * FROM eav_attribute` WHERE attribute_code = 'house_number'` MySql command to check if house_number attribute was removed or not. If it does not show any records, that means the attribute was removed. Make your changes in UpgradeData or create a new Data Patch then run bin/magento setup:upgrade. P/S: I've tested the features after applying the code in my answer before sharing it with you.
    – Tu Van
    Commented Dec 28, 2022 at 15:24
  • 1
    Great, thank you! I am learning a lot from you.
    – dotancohen
    Commented Dec 28, 2022 at 17:13

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.