Retrieve custom fields on addresses in controller.

I’m working through the official Add a New Field in the Address Form tutorial.

That tutorial suggests using Extension Attributes to store the fields’ data, however it seems that Extension Attributes are not supported in the open source version of Magento. Thus, I’d like to read that data back in the controller and store it.

I’ve got a Preference configured for the ShippingInformationManagement class and it properly loads. However, the $addressInformation does not have the submitted fields, even though I do see them in the request. Here I’ve copied the request as curl from the Chrome dev tools, the submission has custom_field on the customAttributes element:

--data-raw '{"cartId":"kboHQydnW0jQFt1CYq7fpDVpwW3bzZqZ","billingAddress":{"countryId":"IL","region":"","street":["32432432432"],"company":"","telephone":"32423432432432","postcode":"22333","city":"wewerew","firstname":"New Customer","lastname":"Cust","customAttributes":[{"attribute_code":"custom_field","value":"FOOBAR"}],"saveInAddressBook":null},"paymentMethod":{"method":"checkmo","po_number":null,"additional_data":null},"email":"[email protected]"}'  

This code is getting the address:

public function saveAddressInformation($cartId, ShippingInformationInterface $addressInformation) : PaymentDetailsInterface { $address = $addressInformation->getShippingAddress(); } 

But the $address object has only this _data:

country_id = "IL" region = "" street = "32432432432" company = "" telephone = "32423432432432" postcode = "22333" city = "wewerew" firstname = "New Customer" lastname = "Cust" custom_attributes = [] 

Now that $address variable also has a customAttributeFactory property, however I’ve gone all through it with Xdebug and it does not seem do contain the custom_field data anywhere.

Based upon the “Access the value of the custom field on server side” section of the official tutorial, I’ve tried all sorts of ways to get the data out of the $address variable, and out of the AddressInterface and ShippingInformationInterface interfaces. (I’ve tried both, because the tutorial suggests one but implements the other, and the AddressInterface does not extend ShippingInformationInterface, neither does ShippingInformationInterface extend AddressInterface, however both extend MagentoFrameworkApiCustomAttributesDataInterface).

$address->getExtensionAttributes()->getCustomFields(); // Throws error $address->getCustomFields(); // Null $address->getCustomField(); // Null $ai = ObjectManager::getInstance()->get(MagentoQuoteApiDataAddressInterface::class)->getExtensionAttributes(); $cf_text = $ai->getCustomfield(); // Null $sii = ObjectManager::getInstance()->get(MagentoCheckoutApiDataShippingInformationInterface::class)->getExtensionAttributes(); $cf_code = $sii->getCustomfield(); // Error: Undefined method getCustomfield $bii = ObjectManager::getInstance()->get(MagentoCheckoutApiDataBillingInformationInterface::class)->getExtensionAttributes(); // Actually, this interface doesn't exist, but I tried! 

So how do I get the values of the custom fields?

submitted by /u/dotancohen
[link] [comments]