Extension Attribute Magento 2

Extension Attribute Magento 2

Hello Friends,

In this blog I am going to cover Extension Attribute Magento 2, I believe all of you are using magento rest api while working in magento so sometimes you require additional fields to add in api So how u can add it easily without making any changes in data interface of existing api, I am going to explain completely about it. So please read this blog till end so you don’t miss any important points.

What is Extension Attribute Magento 2?

Extension attribute is a piece of code which helps you to extend your data in response , For example, There is product api in which by default only name and price are being sent as response in api , now you want to add description as well in response, Now you have 2 option either you require to make changes in existing data interface and apply the changes which is not a good idea and consume time as well. other way is using extension attributes which makes your life easy so you don’t require to make changes in existing data interface.

For example you want to add additional data in product detail api and want to send additional custom data.

Steps to add extension attribute in response.

  • First of all you need to create Custom Module , If you don’t know how to create custom module , please visit my blog on how to create custom module.
  • Now create a plugin , If you don’t know how to create Plugin in Magneto , Visit my previous blog for how to create Plugin
  • Add code as below in di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Api\ProductRepositoryInterface">
        <plugin name="mageacademy_productextension" type="Mageacademy\ExtensionAttributeExample\Plugin\ProductRepositoryPlugin"  sortOrder="1" />
    </type>
</config>
  • Now create extension_attributes.xml and add below code
<?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\Catalog\Api\Data\ProductInterface">
        <attribute code="custom_data" type="string" />
    </extension_attributes>
</config>

Here you can see for attribute which identify which interface you are pointing to so u can add extension attribute in that object

then you can see attribute node , here u need to add code which will be any variable code which is as per your requirement and type here u need to define data type , it can be integer , string or any class path where u can define multiple data

  • Next you need to create Plugin class file and add code as below
<?php

namespace Mageacademy\ExtensionAttributeExample\Plugin;

use Magento\Catalog\Api\ProductRepositoryInterface;

class ProductRepositoryPlugin
{
    public function afterGet(ProductRepositoryInterface $subject,$result,$sku)
    {
        $extensionAttribute = $result->getExtensionAttributes();

        $extensionAttribute->setCustomData("This is my custom Data");
        $result->setExtensionAttributes($extensionAttribute);
        return $result;
    }

}

Here you can see I have used get function of product repository class now I am getting getExtensionAttributes method and in that extension attribute I am setting customData which I need to pass as extension attribute and setting extension attribute and then returning the result.

  • Now I am enabling the module and run the product api as below and you will see extension attribute in result as below

You can see custom_data attribute inside extension_attributes node.

Now you will think how it is working, How it is sending custom_data in extension attribute , from how getExtensionAttribute and setExtensionAttribute is working.

So If you will open ProductInterface interface then you will below interface implemented in this interface

Here you will 2 methods getExtensionAttributes and setExtensionAttributes.

now If you open CustomAttributeDataInterface then you will below interface

Finally you if you will open ExtensibleDataInterface then you will see below code

Here you can see extension_attributes constant is defined , So conclusion is that due to this interface you are able to add extension attribute in rest api.

In our next topic we will explain how you can add extension attribute in your custom rest api.

Hope you like our this blog , so please share our blog with your friends and stay connected with us and visit again.

Subscribe to our newsletter

Leave a Reply
Your email address will not be published. *