I’ve been trying to learn how to create a discount function using this tutorial: https://shopify.dev/docs/apps/build/discounts/experience/build-discounts-function
I’ve successfully installed the app in my development store, but when I try to test it I keep getting InvalidOutputError.
I feel like I’ve followed the tutorial correctly and copied the Javascript provided into run.js and I’m not sure what the issue is. Any ideas?
Here is the code for run.js from the tutorial:
// -check
import { DiscountApplicationStrategy } from "../generated/api";
// Use JSDoc annotations for type safety
/**
* @typedef {import("../generated/api").RunInput} RunInput
* @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult
* @typedef {import("../generated/api").Target} Target
* @typedef {import("../generated/api").ProductVariant} ProductVariant
*/
/**
* {FunctionRunResult}
*/
const EMPTY_DISCOUNT = {
discountApplicationStrategy: DiscountApplicationStrategy.First,
discounts: [],
};
// The configured entrypoint for the 'purchase.product-discount.run' extension target
/**
* @param {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
const targets = input.cart.lines
// Only include cart lines with a quantity of two or more
.filter(line => line.quantity >= 2)
.map(line => {
return /** {Target} */ ({
// Use the cart line ID to create a discount target
cartLine: {
id: line.id
}
});
});
if (!targets.length) {
// You can use STDERR for debug logs in your function
console.error("No cart lines qualify for volume discount.");
return EMPTY_DISCOUNT;
}
// The /shopify_function package applies JSON.stringify() to your function result
// and writes it to STDOUT
return {
discounts: [
{
// Apply the discount to the collected targets
targets: targets,
// Define a percentage-based discount
value: {
percentage: {
value: "10.0"
}
}
}
],
discountApplicationStrategy: DiscountApplicationStrategy.First
};
};
Here is run.graphql:
query RunInput {
cart {
lines {
id
quantity
}
}
}
Here’s information from a run log:
Input (STDIN)
{
"cart": {
"lines": [
{
"id": "gid://shopify/CartLine/0",
"quantity": 2
}
]
}
}
Output (STDOUT)
{
"discounts": [
{
"targets": [
{
"cartLine": {
"id": "gid://shopify/CartLine/0"
}
}
],
"value": {
"percentage": {
"value": "10.0"
}
}
}
],
"discountApplicationStrategy": "FIRST"
}
Error message
[
{
"path": [
"discounts",
0,
"targets",
0
],
"explanation": "Expected one of valid values: productVariant. Got: cartLine"
}
]

