Skip to content

How to store the data of a “used_in_forms” attribute in the Checkout?

I’ve added an attribute to the Customer Addresses:

<?php
namespace DcCustomerSetup;

use MagentoCustomerApiAddressMetadataInterface;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupUpgradeDataInterface;
use MagentoFrameworkSetupModuleContextInterface;

class UpgradeData implements UpgradeDataInterface {

    protected $customerSetupFactory;
    protected $attributeSetFactory;

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

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if ( !version_compare($context->getVersion(), '0.0.2', "<") ) {
            return;
        }

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $attribute = [
            'code' => 'emma_number',
            'label' => 'Emma Number',
        ];

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

        $attData = [
            'used_in_forms' => ['adminhtml_checkout', 'adminhtml_customer', 'adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'],
        ];

        $customerSetup->addAttribute(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attribute['code'], $params);

        $customerSetup->addAttributeToSet(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS, null, $attribute['code']);

        $customAttribute = $customerSetup->getEavConfig()->getAttribute(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attribute['code']);
        $customAttribute->addData($attData);
        $customAttribute->save();

    }

}

I’ve tested storing this field in the five places that I see it used, which may or may not properly conform to the values used in the used_in_forms element:

  1. Guest Checkout: Not stored
  2. Logged-in User Checkout: Not stored
  3. Customer Account Dashboard -> Address Book -> Add new address: Field doesn’t even show
  4. Customer Account Dashboard -> Address Book -> Edit address: Field doesn’t even show
  5. Admin Dashboard -> Customers -> Edit: Properly stored in database!
  6. Admin Dashboard -> Sales -> Edit: Not stored

Since the field is in fact properly stored in the database when editing an extant address from the Admin dashboard’s Customer tab, I see that using the used_in_fields feature does provide backend storage. So why does the Checkout not save this field?

Additionally, what value do I need to add for the Customer Account Dashboard to display and save this field?