The default response from magento 2 category api endpoint /rest/V1/categories/3 is
{
"id": 3,
"parent_id": 2,
"name": "Gear",
"is_active": true,
"position": 4,
"level": 2,
"children": "4,5,6",
"created_at": "2018-08-10 08:32:49",
"updated_at": "2018-08-24 08:02:27",
"path": "1/2/3",
"available_sort_by": [],
"include_in_menu": true,
"custom_attributes": [
{
"attribute_code": "description",
"value": "<p>Gear Category Static Contents.</p>"
},
{
"attribute_code": "display_mode",
"value": "PAGE"
},
{
"attribute_code": "landing_page",
"value": "11"
},
{
"attribute_code": "is_anchor",
"value": "0"
},
{
"attribute_code": "path",
"value": "1/2/3"
},
{
"attribute_code": "custom_layout_update",
"value": "<referenceContainer name=\"catalog.leftnav\" remove=\"true\"/>"
},
{
"attribute_code": "children_count",
"value": "3"
},
{
"attribute_code": "custom_use_parent_settings",
"value": "0"
},
{
"attribute_code": "custom_apply_to_products",
"value": "0"
},
{
"attribute_code": "url_key",
"value": "gear"
},
{
"attribute_code": "url_path",
"value": "gear"
}
]}
I would like to add a custom value to be passed in response, which is not derived from database, but generated based on certain calculation.
So my response should also contain
{
...
{
"attribute_code": "my_custom_flag",
"value": "1"
}
...
}
I tried the plugin approach and setting custom data in "afterGet" method, however it never shows up in response.
Appreciate any help on how to achieve this.
UPDATE: Basis Sairam's suggestion ...
Added di.xml as
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Api\CategoryRepositoryInterface">
<plugin name="my_category_plugin" type="My\Catalog\Plugin\CategoryPlugin"/>
</type>
Added extension_attributes.xml as
<?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\CategoryInterface">
<attribute code="my_custom_flag" type="string"/>
</extension_attributes>
And the plugin file
<?php
namespace My\Catalog\Plugin;
use Magento\Catalog\Api\Data\CategoryExtensionFactory;
use Magento\Catalog\Api\Data\CategoryExtensionInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
class CategoryPlugin
{
protected $extensionFactory;
/**
* @param \Magento\Catalog\Api\Data\CategoryExtensionFactory
* $extensionFactory
*/
public function __construct(\Magento\Catalog\Api\Data\CategoryExtensionFactory $extensionFactory)
{
$this->extensionFactory = $extensionFactory;
}
/**
* @param CategoryRepositoryInterface $subject
* @param CaetgoryInterface $category
*
* @return CaetgoryInterface
*/
public function afterGet(\Magento\Catalog\Api\CategoryRepositoryInterface $subject, \Magento\Catalog\Api\Data\CategoryInterface $category)
{
$extensionAttributes = $category->getExtensionAttributes();
$extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
// set custom flag
$extensionAttributes->setMyCustomFlag('1');
// $extensionAttributes->setExtensionAttribute('my_custom_flag', '1');
$category->setExtensionAttributes($extensionAttributes);
return $category;
}
}
However, the response from the API call never changes, it's still missing "my_custom_flag". It remains unaffected.