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.

  • You can achieve this by using extension attributes – Sairam Sigirisetty Sep 15 at 14:01

Create a extension attribute for the required datainterface here I'm providing sample:

<extension_attributes for="Magento\Store\Api\Data\StoreConfigInterface">
    <attribute code="sample" type="string"/>
</extension_attributes>

Have a plugin on the interface di.xml

<type name="Magento\Store\Api\StoreConfigManagerInterface">
    <plugin name="nameofplugin"
            type="Namespace\Modulename\Plugin\filename"/>
</type>

Plugin class

    <?php

      namespace Emc\App\Plugin;

      use Magento\Store\Api\Data\StoreConfigExtensionInterface;
      use Magento\Store\Api\Data\StoreConfigInterface;
      use Magento\Store\Api\Data\StoreConfigExtensionFactory;
      use Magento\Setup\Exception;
      use Magento\Directory\Model\Currency;
      use Magento\Store\Model\StoreManagerInterface;


/**
 * Class classname
 */
class classname
{
    protected $storeConfigExtensionFactory;
    protected $storeConfigInterface;
    protected $storeConfigExtensionInterface;
    protected $scopeConfig;

    /**
     * Product constructor.
     * @param \Magento\Catalog\Model\ProductRepository $productRepository
     */
    // objects need to be changed based on your requirement 
    public function __construct(
    Currency $currencyFactory, 
    StoreManagerInterface $storeManager, 
    StoreConfigExtensionFactory $storeConfigExtensionFactory, 
    StoreConfigInterface $storeConfigInterface, 
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
            $this->storeConfigExtensionFactory = $storeConfigExtensionFactory;
            $this->storeConfigInterface = $storeConfigInterface;
            $this->scopeConfig = $scopeConfig;
            $this->currencyFactory = $currencyFactory;
            $this->storeManager = $storeManager;

     }


    /**
     * @param string[] $storeCodes list of stores by store codes, will return all if storeCodes is not set
     * @return \Magento\Store\Api\Data\StoreConfigInterface[]
     */
    public function aroundGetStoreConfigs(\Magento\Store\Model\Service\StoreConfigManager $storeConfigManager, \Closure $proceed, array $storeCodes = null)
    {

        $result = $proceed($storeCodes);
        foreach ($result as $storevalues) {
            $storeconfigExtension = $this->storeConfigExtensionFactory->create();
->getCurrencySymbol());
            $storeconfigExtension->setSample($storedKey); //attribute code

            $storevalues->setExtensionAttributes($storeconfigExtension);
        }
        return $result;
    }

}
  • Thanks for your response, have added plugin details as per your suggestion and sample code provided. However, still no success. Edited question accordingly and added details on plugin. – Miky Sep 16 at 7:46

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.