Getting started with building a discounts experience
You can create a new type of discount that's applied to a particular product or product variant in the cart. In this tutorial, you'll use Shopify Functions to create a new discount type called "Volume discount" that offers a percentage off when customers purchase more than the minimum quantity of a product.
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.
You've created an app that uses Shopify CLI 3.49.5 or higher. If you previously installed Shopify CLI, then make sure 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.
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 product discount function
Anchor link to section titled "Step 1: Create the product discount function"To create your product discount 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 one of the following commands to create a new product discount extension:
Choose the language that you want to use. For this tutorial, you should select either JavaScript or Rust.
Navigate to
extensions/product-discount
:Replace the contents of
src/run.graphql
file with the following code.run.graphql
defines the input for the function target. The query differs slightly in JavaScript and Rust 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.js
orsrc/run.rs
file with the following code.This function logic applies a 10% discount to any product variant cart lines with a quantity of two or more.
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 this 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: Update your app access scopes
Anchor link to section titled "Step 3: Update your app access scopes"You must request the write_discounts
access scope to give your app the permissions it needs to create and update discounts.
In
shopify.app.toml
, located in the root of your app, add thewrite_discounts
scope:Push your updated configuration to Shopify:
Restart your app:
Use the URL provided by the CLI to open or re-install your app. You should be prompted to accept the new access scope permissions in your store.
Step 4: Create the product discount with GraphiQL
Anchor link to section titled "Step 4: Create the product discount with GraphiQL"To activate your function, you must create a product discount on the store where you installed your app. You can do this using the discountAutomaticAppCreate
or discountCodeAppCreate
GraphQL mutations.
In subsequent tutorials, you'll use metafields on this product discount to configure your function, and create an interface so that users can configure the function themselves.
Install the Shopify GraphiQL app on your store. If you've already installed GraphiQL, then you should do so again to select the necessary access scopes for discounts.
In the GraphiQL app, in the API Version field, select the 2023-07 version.
Find the ID of your function by executing the following query:
The result contains a node with your function's ID:
Execute the following mutation and replace
YOUR_FUNCTION_ID_HERE
with the ID of your function:You should receive a GraphQL response that includes the ID of the created product discount. If the response includes any messages under
userErrors
, then review the errors, check that your mutation andfunctionId
are correct, and try the request again.
Step 5: Test the product discount
Anchor link to section titled "Step 5: Test the product discount"From your Shopify admin, go to Discounts.
You should now see the Volume discount that you created. Deactivate or delete all other discounts to ensure that the Volume discount is the only active discount.
Open your development store and build a cart with a single item in it.
No discounts should be applied to the cart.
Increase the item quantity to two.
The 10% Volume discount should now be applied to the cart.
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 > product-discount.
- Click on any function run to view its input, output, and any logs written to
STDERR
.
- Add configuration to your discounts experience using metafields.