Discussing Shopify Functions development, deployment, and usage in Shopify apps.
I am creating app with Shopify Functions and facing problems accessing fields in Merchandise Object. Particularly I need to access product field with its properties but getting this error when I try to deploy the code with
shopify app deploy
The same problem happens when accessing other objects such as CartLine. The documentation (https://shopify.dev/api/functions/reference/product-discounts/graphql/common-objects/merchandise ) states that field is accessible over productVariant object. Here is the screenshot of error.
The project template was taken from https://github.com/Shopify/function-examples/tree/main/sample-apps/discounts-tutorial
My code:
Path: extentions/volume/src/main.rs
fn function: line 14-51
for line in cart_lines {
if line.merchandise.product.metafield.value.products.contains(line.merchandise.product.id) {
targets.push(Target::ProductVariant {
id: line.merchandise.id.unwrap_or_default(),
quantity: None,
});
}
}
The Input is the following:
{
"cart": {
"lines": [
{
"quantity": 3,
"merchandise": {
"id": "gid://shopify/ProductVariant/40125776645833",
"product": {
"id": "gid://shopify/Product/6806885537821",
"metafield": null
}
}
},
{
"quantity": 3,
"merchandise": {
"id": "gid://shopify/ProductVariant/40127786613065",
"product": {
"id": "gid://shopify/Product/6806885008953",
"metafield": null
}
}
}
]
},
"discountNode": {
"metafield": {
"value": "{\"quantity\":3,\"percentage\":20}"
}
}
}
If I print line.merchandise object I see:
Merchandise { id: Some("gid://shopify/ProductVariant/40125287745833") }
Merchandise { id: Some("gid://shopify/ProductVariant/40125287713065") }
{"discountApplicationStrategy":"FIRST","discounts":[{"value":{"percentage":{"value":20.0}},"targets":[{"productVariant":{"id":"gid://shopify/ProductVariant/40125287745833"}},{"productVariant":{"id":"gid://shopify/ProductVariant/40125287713065"}}]}]}
product Object is missing
Can anyone help to resolve those errors?
Solved! Go to the solution
This is an accepted solution.
In the tutorial and example code we provide, the input JSON is deserialized into Rust structs that are defined in the api.rs. It looks like you need to update the Rust structs to match your input query.
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
This is an accepted solution.
In the tutorial and example code we provide, the input JSON is deserialized into Rust structs that are defined in the api.rs. It looks like you need to update the Rust structs to match your input query.
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