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?

dotancohen
  • 1,115
  • 6
  • 18

1 Answers1

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-&gt;moduleDataSetup = $moduleDataSetup;
    $this-&gt;customerSetupFactory = $customerSetupFactory;
}

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

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

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

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

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

public function getAliases()
{
    return [];
}

}

Tu Van
  • 6,868
  • 2
  • 11
  • 22
  • 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 Dec 28 '22 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 Dec 28 '22 at 15:09
  • Yes, of course, I removed it with $customerSetup->removeAttribute(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, 'house_number') first. – dotancohen Dec 28 '22 at 15:11
  • Yeah, removeAttribute function should remove your attribute. Run SELECT * FROMeav_attributeWHEREattribute_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 Dec 28 '22 at 15:24
  • Tu Van, thank you, it worked! I made a mistake with removing the element, correcting that mistake resolved the issue! Answer accepted. If you know how to have the fields also appear on the Customer's Account Information page and Checkout page, I would appreciate it. – dotancohen Dec 28 '22 at 15:58
  • Glad to help. I'll try to help you with showing the custom customer attribute in Customer Account Information on frontend, then you can investigate to make your attribute show on the checkout page. https://magento.stackexchange.com/questions/363176/custom-attribute-on-customer-not-showing-data-in-customer-account-information – Tu Van Dec 28 '22 at 16:44
  • 1
    Great, thank you! I am learning a lot from you. – dotancohen Dec 28 '22 at 17:13