Solved

Can I use variables in input queries in cart and checkout validation function extension?

lucky-funny
Shopify Partner
15 0 1

In my case,I have create a cart and checkout validation function,and i want to  check if a product is inAnyCollection use variables($selectedCollectionIds) in input queries,but it didnot work.

my code:

 

configuration in shopify.extension.tom file:

[input.variables]
namespace = "$app:primaryns"
key = "opts"

 

code in input.graphql file:

query Input($selectedCollectionIds: [ID!]) {
    cart {
        lines {
            quantity 
            merchandise {
                __typename
               ... on ProductVariant {
                   id
                   product{
                       id
                       handle
                       inAnyCollection: inAnyCollection(ids: $selectedCollectionIds)
                  }
             }
        }
    }
}

 

code in server:

const response = await graphql(
`
mutation MetaFieldsSet($metaFields: [MetafieldsSetInput!]!) {
    metafieldsSet(
        metafields: $metaFields
    ) {
            metafields {
                id
                key
                namespace
                type
                value
            }
            userErrors {
                message
                code
            }
        }
    }
`,
{
    variables: {
         metaFields: [{
             namespace: "$app:primaryns",
             key: "opts",
             ownerId: "gid://shopify/Shop/1",
             type: "json",
             value: "{\"selectedCollectionIds\":[\"gid://shopify/Collection/1\",\"gid://shopify/Collection/2\"]}"
        }]
    },
});

 

The result is that the variable $selectedCollectionId cannot be obtained in input.graphql.

Who knows what the reason is? 

Accepted Solution (1)
Liam
Shopify Staff
2731 302 783

This is an accepted solution.

Hi again,

 

I've looked into this further and it does seem that you can’t use variables yet with checkout validation as checkout doesn’t yet support metafields for functions. However this should be available as it's on our roadmap. 

Liam | Developer Advocate @ 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 Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 7 (7)

Liam
Shopify Staff
2731 302 783

Hi Lucky-Funny,

 

Based on your code, it looks like you're using Shopify's extension points and metafields to pass values into your query. Here are a few suggestions that might help you resolve the issue:

  1. Double-check your namespace and key values: Ensure that the namespace and key values in your server code match exactly with those defined in the shopify.extension.toml file.

  2. Verify the value of the metafield: The value field in your metaFieldsSet mutation is a JSON string. Make sure it has been correctly formatted and parsed.

  3. Check your GraphQL Query: The issue might be with how you're trying to access the selectedCollectionIds variable in your query. Ensure that the selectedCollectionIds variable is correctly defined and used in your query.

  4. Use the latest API version: Shopify frequently releases new API versions, so ensure you're using the most recent version for the most accurate results.

  5. Debugging: If the problem persists, I would recommend debugging your code to see the actual value being passed to selectedCollectionIds. This will help you ascertain whether the problem is the variable not being passed correctly or the query itself.

Try out the above and let us know if you're still experiencing issues - hope this helps!

Liam | Developer Advocate @ 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 Shopify.dev or the Shopify Web Design and Development Blog

lucky-funny
Shopify Partner
15 0 1

I will carefully review the code according to your suggestion. Before that, could you please help me confirm one thing? I learned in the tutorial documentation(https://shopify.dev/docs/apps/functions/input-output/variables-queries) that Input query variables value can be set using the metafield collection on the create or update mutation on your function owner (for example, discountAutomaticAppCreate).
Now in my case,the function owner is the shop right?
In other words, is it correct for me to set input variable values to the Matefields in the shop?

Liam
Shopify Staff
2731 302 783

This is an accepted solution.

Hi again,

 

I've looked into this further and it does seem that you can’t use variables yet with checkout validation as checkout doesn’t yet support metafields for functions. However this should be available as it's on our roadmap. 

Liam | Developer Advocate @ 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 Shopify.dev or the Shopify Web Design and Development Blog

lucky-funny
Shopify Partner
15 0 1

Okay, thank you for solving my big problem. Do you have a schedule for your plan? Can you let me know as soon as possible when supporting this feature? 

RobFarmLink
Shopify Partner
42 2 31

@Liam is this still not possible? Is it on the roadmap? Could someone at least add to the docs. I just spent a few hours banging my head.

harsh_mishra
Shopify Partner
3 0 0

@LiamAny update on this? Is it possible now?

thomasavada
Shopify Partner
1 0 0

Hi Liam,

Is it possible now that we can use this. Without this, we cannot pass deliver collection, or customer tags limit feature. Or, better, instead of inCollections, hasTags, you can just return us the collectionIds, tags in the product graphQL query, then we can do the logic in the Function body. So no need to pass any variables.