1

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

1 Answer 1

0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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