Skip to content

Instantly share code, notes, and snippets.

@RAAbbott
Created May 30, 2024 05:16
Show Gist options
  • Save RAAbbott/47070a0a47cf8c97b713dbdea3ebe11d to your computer and use it in GitHub Desktop.
Save RAAbbott/47070a0a47cf8c97b713dbdea3ebe11d to your computer and use it in GitHub Desktop.
Quantity Limits Cart Validation - Shopify Functions
query RunInput {
cart {
lines {
quantity
merchandise {
__typename
... on ProductVariant {
product {
metafield(namespace: "quantity_limits", key: "limit") {
value
}
}
}
}
}
}
}
// @ts-check
/**
* @typedef {import("../generated/api").RunInput} RunInput
* @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult
*/
/**
* @param {RunInput} input
* @returns {FunctionRunResult}
*/
export function run(input) {
const errors = input.cart.lines
.filter(({ quantity, merchandise }) => {
if (merchandise.__typename === "ProductVariant") {
const limit = merchandise.product.metafield?.value;
return limit && quantity > Number(limit);
}
})
.map(() => ({
localizedMessage: "Not possible to order more than one of each",
target: "cart",
}));
return {
errors,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment