Update note_attributes without removing existing attributes

I’m wondering if there is a way to update the note_attributes of an order without clearing any existing note_attributes? Currently it seems like any time the note_attributes are updated, any existing data is removed (unless it is included again in the request to add new attributes).

For example, let’s say we GET /admin/api/2024-01/orders/XXXXXXXXX.json and the response includes:

//...
"note_attributes": {
    "foo": "bar"
},
//...

…and we want to add a new attribute. The docs suggest doing PUT /admin/api/2024-01/orders/XXXXXXXXX.json and including the attribute like so:

{
    "order": {
        "note_attributes": [
            {
                "name": "newAttribute",
                "value": "test1234"
            }
        ]
    }
}

However, this will result in the order only including newAttribute in the note_attributes. The “foo: bar” attribute will be lost.

It seems like one solution would be to pull the order before updating and spread the existing attributes into the PUT request, however this seems dangerous as it might create race conditions if 2 apps are updating the order simultaneously.

Is there a way to update the note_attributes without it deleting the existing data?

As you’ve seen the PUT request will overwrite the note attributes, so one option you could do, to avoid the risk of race conditions could be to use the orders/updated webhook to listen for changes to orders. This way, your application can be notified of any changes immediately, helping ensure that your local cache or state is up-to-date before making further updates. Would this work for your use case?

The current setup is pulling the most recent order data immediately before updating the order, so it theoretically should have as recent information as possible. The only issue would be if the timing of the processing logic and PUT requests from 2 different programs overlap. It’s unlikely, but it would be possible so I wanted to ask to see if there was an easy way to mitigate this. From the sounds of things though, it seems unlikely so we may just have to roll with the risk.

It would be great to know from Shopify if there is a reason why it behaves this way – to me at least it feels more intuitive for a PUT update to the order to add onto the existing data rather than deleting existing data more-or-less silently.

2 Likes

Agree, or having a dedicated endpoint just for adding additional note attributes to the existing ones.