Restrict products to tagged customers
A Shopify validation Function that lets only customers with a tag, such as trade or wholesale, check out with products that carry a restricted tag.
- Function API
- Cart and Checkout Validation
- Target
- cart.validations.generate.run
- Plan
- Any plan in a public app; Shopify Plus in a custom app
- App scope
- write_validations
- API version
- 2026-10
- Last checked
In short
This validation Function lets only signed-in customers with a tag, such as trade, check out with products that carry a restricted tag. Everyone else sees a message naming the products. Both tag lists come from one JSON metafield through input query variables. It uses the cart.validations.generate.run target of the Cart and Checkout Validation API, version 2026-10.
How to use it
- Create an app with Shopify CLI 4.9.0 or later and add the extension: shopify app generate extension --template cart_checkout_validation, with JavaScript.
- Replace the generated shopify.extension.toml and the two files in src/ with the files below; input_variables points the query at the configuration metafield.
- Run the fixtures with shopify app function run --input tests/guest-with-restricted-product.input.json.
- Deploy with shopify app deploy and turn the validation on with validationCreate.
- Set the validation’s $app:function-configuration metafield, for example {"customerTags": ["trade"], "productTags": ["trade-only"]}.
Code
shopify.extension.tomlapi_version = "2026-10"
[[extensions]]
name = "Restrict products to tagged customers"
handle = "restrict-products-tagged-customers"
type = "function"
[[extensions.targeting]]
target = "cart.validations.generate.run"
input_query = "src/cart_validations_generate_run.graphql"
input_variables = { namespace = "$app", key = "function-configuration" }
export = "cart-validations-generate-run"
[extensions.build]
command = ""
path = "dist/function.wasm"
src/cart_validations_generate_run.graphqlquery CartValidationsGenerateRunInput($customerTags: [String!], $productTags: [String!]) {
buyerJourney {
step
}
cart {
buyerIdentity {
customer {
hasAnyTag(tags: $customerTags)
}
}
lines {
merchandise {
__typename
... on ProductVariant {
product {
title
hasAnyTag(tags: $productTags)
}
}
}
}
}
validation {
metafield(namespace: "$app", key: "function-configuration") {
jsonValue
}
}
}
src/cart_validations_generate_run.js// @ts-check
/**
* @typedef {import("../generated/api").CartValidationsGenerateRunInput} CartValidationsGenerateRunInput
* @typedef {import("../generated/api").CartValidationsGenerateRunResult} CartValidationsGenerateRunResult
*/
/**
* Settings stored as JSON in the validation's metafield. The same metafield
* feeds the query's $customerTags and $productTags variables (see
* input_variables in shopify.extension.toml), so Shopify checks the tags.
*/
const DEFAULT_CONFIG = {
// {products} is replaced with the restricted product titles.
message: "{products} can only be ordered by approved trade customers. Please sign in or remove it from your cart.",
};
/** Errors about the cart as a whole show at the top of checkout and on the cart. */
const CART_TARGET = "$.cart";
/** @type {CartValidationsGenerateRunResult} */
const NO_ERRORS = { operations: [] };
/**
* @param {CartValidationsGenerateRunInput} input
* @returns {CartValidationsGenerateRunResult}
*/
export function cartValidationsGenerateRun(input) {
// Let anyone build a cart (a trade customer may sign in at checkout); the rule applies from checkout on.
if (input.buyerJourney?.step === "CART_INTERACTION") return NO_ERRORS;
// Guests have no customer record, so they never carry the tag.
if (input.cart.buyerIdentity?.customer?.hasAnyTag) return NO_ERRORS;
const restricted = input.cart.lines
.map((line) => line.merchandise)
.filter((merchandise) => merchandise.__typename === "ProductVariant" && merchandise.product.hasAnyTag)
.map((merchandise) => merchandise.product.title);
if (restricted.length === 0) return NO_ERRORS;
const config = { ...DEFAULT_CONFIG, ...input.validation?.metafield?.jsonValue };
const products = [...new Set(restricted)].join(", ");
return {
operations: [{ validationAdd: { errors: [{ message: config.message.replace("{products}", products), target: CART_TARGET }] } }],
};
}
Test input and expected output
tests/guest-adding-to-cart.input.json{
"buyerJourney": {
"step": "CART_INTERACTION"
},
"cart": {
"buyerIdentity": {
"customer": null
},
"lines": [
{
"merchandise": {
"__typename": "ProductVariant",
"product": {
"title": "Pro Barista Grinder",
"hasAnyTag": true
}
}
},
{
"merchandise": {
"__typename": "ProductVariant",
"product": {
"title": "Coffee Beans 1 kg",
"hasAnyTag": false
}
}
}
]
},
"validation": {
"metafield": {
"jsonValue": {
"customerTags": [
"trade"
],
"productTags": [
"trade-only"
]
}
}
}
}tests/guest-adding-to-cart.output.json{
"operations": []
}tests/guest-with-restricted-product.input.json{
"cart": {
"buyerIdentity": { "customer": null },
"lines": [
{ "merchandise": { "__typename": "ProductVariant", "product": { "title": "Pro Barista Grinder", "hasAnyTag": true } } },
{ "merchandise": { "__typename": "ProductVariant", "product": { "title": "Coffee Beans 1 kg", "hasAnyTag": false } } }
]
},
"validation": {
"metafield": { "jsonValue": { "customerTags": ["trade"], "productTags": ["trade-only"] } }
}
}
tests/guest-with-restricted-product.output.json{
"operations": [
{
"validationAdd": {
"errors": [
{
"message": "Pro Barista Grinder can only be ordered by approved trade customers. Please sign in or remove it from your cart.",
"target": "$.cart"
}
]
}
}
]
}
tests/no-restricted-products.input.json{
"cart": {
"buyerIdentity": { "customer": { "hasAnyTag": false } },
"lines": [
{ "merchandise": { "__typename": "ProductVariant", "product": { "title": "Coffee Beans 1 kg", "hasAnyTag": false } } }
]
},
"validation": {
"metafield": { "jsonValue": { "customerTags": ["trade"], "productTags": ["trade-only"] } }
}
}
tests/no-restricted-products.output.json{ "operations": [] }
tests/tagged-customer.input.json{
"cart": {
"buyerIdentity": { "customer": { "hasAnyTag": true } },
"lines": [
{ "merchandise": { "__typename": "ProductVariant", "product": { "title": "Pro Barista Grinder", "hasAnyTag": true } } },
{ "merchandise": { "__typename": "ProductVariant", "product": { "title": "Coffee Beans 1 kg", "hasAnyTag": false } } }
]
},
"validation": {
"metafield": { "jsonValue": { "customerTags": ["trade"], "productTags": ["trade-only"] } }
}
}
tests/tagged-customer.output.json{ "operations": [] }
Written by Lintel. MIT license: free to use in your own store and in client work.
Limits and edge cases
- Guests have no customer record, so they can’t buy restricted products until they sign in.
- The Function only blocks checkout. Products stay visible and can be added to the cart; hide them in the theme if they must be invisible.
- If the metafield is missing, both variables are null and no product counts as restricted.
Questions
- How do I sell some products only to approved customers on Shopify?
- Tag the customers and the products, then use a validation Function that blocks checkout when an untagged customer has a tagged product, as this example does.
- Can customers still see the restricted products?
- Yes. A Function runs on the cart and checkout, not on product pages. Use the theme to hide prices or buttons for customers without the tag.
- Why are the tags in input variables instead of the code?
- Tags are checked by Shopify with hasAnyTag. A variable lets each store, and each validation, set its own tags in a metafield without a new deploy.
Shopify documentation
- Written for Function API version 2026-10 and checked against Shopify’s reference on the date shown. Shopify releases a new version every quarter.
- Custom apps that contain Functions only run on Shopify Plus stores; a public app with the same Function runs on any plan unless the page says otherwise.
- Every example ships with test fixtures, and we run them before publishing. Run them again after you change the code: shopify app function run.
- MIT license. Use the code in your own store or in client work.
Need this built, tested and maintained for your store? See Shopify Functions development.
