Create server-side validation with a Cart and Checkout Validation Function
You can use Cart and Checkout Validation Functions to ensure that purchases meet certain criteria before checking out, or completing an order. In this tutorial, you’ll use Shopify Functions to enforce an order maximum for buyers with insufficient order history, preventing them from placing their order.
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:
- Generate starter code for Shopify Functions.
- Use GraphQL to define the input of your function.
- Deploy functions to the Shopify platform.
- Review logs for your function.
Requirements
Anchor link to section titled "Requirements"- You've created a Partner account.
- You've created a development store with the Checkout and Customer Accounts Extensibility developer preview enabled.
You've created an app that uses Shopify CLI 3.49.5 or higher. If you previously installed Shopify CLI, then make sure that you're using the latest version.
If you plan to create a UI for your extension, then start with the Remix app template.
You've installed Node.js 16 or higher.
You've installed your app on the development store with the Checkout and Customer Accounts Extensibility developer preview enabled.
Rust-specific requirements
Anchor link to section titled "Rust-specific requirements"The following requirements are specific to Rust-based development with Shopify Functions.
You've installed Rust.
On Windows, Rust requires the Microsoft C++ Build Tools. Make sure to select the Desktop development with C++ workload when installing the tools.
You've installed cargo-wasi:
Step 1: Create the validation function
Anchor link to section titled "Step 1: Create the validation function"To create your validation function, you can use Shopify CLI to generate a starter function, specify the inputs for your function using an input query, and implement your function logic using Javascript or Rust.
Navigate to your app directory:
Run the following command to create a new validation function:
Choose the language that you want to use. For this tutorial, you should select either Rust or JavaScript.
Shopify defaults to Rust as the most performant and recommended language choice to stay within the platform limits. For more information, refer to language considerations.
Navigate to
extensions/cart-checkout-validation
:Replace the contents of
src/run.graphql
file with the following code.run.graphql
defines the input for the function. You need the customer's order count and current cart subtotal.The query differs slightly in Rust and JavaScript due to code generation requirements.
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 function logic checks for order subtotals greater than a set value and errors when a new customer is detected. You can adjust the subtotal limit or new customer detection logic as needed.
Step 2: Preview the function on a development store
Anchor link to section titled "Step 2: Preview the function on a development store"To test your function, you need to make it available to your development store.
If you're developing a function in a language other than JavaScript or TypeScript, ensure you have configured
build.watch
in your function extension configuration.Navigate back to your app root:
Use the Shopify CLI
dev
command to start app preview:You can keep the preview running as you work on your function. When you make changes to a watched file, Shopify CLI rebuilds your function and updates the function extension's drafts, so you can immediately test your changes.
Follow the CLI prompts to preview your app, and install it on your development store.
Step 3: Activate the validation
Anchor link to section titled "Step 3: Activate the validation"- From the Shopify admin, go to Settings > Checkout.
In the Checkout Rules section of the page click Add rule.
A dialog opens and shows the
cart-checkout-validation
function that you just deployed.To add a validation, click Add rule and select the validation.
Click Activate to activate the validation.
Click on Save.
Optional: Control how checkout behaves when encountering runtime exceptions by clicking on the validation and selecting or deselecting Allow all customers to submit checkout.
Step 4: Test the validation
Anchor link to section titled "Step 4: Test the validation"- From your online store, without logging in, create a cart with more then $1,000 in merchandise.
- Proceed to Checkout and verify that a warning message displays.
- Verify that checkout progress is blocked. Clicking the Continue to shipping button shouldn't redirect the user.
- Using the Storefront API
cartLinesAdd
mutation, confirm that the mutation'suserErrors
field contains the function's error message, and that executing the mutation was unsuccessful. Open your terminal where
shopify app dev
is running, and review your function executions.When testing functions on development stores, the output of
dev
includes executions of your functions, any debug logging you have added to them, and a link to a local file with the full function execution details.In a new terminal window, use the Shopify CLI
app function replay
command to replay a function execution locally, and debug your function without the need to re-trigger the function execution on Shopify.Select the function execution from the top of the list. Press
q
to quit when you are finished debugging.
- Learn more about how Shopify Functions work and the benefits of using Shopify Functions.
- Consult the API references for Shopify Functions.
- Learn how to use variables in your input query.