Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
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
}
}
}
}
}
}
}
https://shopify.dev/docs/apps/functions/input-output/variables-queries
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.
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025