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

The below mentioned steps assist you how to get value of custom attribute on Magento 2 Rest API

Step1: Create latest column and locate value for the offered order.
Step 2: Form a specific new file.
Step 3: Created an observe.
Step 4: create a file to handle the event.
Step 5: Remove folder to apply function.

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

Create latest column and locate value for the offered order

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

Form a specific new file

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

<?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="tip_and_trick_attribute" type="string" />

</extension_attributes>

</config>

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

Extension attributes.xml to include my custom attributes 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="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

<event name="sales_order_load_after">

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

</event>

</config>

Create a file to handle the event

I created a file app\code\Webnexs\TipAndTricks\Observer\Sales\OrderLoadAfter.php to hold the event ‘sales_order_load_after’ which I mentioned already.

<?php

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');


$extensionAttributes->setTipAndTrickAttribute($attr);


 $order->setExtensionAttributes($extensionAttributes);

}


private function getOrderExtensionDependency()

{

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

 '\Magento\Sales\Api\Data\OrderExtension'

 );

 return $orderExtension;

}

}

I get my custom attribute from order data and set it into extension attributes of the orders in execute function,

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 creates automatically.

 Step 6

When I get order on Magento 2 Rest API, I observe my custom attribute in the response

Last Update: September 30, 2019  

February 28, 2018   1483   Nandini Ramachandran    Magento2    
Total 1 Votes:
0

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 *

Facebook
Twitter
INSTAGRAM
LinkedIn