The below-mentioned steps assist you how to get the value of the custom attribute on Magento 2 Rest API,
Step 1: Create the latest column and locate the value for the offered order.
Step 2: Form a specific new file.
Step 3: Created the observer.
Step 4: create a file to handle the event.
Step 5: Remove folder to apply function.
Get build your Magento multi-vendor store with all stunning features! Consult today!
Get your free live demo now: Click here
In this blog, I will explain to you how to get the value of the custom attribute on Magento 2 Rest,
Get the value of the custom attribute on Magento 2 Rest API
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 Magento 2 does not include a new field in the version of the Rest API.
Extension attributes.xml to include my custom attributes into c.
Create an observer in Magento 2 To Load Custom Attribute
I created an observer for event ‘sales_order_load_after’ through the file\app\code\Webnexs\TipsAndTricks\etc\events.xml to include my custom attribute in 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 On Magento 2 – Rest API
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 executing the function,
Magento 2 rest API – Remove folder to apply function
You should delete var\generation folder to apply function ‘setTipAndTrickAttribute’ and ‘getTipandTrickAttribute’ in file ‘var\generation\Magento\sales\Api\Data\orderExtension.php’. This file will be created automatically.
Step 6
When I get the order on Magento 2 Rest, I observe my custom attribute in the response,
Wrapping up
Curious about the Magento 2 platform? Then get to build one robust online store today, click here!!!
Leave a Reply