How to expose a custom field from sales_order table to rest api?
- Digital Engineering
- Ecommerce
- General
How to expose a custom field from sales_order table to rest api?
Suppose you have custom field delivery_type
in sales_order
table. You are accessing order using rest Api but this field is not present in response.
Requested Api: http://localhost/default/rest/default/V1/orders/{order_id}
You can get it working for individual order. Though if you want to show this attribute in order collection as well you have to modify getList
method also.
You have to create custom module.
If you follow link to create new custom module.
etc/extension_attributes.xml
1 2 3 4 5 6 |
<?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="delivery_type" type="string" /> </extension_attributes> </config> |
etc/webapi_rest/di.xml
1 2 3 4 5 6 |
<?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="orderInformationUpdate" type="Vendor\Module\Plugin\Api\OrderRepository" /> </type> </config> |
Plugin/Api/OrderRepository.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?php namespace Vendor\Module\Plugin\api; use Magento\Sales\Api\Data\OrderExtensionFactory; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\Data\OrderSearchResultInterface; use Magento\Sales\Api\OrderRepositoryInterface; class OrderRepository { const DELIVERY_TYPE = 'delivery_type'; /** * Order Extension Attributes Factory * * @var OrderExtensionFactory */ protected $extensionFactory; /** * OrderRepositoryPlugin constructor * * @param OrderExtensionFactory $extensionFactory */ public function __construct(OrderExtensionFactory $extensionFactory) { $this->extensionFactory = $extensionFactory; } /** * Add "delivery_type" 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) { $deliveryType = $order->getData(self::DELIVERY_TYPE); $extensionAttributes = $order->getExtensionAttributes(); $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create(); $extensionAttributes->setDeliveryType($deliveryType); $order->setExtensionAttributes($extensionAttributes); return $order; } /** * Add "delivery_type" 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) { $deliveryType = $order->getData(self::DELIVERY_TYPE); $extensionAttributes = $order->getExtensionAttributes(); $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create(); $extensionAttributes->setDeliveryType($deliveryType); $order->setExtensionAttributes($extensionAttributes); } return $searchResult; } } |
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s