Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

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

Vinh Jacker | 12-18-2024

Get Value of Custom Attribute via Rest API

This 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.

Let’s dive in the detailed instruction now.

How to get value of custom attribute via rest api in few steps:

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="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="mageplaza_helloworld_custom_attribute" type="string" />
  </extension_attributes>
</config>

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="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_helloworld_custom_attribute" instance="Mageplaza\HelloWorld\Observer\Sales\OrderLoadAfter" />
  </event>
</config>

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.

<?php
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');
 
    $extensionAttributes->setHelloWorldCustomAttribute($attr);
 
    $order->setExtensionAttributes($extensionAttributes);
 
}
 
   
 
private function getOrderExtensionDependency()
 
{
 
    $orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(
        '\Magento\Sales\Api\Data\OrderExtension'
    );
 
    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;
}

Wrap up

If you need to get value of the custom attribute on Magento 2 REST API, this tutorial will help you do that without diffuculty. Don’t forget to let me know if you have any questions. If you sucessfully follow this guide, share this it with others to help them out.

Related Post

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services