In order rest api http://127.0.0.1/mag/index.php/rest/V1/orders/1 it shows custom attributes
 "payment_additional_info": [
 {
 "key": "method_title",
 "value": "Check / Money order"
 }
 ],
 "applied_taxes": [],
 "item_applied_taxes": [],
 "preorder": "1"
while in order search rest api http://127.0.0.1/mag/index.php/rest/V1/orders/?searchCriteria[filter_groups][0][filters][0][field]=entity_id&searchCriteria[filter_groups][0][filters][0][value]=1
it did not shows custom attribute
"payment_additional_info": [
 {
 "key": "method_title",
 "value": "Check / Money order"
 }
 ],
 "applied_taxes": [],
 "item_applied_taxes": []
//did not show preorder which is custom attribute 
1 Answer 1
Make extension_attributes.xml in vendor/module/etc add this in your extension_attributes.xml file
<?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="your attribute code" type="string" />
</extension_attributes>
Make di.xml in vendor/module/etc add this in your di.xml file
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Sales\Api\OrderRepositoryInterface">
 <plugin name="plugin name"
 type="vendor\module\Plugin\yourplugin" />
</type>
</config>
Make OrderRepositoryPlugin.php in vendor/module/Plugin add this in your file
<?php
namespace vendour\module\Plugin;
use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderExtensionInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
/**
 * Class OrderRepositoryPlugin
 */
class OrderRepositoryPlugin
{
 /**
 * Order feedback field name
 */
 const FIELD_NAME = 'your_attribute_code';
 /**
 * Order Extension Attributes Factory
 *
 * @var OrderExtensionFactory
 */
 protected $extensionFactory;
 /**
 * OrderRepositoryPlugin constructor
 *
 * @param OrderExtensionFactory $extensionFactory
 */
 public function __construct(OrderExtensionFactory $extensionFactory)
 {
 $this->extensionFactory = $extensionFactory;
 }
 /**
 * Add "customer_feedback" extension attribute to order data object to make it accessible in API data
 *
 * @param OrderRepositoryInterface $subject
 * @param OrderInterface $order
 *
 * @return OrderInterface
 */
 public function afterGet(OrderRepositoryInterface $subject, OrderInterface $order)
 {
 $attr = $order->getData(self::FIELD_NAME);
 $extensionAttributes = $order->getExtensionAttributes();
 $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
 $extensionAttributes->setYourAttributeCode($attr);
 $order->setExtensionAttributes($extensionAttributes);
 return $order;
 }
 /**
 * Add "customer_feedback" extension attribute to order data object to make it accessible in API data
 *
 * @param OrderRepositoryInterface $subject
 * @param OrderSearchResultInterface $searchResult
 *
 * @return OrderSearchResultInterface
 */
 public function afterGetList(OrderRepositoryInterface $subject, OrderSearchResultInterface $searchResult)
 {
 $orders = $searchResult->getItems();
 foreach ($orders as &$order) {
 $attr = $order->getData(self::FIELD_NAME);
 $extensionAttributes = $order->getExtensionAttributes();
 $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
 $extensionAttributes->setYourAttributeCode($attr);
 $order->setExtensionAttributes($extensionAttributes);
 }
 return $searchResult;
 }
}
run in command line:
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush
Explore related questions
See similar questions with these tags.