Build a location rule function
You can use location rules to rank the possible locations for a line item during checkout. In this tutorial, 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"Refer to Build location rules.
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 the following command to create a new location rule extension:
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/location-rule
:Replace the contents of
src/run.graphql
file with the following code:run.graphql
defines the input for the function. You need the fulfillment groups, with the available locations for the items in the group.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.The function logic sorts the locations in your store, putting locations in Canada first.
If you're using Rust, then build the function's Wasm module:
If you encounter any errors, then ensure that you've installed Rust and cargo-wasi.
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: Test the location rule
Anchor link to section titled "Step 3: Test the location rule"You can test your location rule to ensure it's working as expected, and review logs for your function.
Before you test the location rule, make sure that you have the following:
- Two locations in your store in different countries, one of them in Canada.
- One product that is stocked on multiple locations.
- From the Shopify admin, go to Settings > Shipping and delivery.
- In the Order routing section, click Manage.
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.
Add products to your cart. At least one product should be stocked at multiple locations, including your Canadian location.
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
.
- Add configuration to your location rules using metafields.