Magento 2 - Extend API result with custom attribute

1k views Asked by At

i have a question about extending an API request from magento 2. I have the following request: /index.php/rest/V1/orders/123 which gives me a JSON with all the information about the order. This works fine. Now I want to extend the given json response with a custom attribute. I would get the required attribute through a few relations starting from the order table customer_id (customers table) -> group_id (customer_groups table) -> my_attribute.

I read in magentos offical documentation about attribute extensions and also here. But I faced two problems. First of all I cant find anything about multiple relations like I have. In magentos documentation they use join

<extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
    <attribute code="stock_item" type="Magento\CatalogInventory\Api\Data\StockItemInterface">
        <join reference_table="cataloginventory_stock_item" reference_field="product_id" join_on_field="entity_id">
            <field>qty</field>
        </join>
    </attribute>
</extension_attributes>

Does this also work with multiple joins. It would be great if I could write PHP to get the attribute. And secondly how exactly can I extend the API so that I get the information in my API request. Cause for example in the the tutorials they only use it in PHP to get the information:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$addressInformation = $objectManager->create('Magento\Checkout\Api\Data\ShippingInformationInterface');
$extAttributes = $addressInformation->getExtensionAttributes();

$selectedShipping = $extAttributes->getCustomShippingCharge(); //get custom attribute data.

Does someone has a similiar situation or have a code example where I can start?

0

There are 0 answers