Get Custom Field In Database Via Api Magento 2

Today I will show you how to get custom field via API in Magento 2, It may be useful in some cases. You can do this through a few steps.

 

Step 1:

Create extension_attributes.xml under /app/code/Magepow/Custom/etc/

 

File: /app/code/Magepow/Custom/etc/extension_attributes.xml

<?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>

Step 2:

Create di.xml under /app/code/Magepow/Custom/etc/webapi_rest

 

File: /app/code/Magepow/Custom/etc/webapi_rest/di.xml

<?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="Magepow\Custom\Plugin\Api\OrderRepository" />
</type>
</config>

Step 3:

Create OrderRepository.php under /app/code/Magepow/Custom/Plugin/Api/

 

File: /app/code/Magepow/Custom/Plugin/Api/OrderRespository.php

<?php
namespace Magepow\Custom\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;
}
}

Step 4:

Now install the custom module and refresh Magento cache

 

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento cache:flush

 

Done. Hope this post helps you in some way.