Getting started with building location rules
You can use location rules to rank the possible locations for a line item during checkout. In this tutorial series, you'll use Shopify Functions to create a function that prioritizes locations in a particular country.
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.
- Use the order routing settings to create a location rule in a Shopify store.
- 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 extensibility preview enabled.
- You've created an app that uses Shopify CLI 3.0 or higher, or you've migrated your existing app so that it's compatible with Shopify CLI 3.0 or higher.
- You've installed your app on the development store with the checkout extensibility 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 location rule function
Anchor link to section titled "Step 1: Create the location rule function"To create your location rule 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 Rust.
Navigate to your app directory:
Run one of the following commands to create a new location rule extension:
Select the
Rust
template:Navigate to
extensions/location-rule
:Build the function's Wasm module:
If you encounter any errors, then ensure that you've installed Rust and cargo-wasi.
Replace the contents of
input.graphql
file with the following code:input.graphql
defines the input for the function. You need the fulfillment groups, with the available locations for the items in the group.Replace the
/src/main.rs
file with the following code.The function logic sorts the locations by a given metafield.
Deploy your app and function:
A message from the CLI displays and indicates that your
location-rule
function is live.
Step 2: Test the location rule
Anchor link to section titled "Step 2: Test the location rule"You can test your location rule to ensure it's working as expected, and review logs for your function.
- From the Shopify admin, go to Settings > Shipping and delivery.
Click manage in the Order routing section.
Make sure you have at least two locations in your store, and that one of them is in Canada.
Click Add rule.
Your new rule should be an available option.
Add your new rule and drag it to the top to make it your highest priority location rule.
Open your development store, build a cart, and proceed through checkout.
Open your development store's admin, and find your new order. This should be assigned to your Canadian location.
To debug your function, or view its output, you can review its logs in your Partner Dashboard.
- Log in to your Partner Dashboard and navigate to Apps > {your app} > Extensions > order-routing-location-rule.
- Click on any function run to view its input, output, and any logs written to
STDERR
.
- 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.