Tag: shopify-checkout

  • Shopify Checkout UI Extension Example

    As you might have already read on the Shopify website, checkout.liquid is deprecated and stores need to upgrade to Checkout Extensibility by August 13, 2024. So here is an example of how to build a Checkout UI extension.

    For this example, our requirement is going to be to provide a free item (e.g. t-shirt) on checkout when the total purchase is greater than a thousand. Clear enough?

    Checkout UI Extension Setup

    If you have read my past blogs then you probably already have a development store and partner account. In any case, these are the prerequisites:

    • A Shopify Partner account
    • A development store that use the Checkout and Customer Accounts Extensibiliy
    • Shopify CLI
    Shopify Checkout UI Extension Example – Create Store

    This example was created on a Windows 11 machine with Node v20.11.0, npm v10.2.4 and Shopify CLI v3.60.1. For the final check, on the bottom left of your Shopify Admin page, it should say Checkout and Customer Accounts Extensibility as shown below.

    Shopify Checkout UI Extension Example – Checkout Extensibility

    Checkout UI Extension Scaffolding

    Super simple, run shopify app init to create you project. You can choose what name you like for your project. In this example, our project name is checkout-practice-app

    C:\shopify>shopify app init
    
    Welcome. Letโ€™s get started by naming your app project. You can change it later.
    
    ?  Your project name?
    โˆš  checkout-practice-app
    
    ?  Get started building your app:
    โˆš  Start by adding your first extension
    
    โ•ญโ”€ info โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚                                                                                โ”‚
    โ”‚  Initializing project with `npm`                                               โ”‚
    โ”‚  Use the `--package-manager` flag to select a different package manager.       โ”‚
    โ”‚                                                                                โ”‚
    โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
    
    
    โ•ญโ”€ success โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚                                                                                โ”‚
    โ”‚  checkout-practice-app is ready for you to build!                              โ”‚
    โ”‚                                                                                โ”‚
    โ”‚  Next steps                                                                    โ”‚
    โ”‚    โ€ข Run `cd checkout-practice-app`                                            โ”‚
    โ”‚    โ€ข For extensions, run `shopify app generate extension`                      โ”‚
    โ”‚    โ€ข To see your app, run `shopify app dev`                                    โ”‚
    โ”‚                                                                                โ”‚
    โ”‚  Reference                                                                     โ”‚
    โ”‚    โ€ข Shopify docs [1]                                                          โ”‚
    โ”‚    โ€ข For an overview of commands, run `shopify app --help`                     โ”‚
    โ”‚                                                                                โ”‚
    โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
    [1] https://shopify.dev

    Checkout UI Extension App

    Next step is to create the checkout UI extension. This is the code that will modify the checkout page. Change to you project directory and run npm run generate extension. You can choose what technology stack you’ld like to work in. I have chosen JavaScript React for this example.

    C:\shopify\checkout-practice-app>npm run generate extension
    
    > checkout-practice-app@1.0.0 generate
    > shopify app generate extension
    
    
    Before proceeding, your project needs to be associated with an app.
    
    ?  Create this project as a new app on Shopify?
    โˆš  Yes, create it as a new app
    
    ?  App name:
    โˆš  checkout-practice-app
    
    โ•ญโ”€ info โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚                                                                                โ”‚
    โ”‚  Using shopify.app.toml:                                                       โ”‚
    โ”‚                                                                                โ”‚
    โ”‚    โ€ข Org:             Joel Patrick Llosa                                       โ”‚
    โ”‚    โ€ข App:             checkout-practice-app                                    โ”‚
    โ”‚                                                                                โ”‚
    โ”‚   You can pass `--reset` to your command to reset your app configuration.      โ”‚
    โ”‚                                                                                โ”‚
    โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
    
    ?  Type of extension?
    โˆš  Checkout UI
    
    ?  Name your extension:
    โˆš  free-shirt-1000
    
    ?  What would you like to work in?
    โˆš  JavaScript React
    
    
    โ•ญโ”€ success โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚                                                                                โ”‚
    โ”‚  Your extension was created in extensions/free-shirt-1000.                     โ”‚
    โ”‚                                                                                โ”‚
    โ”‚  Next steps                                                                    โ”‚
    โ”‚    โ€ข To preview this extension along with the rest of the project, run `npm    โ”‚
    โ”‚      run shopify app dev`                                                      โ”‚
    โ”‚                                                                                โ”‚
    โ”‚  Reference                                                                     โ”‚
    โ”‚    โ€ข For more details, see the docs [1]                                        โ”‚
    โ”‚                                                                                โ”‚
    โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
    [1] https://shopify.dev/api/checkout-extensions/checkout/configuration

    Take a look at your Shopify Partners portal page and your app should be there.

    Shopify Checkout UI Extension Example – App Created

    Checkout UI Extension Logic

    Checkout.jsx

    import {
      BlockLayout,
      InlineLayout,
      Text,
      useTranslate,
      reactExtension,
      useSettings,
      useSubtotalAmount,
    } from '@shopify/ui-extensions-react/checkout';
    
    export default reactExtension(
      'purchase.checkout.cart-line-list.render-after',
      () => <Extension />,
    );
    
    function Extension() {
      const translate = useTranslate();
      const { freebie_title: freeItem } = useSettings();
      const { amount } = useSubtotalAmount();
    
      if (freeItem && amount > 1000) {
        return (
          <InlineLayout columns={['fill', '20%']}>
              <Text>{ freeItem }</Text>
              <BlockLayout inlineAlignment="end">
                  <Text>Free</Text>
              </BlockLayout>
          </InlineLayout>
        );
      }  
    }

    What does this piece of code do? This UI extension targets purchase.checkout.cart-line-list.render-after. Which means this UI will be inserted at that target location. It will be rendered after all line items.

    Moving along, we use 3 APIs provided by Shopify. The useTranslate hook returns the I18nTranslate interface used to translate strings. We can do something like &lt;Text&gt;{translate('welcome')}&lt;/Text&gt; which pulls the welcome message in en.default.json and renders it in a Text component. But we won’t be using it in this example.

    The useSettings hook returns the settings values defined by the merchant for the extension. These settings values are found on shopify.extension.toml. The value of the key freebie_title is then assigned to freeItem. We can set the value in the customize mode of the checkout page which is driven by the below extension settings.

    shopify.extension.toml

    ...snipped...
    
    [extensions.settings]
      [[extensions.settings.fields]]
        key = "freebie_title"
        type = "single_line_text_field"
        name = "Freebie title"
        description = "Free item name"

    The useSubtotalAmount API returns a Money value representing the subtotal value of the items in the cart at the current step of checkout. Obviously we’ll need the amount to check if it more than a thousand so we can render the free item. So if there is a free item set and the sub total is more than a thousand we render the free item using Shopify provided React components. This ends the coding part.

    Running and Customizing the App

    Time to run the app. Execute npm run dev and follow the instructions (e.g. “P” to preview in your browser).

    C:\shopify\checkout-practice-app>npm run dev
    
    > checkout-practice-app@1.0.0 dev
    > shopify app dev
    
    ?  Which store would you like to use to view your project?
    โˆš  checkout-practice
    
    โ•ญโ”€ info โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
    โ”‚                                                                                โ”‚
    โ”‚  Using shopify.app.toml:                                                       โ”‚
    โ”‚                                                                                โ”‚
    โ”‚    โ€ข Org:             Joel Patrick Llosa                                       โ”‚
    โ”‚    โ€ข App:             checkout-practice-app                                    โ”‚
    โ”‚    โ€ข Dev store:       checkout-practice.myshopify.com                          โ”‚
    โ”‚    โ€ข Update URLs:     Not yet configured                                       โ”‚
    โ”‚                                                                                โ”‚
    โ”‚   You can pass `--reset` to your command to reset your app configuration.      โ”‚
    โ”‚                                                                                โ”‚
    โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
    
    โœ” Created extension free-shirt-1000.
    07:43:25 โ”‚ graphiql   โ”‚ GraphiQL server started on port 3457
    07:43:25 โ”‚ extensions โ”‚ Bundling UI extension free-shirt-1000...
    07:43:25 โ”‚ extensions โ”‚ Parsed locales for extension free-shirt-1000 at
    C:/shopify/checkout-practice-app/extensions/free-shirt-1000
    07:43:25 โ”‚ extensions โ”‚ free-shirt-1000 successfully built
    07:43:25 โ”‚ extensions โ”‚ Parsed locales for extension free-shirt-1000 at
    C:/shopify/checkout-practice-app/extensions/free-shirt-1000
    07:43:27 โ”‚ extensions โ”‚ Draft updated successfully for extension: free-shirt-1000
    
    โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    โ€บ Press d โ”‚ toggle development store preview: โœ” on
    โ€บ Press g โ”‚ open GraphiQL (Admin API) in your browser
    โ€บ Press p โ”‚ preview in your browser
    โ€บ Press q โ”‚ quit
    
    Preview URL: https://damaged-breakfast-campus-syria.trycloudflare.com/extensions/dev-console
    GraphiQL URL: http://localhost:3457/graphiql

    Before we can see our app in action, we need to do some bits and bobs. Go to Settings > Checkout > Customize to add the app block.

    Shopify Checkout UI Extension Example – Customize Checkout

    Add app block. Can you see the free-shirt-1000 extension?

    Shopify Checkout UI Extension Example – free-shirt-1000

    App block added. Can you see it below the line items?

    Shopify Checkout UI Extension Example – App Block Added

    App block settings. Does it remind you of the extension settings in shopify.extension.toml? We’re giving away an Acme Brand T-shirt for purchases over a thousand.

    Shopify Checkout UI Extension Example – App Block Settings

    Now, go purchase something over a thousand and go to checkout. You should have something like below.

    Shopify Checkout UI Extension Example – Over a Thousand Purchase

    Try buying something that’s below a thousand and go to checkout. You are not getting a free item.

    Shopify Checkout UI Extension – Under a Thousand Purchase

    Shopify Checkout UI Extension Summary

    There you have it. Your first Shopify Checkout UI Extension. To recap, create the scaffolding using Shopify CLI. After that is in place generate an extension. Choose a target location in the checkout page and add your custom logic. Last bit is doing some bit of config to render your UI extension. Good luck creating your own Shopify Checkout UI Extension in the future. Grab the repo here, github.com/jpllosa/checkout-practice-app.

    More Shopify Checkout UI examples…