Shopping cart

How to Get Value of Custom Attribute via Rest API Magento 2

How to Get Value of Custom Attribute via Rest API is also the useful tutorial so that the developers can add more custom field during running the own Magento 2 stores. This topic takes care of all essential steps you must follow to use Rest API within getting value of the custom attribute.

Overview of getting value of custom attribute via rest api

Step 1: Add new column and set value for the existing order

To get a custom attribute, the first thing is inserting a new column that is called as mageplaza_helloworld_custom_attribute in table sales_order and setting value for the existing orders.

Step 2: Add a specific new file

Because Magento 2 does not support for a new field in the response on the Rest API, so you must add a file \app\code\Mageplaza\HelloWorld\etc\extension_attributes.xml in the extension folder with 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="mageplaza_helloworld_custom_attribute" type="string" />

Step 3: Add an observe

To inject the custom attribute into extension_attributes.xml, it is compulsory to add an observe for event ``sales_order_load_after via \app\code\Mageplaza\HelloWorld\etc\events.xml` file.

<?xml version="1.0"?>
<config xmlns:xsi=""
  <event name="sales_order_load_after">
      <observer name="sales_order_load_helloworld_custom_attribute" instance="Mageplaza\HelloWorld\Observer\Sales\OrderLoadAfter" />

Step 4: Add a file to handle the event

Generate a file app\code\Mageplaza\HelloWorld\Observer\Sales\OrderLoadAfter.php that to mange the event sales_order_load_after in the previous step.

namespace Mageplaza\HelloWorld\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('helloworld_custom_attribute');
private function getOrderExtensionDependency()
    $orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(
    return $orderExtension;

Step 5: Clean folder to activate function

The folder var\generation need to be eliminate if you want to activate functionsetHelloWorldCustomAttribute() and getHelloWorldCustomAttribute in file var\generation\Magento\Sales\Api\Data\OrderExtension.php which is be auto-built.

  * @return string|null
public function getHelloWorldCustomAttribute()
     return $this->_get('helloworld_custom_attribute');

  * @param string $helloWorldCustomAttribute
  * @return $this
public function setHelloWorldAttribute($helloWorldCustomAttribute)
	 $this->setData ('helloworld_custom_attribute', $helloWorldCustomAttribute);
	 return $this;

Related Post

Enjoyed the tutorial? Spread it to your friends!


Brian Tran
Brian Tran
Senior Magento Developer who is passionate about development and love technology

Comments for How to Get Value of Custom Attribute via Rest API Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

You also may like these Magento 2 Extensions

One Step Checkout

120 reviews

Layered Navigation

61 reviews

Gift Card

8 reviews

Reward Points

8 reviews


16 reviews

Shop By Brand

19 reviews

People also searched for:

  • magento 2 how to get value of custom attribute via rest api