Time to build: ~1 hour
Before starting this tutorial you need the following:
- A Shopify Partners account
- A development store
Post-purchase upsell applications are a great way for merchants to increase sales by presenting shoppers with a one-click option to add additional items to their purchases. There are countless different strategies to pick between when determining what products a merchant wants to push for an upsell opportunity: it might be products that are related or frequently bought together with the products that the customer has already bought, it could be an item that the merchant is particularly interested in selling, or it could just be an item that is on sale.
The logic required to determine what products are presented to users is the interesting part of a post-purchase upsell application. But to get to the point where you are writing this business logic code, you need to go through the process of setting up a server and ensuring that it will be able to scale to handle potentially high traffic.
Using Gadget, this tutorial shows you how to skip the boilerplate server work and brings you directly to writing your post-purchase upsell logic.
You can fork this Gadget project and try it out yourself.
You still need to set up the Shopify Post-purchase Extension, add environment variables, and complete the application frontend.
Good news! Shopify already has a tutorial that covers setting up a new frontend application using the Shopify App Bridge Extension.
You should go through the entire first page of their tutorial. It will help you set up a front-end application on a development store and allows you to inject a post-purchase upsell page into your checkout workflow.
The index.jsx code file that Shopify creates may require an additional import statement! If you see any React errors in your web console, adding import React from "react"; at the top of your code file should be the fix!
When running your app locally, you need to start your app with a Cloudflare tunnel like so: yarn dev --tunnel-url https://my-tunnel-url:port. You must include a port, and the default Cloudflare tunnel port is 8080.
Once your Shopify CLI app is up and running, Shopify gives you a button in your store Admin to add test products to your store automatically. Add some products!
You should also make sure your products are enabled and visible in your store, and that some products are being offered at a discounted price. To offer some products at a discount, fill in the Compare at price and Price fields on a product. You can also add some test products manually.
Do not proceed until you have the frontend of Shopify's post-purchase extension demo working!
Once you have completed the first portion of the Shopify tutorial, move on to the Upsell example. Your Gadget app will replace the Node server that you set up in Part 2: Build an app server.
This Gadget application will sync product data with your development store. This product data will be used along with and custom routing endpoints to determine what product will be offered to a shopper.
You will want the development store's Product Variant data in your Gadget app, so you need to set up a new Shopify Connection.
To complete this connection, you will need a Shopify Partners account as well as a store or development store
Our first step is going to be setting up a custom Shopify application in the Partners dashboard.
Both the Shopify store admin and the Shopify Partner Dashboard have an Apps section. Ensure that you are on the Shopify Partner Dashboard before continuing.
Now we get to select what Shopify scopes we give our application access to, while also picking what Shopify data models we want to import into our Gadget app.
You require the Product Read and Write scopes, and want to import the Product, Product Image, and Product Variant models.
We have successfully created our connection!
Shopify's upsell tutorial has you set up a local Node server to test out the post-purchase upsell frontend. You will replace this server with a Gadget app!
There are 3 things you need to add to your Gadget app to get it up and running:
The first thing you want to do is set up the /offer route. This route will accept a payload from the frontend app, determine what product will be presented as part of the post-purchase upsell, and then return the product variant information required to render on the frontend.
Go to the Gadget file explorer and add a new file in the routes folder.
You can then rename the auto-generated file to POST-offer.js. Custom routes in Gadget need to be prepended with one of the accepted Fastify request types. The remainder of the file name is the path of the route! You can read more information about creating custom routes in the Gadget documentation.
You are going to pass information from the post-purchase Shopify App Bridge Extension to our Gadget app. This information includes the line items the customer is purchasing, as well as the total price of the order. You can use this information to determine what kind of offer you want to present to the customer. For this tutorial, you will select a random product variant that is not included as part of the original purchase and is being discounted to offer the customer.
Paste the following code snippet into your POST-offer.js file.
This file uses the Gadget API to call shopifyProductVariant.findMany() and then applies a filter condition to get product variants that are not included in the original purchase.
Notice that the Gadget API is available through the request parameter.
You then select a random variant and return it to the frontend. This is the business logic code. You can replace this code when writing a custom post-purchase upsell application.
If the customer chooses to purchase the product presented to them in the post-purchase upsell window, you need to modify the original order. Another route is needed to authenticate this transaction - Shopify requires a signed JWT to proceed. It is best to handle this with another custom route.
Create another new file in our routes folder and call it POST-sign-changeset.js.
Paste the following code snippet to respond to requests to this endpoint with a signed JWT. This code is almost identical to the server example provided in the Shopify tutorial.
This will allow the original order to be modified successfully. But first, you need to import the jsonwebtoken and uuid modules and provide the SHOPIFY_API_KEY and SHOPIFY_API_SECRET environment variables required by our snippet.
To be able to run this snippet you need to import some modules in the package.json file: jsonwebtoken and uuid. To load these modules in your Gadget application, open the package.json file in the file explorer and add them as dependencies:
Click the Run yarn button in the top right of package.json window to import the packages.
The status page that opens will let you know when the yarn install process is complete.
To add environment variables in Gadget, go to Settings -> Environment variables in the navigation bar.
The SHOPIFY_API_KEY and SHOPIFY_API_SECRET can be found in your Shopify Partners Dashboard. Make sure to grab the keys for the custom frontend application you set up, not the keys generated for your Gadget app!
Our POST-sign-changeset.js snippet is already set up to read environment variables. You can access them in Gadget the same way you would access them in any other Node project: process.env.<ENVIRONMENT_VARIABLE_NAME>.
Those are the only two routes required for a post-purchase upsell app! Now you just need to enable cross-origin requests so that our tunnelled local frontend can retrieve information from our Gadget app.
Gadget's custom routes are built on top of Fastify. This means you can use Fastify plugins such as fastify/cors to customize your routing. You can read more about extending route functionality in Gadget in our documentation.
To add the fastify-cors module to our Gadget application, open the package.json file and add it as a dependency: "fastify-cors": "^6.0.3".
Click the Run yarn button in the top right of package.json window to import the package.
Now you need to add a file that handles the injection of Fastify plugins into our router. Create a new file in the routes folder and call it +scope.js. Then paste the following snippet in the file:
This allows for cross-origin requests from Shopify, our backend Gadget app should now be reachable!
Need a different flavour of CORS handling for your custom app? Check out our docs for more details on how you can handle CORS settings.
Your Gadget file explorer should now look like this:
Our Gadget app is finished! Now you can continue with the Shopify tutorial starting back at Step 3: Update the extension code or copy and paste the snippet below to finish setting up our application extension frontend.
If you don't copy and paste the above snippet, there are tweaks you need to make to the provided Shopify frontend code:
And you're done, congrats!
If you simulate a fake purchase in our Shopify store, you should now be redirected to the post-purchase upsell screen before our purchase summary. And choosing to purchase the offered product will add it to the order.
You can use this as a template for writing your own post-purchase upsell application. All you need to do is replace the business logic in the POST-offer.js file with your custom code!