2

The reason is in the documentation itself: This call returns an array of objects, but detailed information about each object’s attributes might not be included.

What I'm looking for is something to force the inclusion of this custom attribute into the extension attributes response object.

This is how I created the custom attribute:

1) Added the attribute in the database with the standard way in /Setup/UpgradeData.

2) etc/extension_attributes.xml

<extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
    <attribute code="myCustomAttr" type="string" />
</extension_attributes>

3) Two observer events: sales_order_save_before and sales_order_load_after

One of the observers:

<?php
namespace Vendor\Module\Observer\Sales;

use Magento\Framework\Event\ObserverInterface;

class OrderLoadAfter implements ObserverInterface
{
  public function execute(\Magento\Framework\Event\Observer $observer)
  {
      $order = $observer->getOrder();
      $extensionAttributes = $order->getExtensionAttributes();

      if ($extensionAttributes === null) {
          $extensionAttributes = $this->getOrderExtensionDependency();
      }

      $attr = $order->getData('myCustomAttr');
      $extensionAttributes->setData('myCustomAttr', $attr);
      $order->setExtensionAttributes($extensionAttributes);
  }

  private function getOrderExtensionDependency()
  {
      $orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(
          '\Magento\Sales\Api\Data\OrderExtension'
      );
      return $orderExtension;
  }
}

This works, the custom attribute is correctly loaded into the extension attributes of the GET /order/{id} REST API and saved with the PUT.

The GetOrdersList service (/orders?searchCriteria=etc.) is the only one which does not return the attribute. Any idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.