How to get value of custom attribute on Magento 2 Rest API

Here are the following steps which help you how to get value of custom attribute on Magento 2 Rest API

Step1: Generate new column and set value for the existing order.
Step 2: Form a specific new file.
Step 3: Created an observe.
Step 4: Generate a file to handle the event.
Step 5: Remove folder to apply function.

In this article, I will explain you how to get value of custom attribute on Magento 2 Rest API

Generate new column and set value for the existing order

I have created the table sales_order in the latest column name, ‘tip and trick_attribute’ and the place value for access orders. This is a custom attribute I get in this post.

Form a specific new file

The following file \app\code\Webnexs\TipAndTrick\etc\extension_attributes.xml in my extension folder by the following content:

<?xml version="1.0"?>

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">

<extension_attributes for="Magento\Sales\Api\Data\OrderInterface">

 <attribute code="tip_and_trick_attribute" type="string" />



I have to set the file since the Magento 2 does not add a new field to the version of the Rest API

extension attributes.xml to add my custom attribute intoc.

Create an observe

I created an observe for event ‘sales_order_load_after’ through the file\app\code\Webnexs\TipsAndTricks\etc\events.xml to include my custom attribute in to extension_attributes.xml.

<?xml version="1.0"?>

<config xmlns:xsi=""


<event name="sales_order_load_after">

    <observer name="sales_order_load_tip_and_trick_attribute" instance="Webnexs\TipAndTrick\Observer\Sales\OrderLoadAfter" />



Generate a file to handle the event

I generated a file app\code\Webnexs\TipAndTricks\Observer\Sales\OrderLoadAfter.php to hold the event ‘sales_order_load_after’ which I states above.


namespace Webnexs\TipAndTrick\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('tip_and_trick_attribute');




private function getOrderExtensionDependency()


 $orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(



 return $orderExtension;



In execute function, I find my custom attribute from order data and set it into extension attributes of the orders.

Remove folder to apply function

You should delete var\generation folder to apply fuction ‘setTipAndTrickAttribute’ and ‘getTipandTrickAttribute’ in file ‘var\generation\Magento\sales\Api\Data\orderExtension.php’. This file will be generated automatically.

 Step 6

When I find order on Magento 2 Rest API, I see my custom attribute in the response

Last Update: April 5, 2018  

February 28, 2018   678   Nandini R    Magento2    
Total 1 Votes:

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *