0

I am calling this endpoint:

http://localhost/index.php/rest/V1/orders/<myId>

And I want to have 2 customer address attributes inside the response (-> billing_address). In order to get there I created extension_attributes.xml and added this content:

<?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\Customer\Api\Data\AddressInterface">
        <attribute code="my_attribute_1" type="string" />
        <attribute code="my_attribute_2" type="string" />
    </extension_attributes>
</config>

However, I do not see the response contain the respective Key-Value data.

To visualize, I want to have the attributes here: In this node I want to see my custom attributes that are mapped onto the customers address

1 Answer 1

0

Okay, after stepping trough some other places, I've eventually identified how I can add the attributes to a additional key called extension_attributes inside billing_address. Not exactly what I was looking for, but a good start.

My extension_attributes.xml looks like this now:

<?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\OrderAddressExtensionInterface">
        <attribute code="my_attr_1" type="string" />
        <attribute code="my_attr_2" type="string" />
    </extension_attributes>
</config>

Also, I've created in my module etc/webapi_rest/di.xml with this content:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="\Magento\Sales\Model\OrderRepository">
        <plugin name="order_repository" type="SEEC\RestApi\Plugin\Magento\Sales\Model\OrderRepository" disabled="false" sortOrder="200" />
    </type>
</config>

And created this file to attach the data properly:

<?php

namespace SEEC\RestApi\Plugin\Magento\Sales\Model;

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\OrderRepository as ModelOrderRepository;

class OrderRepository
{

    /**
     * @param ModelOrderRepository $orderRepository
     * @param OrderInterface $order
     * @return OrderInterface
     */
    public function afterGet(
        ModelOrderRepository $orderRepository,
        OrderInterface $order
    ) {
        $billingAddress = $order->getBillingAddress();
        $billingAddressExtensionAttributes = (null !== $billingAddress->getExtensionAttributes())
            ? $billingAddress->getExtensionAttributes()
            : '';

        $billingAddressExtensionAttributes->setMyAttr1($billingAddress->getMyAttr1());
        $billingAddressExtensionAttributes->setMyAttr2($billingAddress->getMyAttr2());

        $billingAddress->setExtensionAttributes($billingAddressExtensionAttributes);

        if (!$order->getIsVirtual()) {
            $shippingAddress = $order->getShippingAddress();
            $shippingAddressExtensionAttributes = (null!== $shippingAddress->getExtensionAttributes())
                ? $shippingAddress->getExtensionAttributes()
                : '';

            $shippingAddressExtensionAttributes->setMyAttr1($shippingAddress->getMyAttr1());
            $shippingAddressExtensionAttributes->setMyAttr2($shippingAddress->getMyAttr2());
            $shippingAddress->setExtensionAttributes($shippingAddressExtensionAttributes);
        }

        return $order;
    }
}

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.