Discussing Shopify Functions development, deployment, and usage in Shopify apps.
I am trying to query some custom data from the properties attribute from cart lines but failing.
Neither of the following works. Any suggestions? Is it possible?
query RunInput { cart { lines { id quantity attribute(key: "properties") { value } } } }
query RunInput { cart { lines { id quantity attribute(key: "properties") { custom_data: value } } }
Solved! Go to the solution
This is an accepted solution.
I managed to figure it out. Data added to the properties attribute are considered to be custom properties, so one just has to target the key of that custom property.
query RunInput {
cart {
lines {
id
quantity
custom_data: attribute(key: "_custom_data") {
value
}
}
}
You can't use all properties. Instead, you should use the 'properties' key. For example:
attribute(key: "discount") {
key
value
}
Thanks, but I'm not trying to get all the properties. I'm trying to get the attribute 'properties', the key is 'properties'. Custom data is added to that when the product is added to cart.
From the cart.json:
Are you sure it's dependent on the Cart Line properties field? If it is associated with the cart, you should do it like this.
query RunInput { cart {
attribute(key: "properties") {
value
} lines { id quantity } } }
This is an accepted solution.
I managed to figure it out. Data added to the properties attribute are considered to be custom properties, so one just has to target the key of that custom property.
query RunInput {
cart {
lines {
id
quantity
custom_data: attribute(key: "_custom_data") {
value
}
}
}
FYI, There's a better way to do this using Inline Fragments:
query RunInput {
cart {
lines {
id
quantity
attributes {
... on Attribute {
key
value
}
}
}
}
}
That will give you an array of all the attributes:
"attributes": [
{
"key": "test1",
"value": "true"
},
{
"key": "test2",
"value": "false"
}
]
This does not work with Shopify's cart function input where I've tried.
`attributes` doesn't exist in the schema. I am only able to grab them individually based on keys, not a list.
What API version are you using? It's definitely supported in latest.
Hi @rogueyoshi. This is a screenshot of the Storefront API. Functions APIs have their own GraphQL schemas. Generally, functions cannot query unbounded lists, for performance reasons. Thus you need to specify the specific attribute.
https://shopify.dev/docs/api/functions/reference/product-discounts/graphql/common-objects/cartline
Nick Wesselman | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Thanks for the clarification @Nick_Wesselman!
If this is the case, is there no way to preserve all of the properties from an expanded product?
I understand I can take the specific values by using the key for each attribute I want, but I have forms with a bunch of inputs/selects that add specific custom properties and the property names aren't all the same every time.
All I want is all the attributes/properties from the expanded product to be kept through to the order notes when I run a cart expand on it, but as soon as I look at the order details all of the original properties on item that I expanded are gone and it breaks the functionality that I need.
It even shows them in the checkout screen on the parent of the expanded items but it doesn't carry through to the order notes where we need them in order to see all the options chosen.
I recently had the same issue. What I ended up doing was adding attributes to ExpandedItems in the cart transform code. ExpandedItem attributes are available in api version 2024-04 and up.
@aashby13 wrote:I recently had the same issue. What I ended up doing was adding attributes to ExpandedItems in the cart transform code. ExpandedItem attributes are available in api version 2024-04 and up.
Yeah that's what I'm afraid of, the integration I'm building is for a huge variety of different options and properties that will be possible to have on a product.
There's no way for me to predict or know exactly what each of the properties keys will be as they will be different on all the products. I think that approach only works if you have the same properties/property names every single time on the products you are expanding.
Gotcha. My issue was somewhat similar. I took all the key/value pairs and put them in a nested array and then stringified that nested array and use it as a value in a hidden custom cart attribute. That way I could query the custom attribute and then parse it in my cart transform extension code.
That's smart, I will see if I can make something like that route work, basically add all the properties to one hidden property that stays the same and build them on the expanded items off of that. Thank you!
Would be a really nice feature if you could just preserve them from the base product.
Hi @aashby13 first of all! thank you so so so much! I was so stuck.
I was able to get my properties with the custom-data, but now I'm not sure how to set this in the function: