Magento – Property does not have corresponding setter in class Magento\Quote\Api\Data\AddressInterface while change checkout step

eavextension-attributesmagento2moduleshipping-address

1 – I add a eav attribute to customer_address

$attributesInfo = [
    'reference' => [
         'label' => 'Reference',
         'type' => 'varchar',
         'input' => 'text',
         'position' => 100,
         'visible' => true,
         'required' => false,
    ],
];

foreach ($attributesInfo as $attributeCode => $attributeParams) {
    $customerSetup->addAttribute('customer_address', $attributeCode, $attributeParams);
}

2 – I added the extension attribute in my module

<extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
    <attribute code="reference" type="string"/>
</extension_attributes>

In my requirejs-config.js I override some javascript file to add reference field

var config = {
"map": {
    "*": {
        "Magento_Checkout/js/model/shipping-save-processor/default" : "Agr_Checkout/js/shipping-save-processor-default-override",
        "Magento_Customer/js/model/customer/address" : "Agr_Checkout/js/model/customer/address",
        "Magento_Checkout/js/model/address-converter" : "Agr_Checkout/js/model/address-converter",
        "Magento_Checkout/js/model/new-customer-address" : "Agr_Checkout/js/model/new-customer-address"
    }
}

3 – I confirm that the reference field is sending in address

enter image description here

4 – When i send my shipping information (Click on Next) I'm getting this error: "Property "Reference" does not have corresponding setter in class "Magento\Quote\Api\Data\AddressInterface"."

enter image description here

I already do:
– Clean and flush magento cache
– Run setup:upgrade
– Run setup:di:compile

What I'm doing wrong?

Best Answer

Setting up attributes in an eav setup or upgrade script works best, and will automatically add to forms you request to add it to.

    class InstallData implements InstallDataInterface
    {
        private $_eavSetupFactory;
        private $_eavConfig;
        private $_attributeResource;
        protected $_logger;

        public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig, Attribute $attributeResource, Monolog $logger)
        {
            $this->_eavSetupFactory = $eavSetupFactory;
            $this->_eavConfig = $eavConfig;
            $this->_attributeResource = $attributeResource;
            $this->_logger = $logger;
        }

        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        {
            $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);
            $addressAttributes = [
        'attribute1' => [
            'type' => 'int',
            'label' => 'attribute1',
            'input' => 'text',
            'unique' => true,
            'required' => false,
            'visible' => true,
            'user_defined' => false,
            'position' => 100,
            'system' => false,
            'adminhtml_only' => 0
        ],
        'attribute2' => [
            'type' => 'int',
            'label' => 'attribute2',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => false,
            'position' => 110,
            'system' => false,
            'adminhtml_only' => 1
        ]
    ];

    $usedInFormsAddress = [
            'adminhtml_customer_address',
            'customer_address_edit',
            'customer_register_address'
        ];

    foreach ($addressAttributes as $code => $options) {
        $eavSetup->addAttribute(
            'customer_address',
            $code,
            $options
        );

        try {
            $attribute = $this->_eavConfig->getAttribute('customer_address', $code);
            $attribute->setData(
                'used_in_forms',
                $usedInFormsAddress
            );
            $this->_attributeResource->save($attribute);
        } catch (LocalizedException $exception) {
            $this->_logger->critical($exception->getMessage());
        } catch (Exception $exception) {
            $this->_logger->critical($exception->getMessage());
        }
    }

enter image description here

This code here will add to the forms and there won't be any issue saving or going to next step

$usedInFormsAddress = [
            'adminhtml_customer_address',
            'customer_address_edit',
            'customer_register_address'
        ];

try {
            $attribute = $this->_eavConfig->getAttribute('customer_address', $code);
            $attribute->setData(
                'used_in_forms',
                $usedInFormsAddress
            );
            $this->_attributeResource->save($attribute);
        } catch (LocalizedException $exception) {
            $this->_logger->critical($exception->getMessage());
        } catch (Exception $exception) {
            $this->_logger->critical($exception->getMessage());
        }
Related Topic