19

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?

  • Did the answer below work? – Stevie G Jan 24 '17 at 13:38
  • I solved by hard sql insertion, executing a script for update de reference with the address_id, I know is wrong but I was a little rushed, i will test and give you a feedback later. – allamgr Jan 24 '17 at 17:56
  • I don't think you can be blamed... apparently you can only add custom_attributes in Enterprise and so far I have found no "easily customisable checkout" way around this. – LM_Fielding Jun 30 '17 at 11:31
  • any update regarding? – Magento2 Devloper Mar 7 '18 at 9:02
  • @allamgr i am also facing same issue with the new customer address attribute did u get solution for this? can u share me your thoughts on this. prnt.sc/iovkp2 – Nagaraju K Mar 9 '18 at 12:30
1

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());
        }
0

Try setting it via custom attributes.

Example:

...
 custom_attribute: [{"attribute_code": "reference", "value": "Your value"}]
...
  • Did you ever get this to work or is it only an experiment? – LM_Fielding Jun 30 '17 at 11:31
  • I got it working – Phoenix128_RiccardoT Jun 30 '17 at 11:32
  • With community edition? I would greatly appreciate it, even with bounty if you could demonstrate how. – LM_Fielding Jun 30 '17 at 12:25
  • 1
    It was with Magento2 Enterprise Edition on a custom detached frontend checkout procedure. I have to search that code. It is an old work. – Phoenix128_RiccardoT Jun 30 '17 at 12:31
  • Please show me if you find it, but I don't think it's available. – LM_Fielding Jun 30 '17 at 12:39
0

how do you pass the attribute in the request? you can check browser console like that

{
    ...
    extension_attributes: {
         reference: "value"
    }
}

It's correct. You can remove var folder and generated folder var/cache var/page_cache var/view_proceed and generated/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.