Created
May 30, 2024 05:16
-
-
Save RAAbbott/47070a0a47cf8c97b713dbdea3ebe11d to your computer and use it in GitHub Desktop.
Quantity Limits Cart Validation - Shopify Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query RunInput { | |
cart { | |
lines { | |
quantity | |
merchandise { | |
__typename | |
... on ProductVariant { | |
product { | |
metafield(namespace: "quantity_limits", key: "limit") { | |
value | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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