How to make the 'hasTags' part of a discount function dynamic in a Shopify app?

How to make the 'hasTags' part of a discount function dynamic in a Shopify app?

Hamza_Hussain
Shopify Partner
56 6 13

Hi guys I working on a Shopify App and I made a Shopify function to add discounts and this works fine but I want to make the "hasTags" part dynamic. The goal is to make text fields in the admin called product Tag and Customer Tags and it's values should come here in the product discount functions (Extension).

 

query RunInput {
  discountNode {
    metafield(namespace: "custom", key: "discount_value") {
      value
    }
  }
  cart {
    buyerIdentity {
      customer {
        amountSpent {
          amount
        }
        metafield(namespace: "custom", key: "customerDiscount") {
          value
        }
        hasTags(tags: ["customerDiscount"]) {
          hasTag
          tag
        }
      }
    }
    lines {
      merchandise {
        ... on ProductVariant {
          id
          product {
            id
            handle
            hasTags(tags: ["productDiscount"]) {
              tag
              hasTag
            }
          }
        }
      }
    }
  }
}
Honey G Hamza
Replies 2 (2)

tobebuilds
Shopify Partner
525 38 139

https://shopify.dev/docs/apps/functions/input-output/variables-queries

Founder, Regios Discounts app (4.8 stars, 81 reviews, Built for Shopify)
- Custom discounts made simple
- "Just about any discount you'll ever need"
- Built by an ex-Google software engineer

sherpabot
Shopify Partner
42 3 4

The documentation might seem a bit complex, so here’s a streamlined version:

First, define the namespace and key in your run.graphql file like this:

 

metafield(namespace: "custom", key: "discount_value") 

 

 

Next in the extensions toml file you need to identify it:

 

  [extensions.input.variables]
  namespace = "<YOUR_NAME_HERE>"
  key = "<YOUR_KEY_HERE>"

 


Lastly, when creating the actual meta tag for what ever route/mutation you use in the admin you will push the Product & Customer tag using the name space:

 

  const response = await admin.graphql(
    `#graphql
        mutation CreateAutomaticDiscount($discount: DiscountAutomaticAppInput!) {
          discountCreate: discountAutomaticAppCreate(automaticAppDiscount: $discount) {
            automaticAppDiscount {
              discountId
            }
            userErrors {
              code
              message
              field
            }
          }
        }`,
    {
      variables: {
        discount: {
          ...baseDiscount,
          metafields: [
            {
              namespace: "<YOUR_NAME_HERE>",
              key: "<YOUR_KEY_HERE>",
              type: "json",
              value: JSON.stringify({
                ... OTHER_ITEMS_HERE,
                customer_tag: <TAG_PASSED_FROM_ADMIN>
                product_tag: <TAG_PASSED_FROM_ADMIN>,
              }),
            },
          ],
        },
      },
    },
  );

 


Now in your Graphql file you can import the dynamic variable (tags for product/customer) like you would a graphql variable.

 

query RunInput($customer_tag: String! = "default-tag") {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ... on ProductVariant {
          id
        }
      }
    }
    buyerIdentity {
      customer {
        hasTags(tags: $customer_tag) {
          tag
          hasTag
        }
      }
    }
  }
  discountNode {
    metafield(namespace: "<YOUR_NAME_HERE>", key: "<YOUR_KEY_HERE>") {
      value
    }
  }
}

 

Here I named the graphql input variable customer_tag. Be sure you match the grapql variable input with the one you called in the name space when making the metafeild (as shown when creating the discount). That is crucial. 


Now you are ready. 

  1. Finalize by running shopify app function schema and shopify app function schema and shopify app function typegen. Next you can run shopify app dev but at times that isn't sufficient and you will need to shopify app deploy prior to testing. Lastly, you can test the functionality and check the extensions STDOUT in the partner dashboard within your app.