Getting started with custom fields
In this tutorial, you'll use the Checkout::ShippingMethods::RenderAfter
extension point, checkout UI components, and UI extensions APIs to create a custom field for collecting delivery instructions from customers, and then save those instructions to a metafield.
This tutorial is for delivery instructions, but you can use it as the basis for building other use cases for custom fields.
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.
Render an optional input field for customers to add a note.
Save the note to a metafield.
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.
You're familiar with how custom fields work.
Sample code
Anchor link to section titled "Sample code"The sample code imports several checkout components, including BlockStack
, Checkbox
, and TextField
to compose the user interface that renders at the Checkout::ShippingMethods::RenderAfter
extension point. The code uses the applyMetafieldChange
extension point API to update a metafield with a customer value.
You can copy and paste the following code into your index
file and add a few example values to get the extension to render in the browser.
You'll also need to update your extension's configuration file with the information in the code 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::ShippingMethods::RenderAfter
extension point renders the extension after the shipping methods section on the shipping step of the checkout.
Step 4: Set up a metafield
Anchor link to section titled "Step 4: Set up a metafield"Now that you've set up the extension point, you'll save custom fields using public metafields. Public metafields are available to any app or extension.
Access the metafield and set that as your initial value, and use
applyMetafieldsChange()
to update and persist that value.
Step 5: Render the note box and set the value
Anchor link to section titled "Step 5: Render the note box and set the value"In this step, you'll render a checkbox that toggles an input where customers can add a note, which is then saved to a metafield
. The Checkbox
and TextField
components display the offer to the customer.
To set the note value, you'll make a request to the applyMetafieldChange
extension point API with the key
and namespace
values for the metafield.
Step 6: Show the note in the Shopify admin
Anchor link to section titled "Step 6: Show the note in the Shopify admin"After you've saved the note metafield to the order, display it on the order details page in the Shopify admin so that merchants can view it.
Add a metafield definition for Orders. Use the same namespace
and key
as defined in the extension config file.
Your extension in the Shopify admin should now look similar to the following image:
Step 7: Deploy the UI extension
Anchor link to section titled "Step 7: 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 custom fields 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.