I am trying to override Magento\Eav\Api\Data\AttributeOptionInterface
in my custom module. I want to add one new option to existing interface.
The idea behind is, As of now we can only pass defined options data in attribute options REST API
See below API request format for more clarity.
Api
Url = http://test.com/index.php/rest/V1/products/attributes/manufacturer/options/
Api Methodd - Post
Api body - {
"option": {
"label": "test",
"is_default": "true",
}
}
As of now its passing 3 parameters as value, I want to add my custom 4th parameters Ex – Slider.
So parameters should be
{
"option": {
"label": "test",
"is_default": "true",
"slider":"test1"
}
}
I have tried to override interface and its method but its giving property setter not defined
Error Message
"message": "Property \"Slider\" does not have corresponding setter in class \"Magento\\Eav\\Api\\Data\\AttributeOptionInterface\"."
So here my question is how can I override API interface and pass my custom or add my option in existing parameters.
Thanks.
Best Answer
To resolve this issue, We require to override mainly two interfaces and model. After overriding interfaces and model, Overrided interfaces requires binding with model using di.xml to initiate objects while API call.
Check the below code to add new fields to AttributeOptionInterface.
[Package_Name][Module_Name]\etc\di.xml
[Package_Name][Module_Name]\Api\Eav\Data\AttributeOptionInterface.php
[Package_Name][Module_Name]\Model\Eav\Entity\Attribute\Option.php
After overriding interface to add 'slider' field and overring option.php, it is required to change the parameter type of API call for attribute options in webapi.xml for Magento_Catalog module.
[Package_Name][Module_Name]\etc\webapi.xml
And Finally,
[Package_Name][Module_Name]\Api\Catalog\ProductAttributeOptionManagementInterface.php