I am trying to create Shopify Functions to create custom discounts. I know I can use other apps such as AIOD, but the manual discounts created in them dont work on default checkout page of Shopify and I need that functionality because I am creating Android app created in React Native consuming direct GraphQl APIs of Shopify and the discounts created in AIOD dont work in GraphQl functions such as CartCreate.
I created Shopify Functions using Shopify cli and this is the code of extensions created:
-
Name - getmobi-discount
Code of cart_lines_discounts_generate_run.js:
const COLLECTION_IDS = [
"gid://shopify/Collection/11",
"gid://shopify/Collection/22",
"gid://shopify/Collection/33",
"gid://shopify/Collection/44"
];
const RULES = {
"gid://shopify/Collection/11": 1000,
"gid://shopify/Collection/22": 500,
"gid://shopify/Collection/33": 750,
"gid://shopify/Collection/44": 1000
};
export function cartLinesDiscountsGenerateRun(input) {
const discounts = [];
for (const line of input.cart.lines) {
const matches = line.merchandise.product.inCollections.map(c => c.isMember);
for (let i = 0; i < COLLECTION_IDS.length; i++) {
if (!matches[i]) continue;
const colId = COLLECTION_IDS[i];
const amount = RULES[colId];
if (amount) {
discounts.push({
targets: [{ cartLine: { id: line.id } }],
value: {
fixedAmount: {
amount: amount.toString()
}
}
});
break;
}
}
}
return {
discounts,
discountApplicationStrategy: "FIRST"
};
}
Code of cart_lines_discounts_generate_run.graphql
query CartLinesDiscountsGenerateRunInput {
cart {
lines {
id
quantity
cost {
amountPerQuantity {
amount
}
}
merchandise {
... on ProductVariant {
id
product {
inCollections(
ids: [
"gid://shopify/Collection/11"
"gid://shopify/Collection/22"
"gid://shopify/Collection/33"
"gid://shopify/Collection/44"
]
) {
isMember
}
}
}
}
}
}
}
Code of index.js
export * from './cart_lines_discounts_generate_run';
Code of shopify.extension.toml
api_version = "2026-04"
[[extensions]]
name = "getmobi-discount"
handle = "getmobi-discount"
type = "function"
uid = "GUID"
description = "GetMobi discount"
[[extensions.targeting]]
target = "cart.lines.discounts.generate.run"
input_query = "src/cart_lines_discounts_generate_run.graphql"
export = "cart-lines-discounts-generate-run"
[extensions.capabilities]
discount_classes = ["PRODUCT"]
When I run command “shopify app deploy”, it deploys the app successfully and when I install the app in my Shopify Store and go to Discounts screen to create a new discount, it shows there as well:
