{"id":759,"date":"2020-01-13T11:19:18","date_gmt":"2020-01-13T11:19:18","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/01\/13\/get-value-of-custom-attribute-on-magento-2-rest-api\/"},"modified":"2025-05-21T17:56:01","modified_gmt":"2025-05-21T12:26:01","slug":"get-value-of-custom-attribute-on-magento-2-rest-api","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-value-of-custom-attribute-on-magento-2-rest-api\/","title":{"rendered":"How to Get Value of Custom Attribute on Magento 2 Rest API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 REST API is the best way of developing the cross-platform app to communicate with your Magento 2&nbsp;stores. While developing an app, you would require to get value of custom attribute on Magento 2 REST API and use this further in the app. Today, I have come up with the complete guide&nbsp;to <em><strong>get value of custom attribute on Magento 2 REST API<\/strong><\/em>. Additionally, in <a href=\"https:\/\/meetanshi.com\/blog\/get-customer-data-from-attribute-value-in-magento-2\/\">Magento 2 get customer data from attribute value<\/a> it becomes a crucial aspect in this context.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to&nbsp;Get Value of Custom Attribute on Magento 2 Rest API:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create\/Add a new column in \u2018<strong>sales_order<\/strong>\u2018 table and set value for the existing order.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new file at app\\code\\Vendor\\Module\\etc\\extension_attributes.xml and put the below code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n    &lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Api\/etc\/extension_attributes.xsd\">\n        &lt;extension_attributes for=\"Magento\\Sales\\Api\\Data\\OrderInterface\">\n            &lt;attribute code=\"custom_attribute\" type=\"string\" \/>\n        &lt;\/extension_attributes>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>event.xml<\/strong>&nbsp;at&nbsp;<strong><strong>app\\code\\Vendor\\Module\\etc\\events.xml<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n    &lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"   xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\">\n        &lt;event name=\"sales_order_load_after\">\n            &lt;observer name=\"sales_order_load_custom_attribute\" instance=\"Vendor\\Module\\Observer\\Sales\\OrderLoadAfter\" \/>\n        &lt;\/event>\n    &lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>OrderLoadAfter.php<\/strong>&nbsp;at&nbsp;<strong><strong>app\\code\\Vendor\\Module\\Observer\\Sales\\OrderLoadAfter.php<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Module\\Observer\\Sales;\nuse Magento\\Framework\\Event\\ObserverInterface;\nclass OrderLoadAfter implements ObserverInterface\n{\n    public function execute(\\Magento\\Framework\\Event\\Observer $observer)\n    {\n        $order = $observer->getOrder();\n        $extensionAttributes = $order->getExtensionAttributes();\n        if ($extensionAttributes === null) {\n            $extensionAttributes = $this->getOrderExtensionDependency();\n        }\n        $attr = $order->getData('custom_attribute');\n        $extensionAttributes->setCustomAttribute($attr);\n        $order->setExtensionAttributes($extensionAttributes);\n    }\n    private function getOrderExtensionDependency()\n    {\n        $orderExtension = \\Magento\\Framework\\App\\ObjectManager::getInstance()->get(\n            '\\Magento\\Sales\\Api\\Data\\OrderExtension'\n        );\n        return $orderExtension;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After getting an order on Magento 2 Rest API, you can see the custom attribute in the response.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s all about getting value of custom attribute on Magento 2 REST API. Let me know which custom attribute value did you get using the code above and how you used the value in your app. Also you can <a href=\"https:\/\/meetanshi.com\/blog\/add-category-attribute-to-custom-attribute-group-in-magento-2\/\">Magento 2 add category attribute to custom attribute group<\/a> integrating a third-party API and using a category attribute!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 REST API is the best way of developing the cross-platform app to communicate with your Magento 2&nbsp;stores. While developing an app, you would&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-759","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=759"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/759\/revisions"}],"predecessor-version":[{"id":14004,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/759\/revisions\/14004"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}