Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: How to use Cart Attribute on Shopify Function with Rust ?

Solved

How to use Cart Attribute on Shopify Function with Rust ?

TheCodeLover
Shopify Partner
16 0 1

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

http://shopifydevs.com
Accepted Solution (1)

nich-young
Shopify Partner
1 1 1

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.

View solution in original post

Replies 2 (2)

EcomGraduates
Shopify Partner
757 66 110

hello there  

 

  • Based on the provided input, you should be able to access the cart attribute value using the following code in Rust:

 

 

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
|  Check out our reviews: Trustpilot Reviews
|  We are Shopify Partners: EcomGraduates Shopify Partner



nich-young
Shopify Partner
1 1 1

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.