I tried to add custom data to the endpoint V1/categories but ended up with getting error
My code is below
- etc/extension_attributes.xml
<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\CategoryRepositoryInterface">
<attribute code="banner_slider" type="string" />
</extension_attributes>
</config>
2.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\CategoryRepositoryInterface">
<plugin name="banner_image_data" type="Vendor\ModuleName\Plugin\Model\CategoryRepository"/>
</type>
</config>
3.Plugin
use Magento\Catalog\Model\CategoryRepository as Subject;
class CategoryRepository
{
/**
* @param Subject $subject
* @param CategoryInterface $data
* @return CategoryInterface
*/
public function afterGet(
Subject $subject,
CategoryInterface $data
)
{
$extensionAttributes = $data->getExtensionAttributes();
$extensionAttributes->setBannerSlider('sandesh');
$data->setExtensionAttributes($extensionAttributes);
return $data;
}
}
My error
Call to undefined method Magento\Catalog\Api\Data\CategoryExtension::setBannerSlider()