Magento, Magento 2

How to Add Custom Field in Product REST API in Magento 2

How to Add Custom Field in Product REST API in Magento 2

Today, I will explain to how to add custom field in product API in Magento 2. When we need to pass product data from our store to other application we need to pass data using APIs. By default, Magento provides some attributes in product Rest API. But, if we want to add custom our field in product rest API then, how can we do that?

Generally, I see some tutorials and some answers for that. But, in that they guide for the add field using extension_attributes with create new product attribute in Database. But, Today I will add some steps to add custom field using extension attributes but without add field in database.

So, Let’s follow the below steps for add custom field in product REST API using extension_attributes

Steps to Add custom field in product REST API in Magento 2:

You may also like this :

1) First of all, Let’s assume that you have created simple module. Now, you need to create extension_attributes.xml to product extensible data object at app/code/RH/Helloworld/etc/ and paste the below code :

<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<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="special_discount" type="int"/>
    </extension_attributes>
</config>

Here, I added special_discount value as attribute code. You can add your code which you want to add in product REST API.

2) Now, You need to create di.xml file for add plugin. You need to create file at app/code/RH/Helloworld/etc/ and paste the below code :

<?xml version="1.0"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<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="add_custom_field_product_api" type="RH\Helloworld\Plugin\ProductCustAttr"/>
    </type>
</config>

3) After that, Create ProductCustAttr.php plugin file for add custom field and it’s value in product REST API. You need to create file at app/code/RH/Helloworld/Plugin/ and paste the below code :

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\Helloworld\Plugin;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product as ProductModel;

class ProductCustAttr
{
     public function afterGet(
        \Magento\Catalog\Api\ProductRepositoryInterface $subject,
        \Magento\Catalog\Api\Data\ProductInterface $entity
    )
    {
        $product = $entity;
        /** Get Current Extension Attributes from Product */
        $extensionAttributes = $product->getExtensionAttributes();
        $extensionAttributes->setSpecialDiscount(10); // custom field value set
        $product->setExtensionAttributes($extensionAttributes);
        return $product;
    }

    public function afterGetList(
        \Magento\Catalog\Api\ProductRepositoryInterface $subject,
        \Magento\Catalog\Api\Data\ProductSearchResultsInterface $searchCriteria
    ) : \Magento\Catalog\Api\Data\ProductSearchResultsInterface
    {
        $products = [];
        foreach ($searchCriteria->getItems() as $entity) {
            /** Get Current Extension Attributes from Product */
            $extensionAttributes = $entity->getExtensionAttributes();
            $extensionAttributes->setSpecialDiscount(10); // custom field value set
            $entity->setExtensionAttributes($extensionAttributes);
            $products[] = $entity;
        }
        $searchCriteria->setItems($products);
        return $searchCriteria;
    }
}

afterGet() method is used for get product by sku in REST API. afterGetList() method is used for search product using searchCriteria.

That’s it !!

Now, just remove generated and clean cache.

Output :

How to Add Custom Field in Product REST API in Magento 2 Output

I hope this blog is easy to understand about how to add custom field in product API in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Stay Safe and Stay Connected !!

Tagged , ,