Today's tutorial will bring you a solid guide on how to add a custom field in product REST API in Magento 2. APIs are necessary when we require to pass product data from our store to other applications, to pass data. Magento provides some attributes in product Rest API, by default. But if there is a requirement to add a custom field in product Rest API, here is a step by step tutorial. The following guide will help you add steps to add a custom field using extension attributes.
Steps to Add custom field in product REST API in Magento 2:
1) First of all, we assume that a simple module is already created. Now, you can create extension_attributes.xml to product extensible data object at app/code/MageDelight/Helloworld/etc/ and paste the 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="special_discount" type="int"/> </extension_attributes> </config>
2) Now, create di.xml file for add plugin. Then, create file at app/code/MageDelight/Helloworld/etc/ and paste the below code:
<?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="add_custom_field_product_api" type="RH\Helloworld\Plugin\ProductCustAttr"/> </type> </config>
3) Post this, create ProductCustAttr.php plugin file to add a custom field and it’s value in product REST API. You need to create a file at app/code/MageDelight/Helloworld/Plugin/ and paste the below code:
<?php namespace MageDelight\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 to get products by SKU in the REST API. afterGetList() method is used for search product using searchCriteria.
That’s it !!
Now, just clean the cache.
We believe that this tutorial is easy to understand how to add a custom field in product API in Magento 2. In case, we missed anything or need to add some information, always feel free to leave a comment on this blog or reach us out. We will get back with a proper solution.