How can I find and retrieve cart attributes in the order object?

rubby
Shopify Partner
2 0 0

So I've added a script tag that adds a custom "ref" attribute via /cart/update.js to the customer's cart. 

 

I've also set up a webhook that listens to the orders/paid event.

 

I've tried looking for the "ref" attribute everywhere but I can't find it. It's not in the order object, nor in the checkout object from /admin/api/2024-01/checkouts/[checkout_token].json.

 

The order object includes a cart_token but I can't figure out how to get a cart by it's token via API.

 

All of my app's logic is on the server side, there's no custom checkout UI or anything.

Replies 2 (2)

rubby
Shopify Partner
2 0 0

Figured it out! For future readers in a similar situation, you have to make sure your attribute names start with a "_" character - in my case it had to be "_ref". This way the attribute gets passed to the note_attributes array in the order object.

dylanpierce
Shopify Partner
215 3 98

If there's anyone else stumbling across this via Google, I too was eventually able to update the `order.note_attributes` from the cart.

But unlike @rubby I was able to create a note attribute without the `_` prefix. A normal variable works fine.

1. Create a note attribute from the frontend of the online store:

 

```javascript

const formData = new FormData()

formData.append("attributes[your_attribute_name]", "your attribute value")

 

fetch(window.Shopify.routes.root + 'cart/update.js', {
  method: 'POST',
  body: formData
})
.then(response => response.json())
.then(data => console.log(data));

```

 

2. In the webhook, observe your attribute added as an entry `order.note_attributes`.

 

Another side effect is the note attribute is shown in the order page in the Shopify Admin. Might not be ideal if you need to store details that shouldn't be editable, like a unique ID number.

Founder of Real ID - Verify your customer's real IDs easily & securely with modern A.I.

Want to see it in action? Check out our demo store.