How can I access multiple discountNodes Metafields value?

yedla_dinesh
Shopify Partner
11 0 1

Hi everyone, 

 

I am attempting to create multiple discounts using the 'discountAutomaticAppCreate' GraphQL function and add metafields with the same key and namespace. This process has been successful and I have created 2 automatic discount. However, when I try to access the metafield in the 'run.graphql' file, I only retrieve the metafield of the latest discount node.

Therefore, my question is: How can I access the metafields of multiple discount nodes?

Is I'm missing somthing? Please help with me out with some clarification.
@Nick_Wesselman @Liam @SomeUsernameHe 


Kindly check the below code.

 

query RunInput($selectedCollectionIds: [ID!]) {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ... on ProductVariant {
          id
          product {
            id
            inAnyCollection(ids: $selectedCollectionIds)
          }
        }
      }
    }
  }
  discountNode {
    metafield(
      key: "test-function-config"
      namespace: "test-discount"
    ) {
      value
    }
  }
}

 


Thanks

Shopify Expert
Helping thousands of @Shopify store owners to achieve sustainable growth & increase revenue 2-3x 
Replies 5 (5)

SomeUsernameHe
Shopify Partner
469 53 90

To access the metafields of multiple discount nodes in Shopify using GraphQL, you need to modify your query to include each discount node you're interested in. From the provided query, it seems you're trying to access a single metafield from a generic discountNode which would typically return the metafield from the latest or default discount node.

To fetch metafields from multiple discounts, you must query each discount node separately. Here's how you can modify your query to retrieve the metafields from multiple discount nodes by specifying their IDs:

 

 

query GetDiscountMetafields($discountIds: [ID!]!) {
nodes(ids: $discountIds) {
... on DiscountNode {
id
metafield(namespace: "test-discount", key: "test-function-config") {
value
}
}
}
}

 

In this modified query, you provide a list of discount IDs as an input variable $discountIds. The query uses these IDs to fetch the metafield for each DiscountNode. This approach allows you to access the metafields of each discount individually based on the IDs you specify.

 

Assuming you know the specific IDs of the discounts whose metafields you want to access, you can pass them in the query like this:

 

{
"discountIds": ["gid://shopify/DiscountNode/1234567890", "gid://shopify/DiscountNode/0987654321"]
}

 

This method ensures that you retrieve the metafields from all specified discounts, allowing for more precise control over which discounts' metafields are accessed

 

Sorry, was not able to test this code completely since I am heading to bed, but if you still need help with it I'm a bit, I will troubleshoot when I wake up 

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
Nick_Wesselman
Shopify Staff
156 39 60

The question is about Functions, not the Admin API.

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

Nick_Wesselman
Shopify Staff
156 39 60

Hi @yedla_dinesh --

 

I may not understand your goals fully, but in a given function execution, you can only obtain the metafields for a single discount. You can reuse the same functionId across multiple discounts, and they should both execute independently and in parallel, with their own metafields. They may not both apply to the cart, depending on the discount combination rules.

 

-Nick

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

yedla_dinesh
Shopify Partner
11 0 1

Thanks for your reply.

I understand that in a single function execution, I can only obtain the metafields for a single discount. However, I want to access the metafields for multiple discounts in one go.

Let's say I've created 2-3 discounts using the 'discountAutomaticAppCreate' GraphQL function (and I've also added metafield data to each discount). Now, can I access all the discount metafield values in the Shopify product discount function (run.graphql) input query?

I really appreciate your help!

Thanks

 

Shopify Expert
Helping thousands of @Shopify store owners to achieve sustainable growth & increase revenue 2-3x 
Nick_Wesselman
Shopify Staff
156 39 60

Hi @yedla_dinesh --

No, this is not possible. Each discount you create with discountAutomaticAppCreate represents a separate execution of your function. They will all be executed in parallel and the discounts will be combined according to Shopify's discount combination rules. Each execution can only access the metafields of a single discount.

 

If you are looking for some sort of shared configuration, you might consider a shop metafield. Otherwise the best practice is to configure and output the discounts independently from each function run, and allow Shopify to choose the best discount combination for the shopper.

 

-Nick

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