Format address suggestions in checkout
You can format the selected address suggestion provided by the purchase.address-autocomplete.format-suggestion
extension target. The formatted address is used to auto-populate the fields of the address form. This target is used in conjunction with the purchase.address-autocomplete.suggest
target to provide address suggestions from an external service when the database doesn't return a formatted address.
In this tutorial, you'll use checkout UI extensions to format the suggested addresses returned by the address autocomplete extension you built in the Customize the address autocomplete provider tutorial.
Checkout UI extensions are available only to Shopify Plus merchants.
Anchor to What you'll learnWhat you'll learn
In this tutorial, you'll learn how to do the following:
- Format the selected address suggestion provided by the
purchase.address-autocomplete.format-suggestion
extension target. - Auto-populate the fields of the shipping address form when a customer selects an address suggestion using an external address service.
- Deploy your checkout UI extension to Shopify.
Requirements
-
You've created a Partner account.
-
You've created a new development store with the following:
- Generated test data
- Checkout and Customer Accounts Extensibility developer preview enabled
Complete the customize the address autocomplete provider tutorial to set up the address autocomplete extension. This tutorial builds on the address autocomplete extension created in the previous tutorial.
Project
Anchor to Create a second checkout UI extensionCreate a second checkout UI extension
Set up the second target for your checkout UI extension.
The purchase.address-autocomplete.format-suggestion
extension target must be used in the same extension as the purchase.address-autocomplete.suggest
target.
Anchor to Update the address autocomplete ,[object Object], codeUpdate the address autocomplete suggest.js
code
suggest.js
codeUpdate the suggest.js
file in the src
directory of your address autocomplete extension that you created in the previous tutorial with this tutorial's suggest.js
code.
Anchor to Add the ,[object Object], target to your address autocomplete extensionAdd the format-suggestion
target to your address autocomplete extension
format-suggestion
target to your address autocomplete extension-
Create a new file called
format-suggestion.js
in thesrc
directory of your extension that you created in the previous tutorial. -
Import the
extension
function from the@shopify/ui-extensions/checkout
library. -
Set the extension target to
purchase.address-autocomplete.format-suggestion
. -
Export this extension so it can be referenced in your configuration.
Anchor to Update the extension configuration fileUpdate the extension configuration file
-
Update the
shopify.extension.toml
file to include theformat-suggestion
target. -
Restart the server to apply the changes:
Terminal
Anchor to Format the selected addressFormat the selected address
Use the purchase.address-autocomplete.format-suggestion
extension target to format the selected address suggestion provided by the address autocomplete extension.
Anchor to Extract the attribute from the targetExtract the attribute from the target
In the format-suggestion.ts
file, extract the selectedSuggestion
attribute provided by the target. This is the autocomplete suggestion that the buyer selected during checkout.
Anchor to Fetch an address from an external serviceFetch an address from an external service
Use the selectedSuggestion
attribute to fetch the formatted address from the external address service. Return the formatted address to the checkout UI extension.
Anchor to Preview the extensionPreview the extension
Preview your extension to make sure that it works as expected.
Anchor to Start your serverStart your server
Run the Shopify CLI dev
command to build your app and preview it on your development store.
Make sure that you select a development store that has enabled the developer preview for Checkout and Customer Accounts Extensibility.
-
In a terminal, navigate to your app directory.
-
Either start or restart your server to build and preview your app:
Terminal
shopify app dev -
Press
p
to open the developer console. -
In the developer console page, click on the preview link for the custom format suggestion extension.
The checkout opens.
This section describes how to solve some potential errors when you run dev
for an app that contains a checkout UI extension.
Anchor to Property token errorProperty 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 Shopify CLI to open a checkout session for you.
Terminal
Anchor to Missing checkout linkMissing 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:
Terminal
shopify app info
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:Terminal
shopify app dev -
On the checkout page for your store, change the URL by appending the
?dev=https://{tunnel_url}/extensions
query string and reload the page. Thetunnel_url
parameter allows your app to be accessed using a unique HTTPS URL.You should now see a rendered order note that corresponds to the code in your project template.
Anchor to In the Checkout and Accounts EditorIn the Checkout and Accounts Editor
Preview your extension in the Checkout and Accounts Editor:
- Navigate to the Address autocompletion section in the Settings tab of the checkout and accounts editor.
- Select your address autocomplete app.
- Enter a house number and street name in the address field of the delivery address (for example, "1 White House").
- Select an address suggestion in the dropdown (for example, “1 White House, Huntington NY 11743”). The delivery address fields are automatically updated.
When you're ready to release your changes to users, you can create and release an app version. An app version is a snapshot of your app configuration and all extensions.
-
Navigate to your app directory.
-
Run the following command.
Optionally, you can provide a name or message for the version using the
--version
and--message
flags.Terminal
shopify app deploy
Releasing an app version replaces the current active version that's served to stores that have your app installed. It might take several minutes for app users to be upgraded to the new version.
If you want to create a version, but avoid releasing it to users, then run the deploy
command with a --no-release
flag.
You can release the unreleased app version using Shopify CLI's release
command, or through the Partner Dashboard.
Anchor to Tutorial complete!Tutorial complete!
Congratulations! You formatted the address autocomplete suggestions. Keep the momentum going with these related tutorials and resources.