Discussing Shopify Functions development, deployment, and usage in Shopify apps.
Hello, I am totally new in Rust. I want to develop a function where payment method will be hidden based on cart attribute. I have done everything but in rust, I don't know how to access cart attribute value.
Here is my input.Graphql
query Input { cart { payment_method: attribute(key: "payment_method") { value } cost { totalAmount { amount } } } paymentMethods { id name } paymentCustomization { metafield(namespace: "payment_customize", key: "function-configuration") { value } } }
This is this input which I got in app extenstion:
{ "cart": { "payment_method": { "value": "COD" }, "cost": { "totalAmount": { "amount": "100.0" } } }, "paymentMethods": [ { "id": "gid://shopify/PaymentCustomizationPaymentMethod/0", "name": "(for testing) Bogus Gateway" }, { "id": "gid://shopify/PaymentCustomizationPaymentMethod/1", "name": "Cash on Delivery (COD)" } ], "paymentCustomization": { "metafield": { "value": "{\"paymentMethodName\":\"Cash on Delivery (COD)\",\"cartProperty\":\"COD\"}" } } }
But In Rust, I was trying to access attribute value like this:
if input.cart.payment_method.value.to_string() == config.cart_property.to_string() { return Ok(no_changes); }
Can anyone help me with this ?
Thanks
Solved! Go to the solution
This is an accepted solution.
Edited: I finally worked this out, partially with ChatGPT to explain how the rust language worked. I thought someone else may want the basic code samples to work from when trying to get used to working in Rust.
If you have an INPUT like this and want to filter items where your Attribute value is 'Yes'
{
"cart": {
"lines": [
{
"quantity": 1,
"attribute": {
"value": "Yes"
},
"merchandise": {
"__typename": "ProductVariant",
"id": "gid://shopify/ProductVariant/1234",
}
}
]
}
}
Update the main.rs file to include the struct from the crate for attribute and then run a filter that checks the type for Some properly.
// main.rs
// Import create definition
use crate::input::InputCartLinesAttribute;
...
#[shopify_function]
fn function(input: input::ResponseData) -> Result<output::FunctionResult> {
...
let targets = input
.cart
.lines
.iter()
...
.filter(|line| match &line.attribute {
Some(InputCartLinesAttribute { value: Some(str) }) => str == "Yes",
_ => false,
})
...
Hope this helps someone as I spent a long time scratching my head to get this to work.
hello there
Here, you first assign the payment_method
field from the input GraphQL query to a variable. You then use the if let
construct to check if it contains a value. If it does, you can access the value
field and compare it to your desired value from the config
variable. If the comparison is true, you can return the no_changes
value.
If this fixed your issue, likes and accepting as a solution are highly appreciated.
Build an online presence with our custom built Shopify Theme EcomifyTheme
This is an accepted solution.
Edited: I finally worked this out, partially with ChatGPT to explain how the rust language worked. I thought someone else may want the basic code samples to work from when trying to get used to working in Rust.
If you have an INPUT like this and want to filter items where your Attribute value is 'Yes'
{
"cart": {
"lines": [
{
"quantity": 1,
"attribute": {
"value": "Yes"
},
"merchandise": {
"__typename": "ProductVariant",
"id": "gid://shopify/ProductVariant/1234",
}
}
]
}
}
Update the main.rs file to include the struct from the crate for attribute and then run a filter that checks the type for Some properly.
// main.rs
// Import create definition
use crate::input::InputCartLinesAttribute;
...
#[shopify_function]
fn function(input: input::ResponseData) -> Result<output::FunctionResult> {
...
let targets = input
.cart
.lines
.iter()
...
.filter(|line| match &line.attribute {
Some(InputCartLinesAttribute { value: Some(str) }) => str == "Yes",
_ => false,
})
...
Hope this helps someone as I spent a long time scratching my head to get this to work.