Getting started with pre-purchase product offers
In this tutorial, you'll use the Checkout::Dynamic::Render
extension point to build a pre-purchase upsell offer that prompts the customer to add a product to their order. You'll also create a banner to display when the customer adds the offered 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:
Create a checkout UI extension that renders some text in the checkout flow.
Run the extension locally and test it on a development store.
Show an initial loading state for the products to be offered.
Fetch cart lines for the product offers from the Storefront API, to display to customers.
Filter the cart lines to determine which to display for the product offer.
Add a cart line to an order.
Show an error banner if there's a problem adding a cart line to an order.
Deploy your extension code to Shopify.
Requirements
Anchor link to section titled "Requirements"You've created a Partner account.
You've created a new development store, where you've enabled the checkout extensibility developer preview.
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.
Sample code
Anchor link to section titled "Sample code"The sample code imports some checkout components to build the UI. The code fetches products for the offer using query
. The applyCartLinesChange
API adds a cart line to the order when a customer clicks the button to add the product.
You can copy and paste the following code into your index
file. You'll also need to update your extension's configuration file following the extension.config.toml
example.
The rest of the tutorial walks through this sample code step-by-step.
Step 1: Create a UI extension
Anchor link to section titled "Step 1: Create a UI extension"To create a checkout UI extension, you can use Shopify CLI, which generates starter code for building your extension and automates common development tasks.
Navigate to your app directory:
Run one of the following commands to create a new checkout UI extension:
Select a language for your extension. You can choose from TypeScript, JavaScript, TypeScript React, or JavaScript React.
You should now have a new extension directory in your app's directory. The extension directory includes the extension script at
src/index.{file-extension}
. The following is an example directory structure:Start your development server to build and preview your app:
To learn about the processes that are executed when you run
dev
, refer to the Shopify CLI command reference.Press
p
to open the developer console. In the developer console page, click on the preview link for your extension.
Step 2: Import components
Anchor link to section titled "Step 2: Import components"In the index
file, import the components from the Checkout UI extensions API that you need to build the extension:
Step 3: Set up the extension point
Anchor link to section titled "Step 3: Set up the extension point"The Checkout::Dynamic::Render
extension point enables merchants to control what placement the extension is rendered in checkout. In this example, you'll use the ORDER_SUMMARY4
placement so that the extension appears after the order summary.
Step 4: Fetch products for the offer
Anchor link to section titled "Step 4: Fetch products for the offer"Now that you've set up the extension point, you'll fetch products so that you can display them to customers for the product offer.
Set up a function that handles retrieving your products. You'll query the Storefront API
products
resource. Shopify also provides a Storefront API Learning Kit that offers some useful examples for using the Storefront API.If you need to use
fetch
instead ofquery
, then you can use the following examples. For more details on when to usefetch
instead ofquery
, refer to the checkout UI extension configuration documentation.Show a loading state UI while fetching the products using the
SkeletonText
andSkeletonImage
components.
Step 5: Select which products to offer
Anchor link to section titled "Step 5: Select which products to offer"In this step, you'll pick a product to offer, while making sure you aren’t offering a product that’s already in the cart.
Step 6: Set up the logic to display an error banner
Anchor link to section titled "Step 6: Set up the logic to display an error banner"If there is an error adding a product on offer, you'll want to update the UI to inform the user. To do this, you'll use the Banner
component.
Step 7: Render the components
Anchor link to section titled "Step 7: Render the components"In this step, you'll render your product on offer and use the onPress
property to apply the cart lines change if customers click on the product offer and handle any errors.
Step 8: Deploy the UI extension
Anchor link to section titled "Step 8: Deploy the UI extension"When you're ready, you can deploy your extension code to Shopify.
Navigate to your app directory.
Run one of the following commands:
To learn about the processes that are executed when you run
deploy
, refer to the Shopify CLI command reference.
Troubleshooting
Anchor link to section titled "Troubleshooting"This section describes how to solve some potential errors when you run dev
for an app that contains a checkout UI extension.
Property token error
Anchor link to section titled "Property token error"If you receive the error ShopifyCLI:AdminAPI requires the property token to be set
, then you'll need to use the --checkout-cart-url
flag to direct the CLI to open a checkout session for you.
Missing checkout link
Anchor link to section titled "Missing checkout link"If you don't receive the test checkout URL when you run dev
, then verify the following:
You have a development store populated with products.
You're logged in to the correct Partners organization and development store. To verify, check your app info using the following command:
Otherwise, you can manually create a checkout with the following steps:
From your development store's storefront, add some products to your cart.
From the cart, click Checkout.
From directory of the app that contains your extension, run
dev
to preview your app:On the checkout page for your store, change the URL by appending the
?dev=https://{tunnel_url}/extensions
query string and reload the page.You should now see a rendered order note that corresponds to the code in your project template.
- Get familiar with the UX guidelines for adding pre-purchase product offers to checkout.
- Learn about the components that are available in checkout UI extensions.
Consult the API reference for checkout UI extension points and their respective types.
Learn how to release and publish a version of your checkout UI extension.