Edit Discount Created with a Shopify Functions - How to get the Method context? (Code vs Automatic)

Solved

Edit Discount Created with a Shopify Functions - How to get the Method context? (Code vs Automatic)

Nico4
Shopify Partner
25 0 12

When clicking a previously created Discount in Shopify backend - How do I get the Discount Method (Code vs Automatic) when loading my Shopify Discount React file edit.jsx?

 

Is It passed down from the Shopify Discount list to my React file and how?

 

Or do I need to make an additional GraphQL query to defined the the Method (Code vs Automatic) before being able to define if Im dealing with A Code or Automatic discount?

 

Once I know the type of discount Im dealing with I can query GraphQL with either `codeDiscountNode` or `automaticDiscountNode`

 

Visual context:Screenshot 2023-02-27 at 3.49.38 PM.jpg

Accepted Solution (1)

Nick_Wesselman
Shopify Staff
171 43 70

This is an accepted solution.

Hi @Nico4 --

Check out this example. When querying a `discountNode`, you can check its type name and use inline fragments to get the appropriate fields.

query GetDiscount($id: ID!) {
    discountNode(id: $id) {
      id
      configurationField: metafield(
        namespace: "${metafields.namespace}"
        key: "${metafields.key}"
      ) {
        id
        value
      }
      discount {
        __typename
        ... on DiscountAutomaticApp {
          title
          discountClass
          combinesWith {
            orderDiscounts
            productDiscounts
            shippingDiscounts
          }
          startsAt
          endsAt
        }
        ... on DiscountCodeApp {
          title
          discountClass
          combinesWith {
            orderDiscounts
            productDiscounts
            shippingDiscounts
          }
          startsAt
          endsAt
          usageLimit
          appliesOncePerCustomer
          codes(first: 1) {
            nodes {
              code
            }
          }
        }
      }
    }
  }

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

View solution in original post

Reply 1 (1)

Nick_Wesselman
Shopify Staff
171 43 70

This is an accepted solution.

Hi @Nico4 --

Check out this example. When querying a `discountNode`, you can check its type name and use inline fragments to get the appropriate fields.

query GetDiscount($id: ID!) {
    discountNode(id: $id) {
      id
      configurationField: metafield(
        namespace: "${metafields.namespace}"
        key: "${metafields.key}"
      ) {
        id
        value
      }
      discount {
        __typename
        ... on DiscountAutomaticApp {
          title
          discountClass
          combinesWith {
            orderDiscounts
            productDiscounts
            shippingDiscounts
          }
          startsAt
          endsAt
        }
        ... on DiscountCodeApp {
          title
          discountClass
          combinesWith {
            orderDiscounts
            productDiscounts
            shippingDiscounts
          }
          startsAt
          endsAt
          usageLimit
          appliesOncePerCustomer
          codes(first: 1) {
            nodes {
              code
            }
          }
        }
      }
    }
  }

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