Add configuration to your validation function
Your validation's configuration will be stored in metafields and made available to the function at runtime. Metafields are the primitive to store your app's data in Shopify.
What you'll learn
Anchor link to section titled "What you'll learn"In this tutorial, you'll learn how to do the following tasks:
- Define what configuration settings will be surfaced to merchants.
- Read and use the merchant-defined values in your function.
Requirements
Anchor link to section titled "Requirements"- You've completed the Getting started with building validations tutorial.
Step 1: Create the metafield definition
Anchor link to section titled "Step 1: Create the metafield definition"To make your function reusable, replace hardcoded values in your function with metafield values.
For security reasons, it's mandatory to create a metafield definition under a reserved namespace. Reserved namespaces require additional permissions to work in Shopify admin, so you'll need to request these permissions at app installation time.
To create a metafield definition and grant additional access to the Shopify admin, update your shopify.server.ts
file with the following code inside the afterAuth
hook:
Your code should look like the following example:
Step 2: Configure the function
Anchor link to section titled "Step 2: Configure the function"Update your input query to request a metafield value that's owned by your validation. Then, use that value in your function logic instead of the previously hardcoded value.
Navigate to your function in
extensions/cart-checkout-validation
.Replace the contents of
src/run.graphql
file with the following code:If you're using JavaScript, then run the following command to regenerate types based on your input query:
Replace the
src/run.rs
orsrc/run.js
file with the following code:This update includes parsing the JSON metafield value, and using values from that JSON in the function logic instead of hardcoded values.
The JSON includes cart quantity limits for each product.
This change is automatically reflected as long as you're running
dev
.
- Build a validation user interface using Admin UI extensions.
- Learn how to use variables in your input query.