Skip to main content

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.

Shopify Plus

Checkout UI extensions are available only to Shopify Plus merchants.

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

Complete customize the address autocomplete provider tutorial

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

Update 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

  1. Create a new file called format-suggestion.js in the src directory of your extension that you created in the previous tutorial.

  2. Import the extension function from the @shopify/ui-extensions/checkout library.

  3. Set the extension target to purchase.address-autocomplete.format-suggestion.

  4. Export this extension so it can be referenced in your configuration.


Anchor to Update the extension configuration fileUpdate the extension configuration file

  1. Update the shopify.extension.toml file to include the format-suggestion target.

  2. Restart the server to apply the changes:

Terminal

shopify app dev

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.

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.

  1. In a terminal, navigate to your app directory.

  2. Either start or restart your server to build and preview your app:

    Terminal

    shopify app dev
  3. Press p to open the developer console.

  4. In the developer console page, click on the preview link for the custom format suggestion extension.

    The checkout opens.

Note
This extension is non-rendering and won't appear during on your checkout

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

shopify app dev --checkout-cart-url cart/{product_variant_id}:{quantity}

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:

  1. From your development store's storefront, add some products to your cart.

  2. From the cart, click Checkout.

  3. From directory of the app that contains your extension, run dev to preview your app:

    Terminal

    shopify app dev
  4. On the checkout page for your store, change the URL by appending the ?dev=https://{tunnel_url}/extensions query string and reload the page. The tunnel_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:

  1. Navigate to the Address autocompletion section in the Settings tab of the checkout and accounts editor.
  2. Select your address autocomplete app.
  3. Enter a house number and street name in the address field of the delivery address (for example, "1 White House").
  4. 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.

  1. Navigate to your app directory.

  2. 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.

Tip

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.

Congratulations! You formatted the address autocomplete suggestions. Keep the momentum going with these related tutorials and resources.

Was this page helpful?