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;
}
}