2

I have added an extension attribute by creating extension_attributes.xml file in etc. Its content is:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
   <extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
      <attribute code="custom_field" type="string" />
   </extension_attributes>
</config>

Now when I try to get extension attribute in model I use following code:

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $address = $objectManager->get('\Magento\Quote\Api\Data\AddressInterface');
 echo $address->getExtensionAttributes()->getCustomField();

But the problem is $address->getExtensionAttributes() return NULL. What am I doing wrong here?

1 Answer 1

0

As also written here (different question but same answer):

The documentation for extension attributes is not quite clear on what's the right way to use them and it seems like each entity type does it in a slightly different way.

That being said, from my experience you have to create plugins to populate the extension attributes, and also to save them. It is not done automatically.

Take a look at Magento\GiftMessage\Model\Plugin\OrderGet and Magento\GiftMessage\Model\Plugin\OrderSave for an example.

2
  • Thank you. I will have a look at it and then see what happens.
    – mag777
    Commented Apr 27, 2017 at 11:22
  • any update on this? Commented Oct 18, 2019 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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