1

I am trying to use extension_attributes in order to add a field in the orders. The thing is, I want that field to appear when we get the orders with API calls (http://monsite.fr/soap/default?services=salesOrderRepositoryV1) and I can't get this to work.

For now I altered the tables sales_order and sales_order_grid with the Setup of my module (InstallSchema) to add a field "file_name" and declared extension_attributes.xml in etc :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
        <attribute code="file_name" type="string"/>
    </extension_attributes>
</config>

I've got the generated class in vendor with the methods getFileName and setFileName, but that's it, when I try the url http://monsite.fr/soap/default?services=salesOrderRepositoryV1 I still have the ordinary attributes (entityId, createdAt, incrementId, etc.), there is not a fileName attribute (not even in extension_attributes node, of course).

Is there someone who knows how to get an extension attribute to appear with API ?

EDIT

Okay, now I get the field "fileName" in the <extensionAttributes> of the SOAP response, but it doesn't retrieve the values in database... I added a plugin in di.xml :

<type name="Magento\Sales\Api\OrderRepositoryInterface"> <plugin name="get_file_name" type="Company\Module\Model\Plugin\FileGet"/>

and I implemented a function afterGet() where I have access to the extension attributes ($orderInterface->getExtensionAttributes()) and call the generated methods getFileName() and setFileName(), but getFileName() return null...

One more thing, I also tried with an Observer on the event <event name="sales_order_load_after">, and here I can call getData('file_name') to have database values, but it is done without passing by extension attributes, I'm a little bit confuse, here.

To summ it up :

-with the plugin on OrderRepositoryInterface and the function afterGet(), I have acces to the extension_attributes of my order, and can call getFileName() and setFileName() methods (generated in OrderExtensionInterface), but doing so doesn't seem to affect the database;

-with the Observer : on the event sales_order_load_after, I can use getdata('file_name') as if file_name were a basic attribute and not an extension_atributes, and it does retrieve the value in database, but then it is not present in the SOAP response.

I don't know if I am clear enough, feel free to ask more questions, or sample code if needed.

EDIT

It appears that even if we don't have auto-implementation for it, it is possible to call getData('file_name') through the $resultOrder (class OrderInterface) of the afterGet method of the plugin. Now I have to do a similar work in order to set the value with a SOAP request.

I hope that this monologue of mine can help someone^^". feel free to post a question if you also have some trouble with extension_attributes, I'll help if I can.

3
  • That definitely helps, did you get any further? Commented Aug 4, 2017 at 13:56
  • Hi there, thanks for the info! Did you ever get to the point where you have a complete get and set for an extension_attribute via REST API? I managed to configure the get, but can't manage to set a new value to it. I don't get errors, but the new value isn't stored in the DB. Is that a limitation?
    – justabuzz
    Commented Jan 18, 2019 at 8:33
  • @justabuzz can you please share how you managed to configure the get method with new extension attributes? Commented Jan 8, 2021 at 13:15

0

Your Answer

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