Magento 2.x.x extension attributes API add object array
-
14-04-2021 - |
Pregunta
Have added custom attribute to order API with following info here: https://www.atwix.com/magento-2/adding-custom-attribute-to-api-response-in-magento-2/
It adds the "customer_feedback": "this is my feedback"
fine to api.
How to add an Json object array ? E.G:
"my_custom_object": {
"Key1": "value!",
"Key2": "value2"
}
Thanks
No hay solución correcta
Otros consejos
I sourced out the answer to my question!.
One simple cannot do:
$my_custom_object = array("Key1" => "value!")
OR
$my_custom_object = json_encode(array("Key1" => "value!"));
It expect the value to be an object: So setting customAttribute like so:
$extensionAttributes->setOurCustomData($ourCustomData);
$ourCustomData
Must be an object:
So I create Interface with getter and setter and a model that implements the interface and in extension_attributes.xml
:
...
<attribute code="our_custom_data" type="Vendor\Module\Api\Data\CustomInterface"/>
...
In Plugin orderRepository:
Inject model : \Vendor\Module\Model\CustomModel $customModel
...
$ourCustomData = $this->customModel;
$extensionAttributes->setOurCustomData($ourCustomData);
...
Hope it helps anyone!
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange