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]=e...
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
You can use the below steps for the expected result:
Create extension_attributes.xml in vendor/module/etc and use the below code:
<?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>
Then create di.xml file at vendor/module/etc and use the below code:
<?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>
Create an OrderRepositoryPlugin.php file at vendor/module/Plugin and add the below code:
<?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; } }
Do not forget to run the below commands:
php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento cache:flush