Skip to main content

Build a discount extension

Learn how to develop a scaffolded POS UI extension into a simple discount extension.

This tutorial shows you how to use the pos.home.tile.render and pos.home.modal.render extension targets to build a simple POS UI extension that quickly applies discounts to items in the cart.

You'll develop the scaffolded extension into a smart grid tile that becomes enabled when the cart reaches a total value. When tapped, the tile opens a modal that presents buttons representing available discounts. When tapped, these buttons apply a discount to the cart and present a toast for notification of success.

POS UI Extensions Discount Example App

In this tutorial, you'll learn how to do the following tasks:

  • Enable or disable the tile based on cart contents.
  • Add buttons to the modal that apply a discount and show a toast when tapped.
  • Give your extension a useful description.
  • Deploy your extension to Shopify, create a version, and publish.

The sample code imports some extension components to build the UI. The tile contains the logic to control its enablement based on the cart subtotal. The modal contains the logic to apply discounts based on button tap.

You can copy and paste the following code into your index file. You can also update your extension's configuration file following the shopify.extension.toml example.

The rest of the tutorial walks through this sample code step-by-step.

Discount extension

import {render} from 'preact';
import {useState} from 'preact/hooks';

export default async () => {
render(<Extension />, document.body);
};

const Extension = () => {
const shouldDisable = (subtotal) => {
return Number(subtotal) < 100;
};

// You can use the current cart value to set up state
const [disabled, setDisabled] = useState(
shouldDisable(shopify.cart.current.value.subtotal),
);

// You can subscribe to changes in the cart to mutate state
shopify.cart.current.subscribe((cart) => {
setDisabled(shouldDisable(cart.subtotal));
});

return (
<s-tile
heading="Discount Example App"
disabled={disabled}
onClick={() => shopify.action.presentModal()}
/>
);
};

Anchor to step-1-enable-the-tileStep 1: Enable or disable the tile based on cart contents

You can enable or disable the tile based on cart contents by accessing its cart subscribable. In the tile code, initialize state based on the current value using cart.current.value.

Enable the tile based on cart contents

JSX

const [enabled, setEnabled] = useState(
shouldEnable(shopify.cart.current.value.subtotal),
);

Anchor to step-2-subscribe-to-cartStep 2: Subscribe to cart changes

In the tile code, subscribe to cart changes and mutate state based on the updated cart.

Subscribe to cart changes

JSX

shopify.cart.current.subscribe((cart) => {
setEnabled(shouldEnable(cart.subtotal));
});

Anchor to step-3-add-buttonsStep 3: Add buttons to the modal that apply a discount and display a toast when tapped

You can add buttons to the modal that trigger some action on press.

Create the buttons on the modal. Note that most components belong in a ScrollView.

Add buttons to the modal

JSX

<s-scroll-view>
<s-button onClick={() => onButtonPress('Percentage', '25% off', '25')}>
25%
</s-button>
<s-button onClick={() => onButtonPress('FixedAmount', '$10 off', '10')}>
$10
</s-button>
</s-scroll-view>

Anchor to step-4-define-onpressStep 4: Define onPress

Define an onPress function to apply the discount and show the toast.

Define onPress

JSX

const onButtonPress = (type, title, amount) => {
shopify.cart.applyCartDiscount(type, title, amount);
shopify.toast.show('Discount applied');
};

Anchor to step-5-give-descriptionStep 5: Give your extension a useful description

Your extension's description will be visible to the merchant when they discover and add it to their POS. Extension description When you generate a POS UI Extension from Shopify CLI, the extension description defaults to the name of the extension. You can update the description in the generated toml file (shopify.extension.toml).

Note

name is an internal value that is visible in the Partner Dashboard as the title of an extension on the page that displays the extensions list.

Description configuration

shopify.extension.toml

...
name = "Loyalty discount"
handle = "Loyalty discount"
description = "Add loyalty discount"
...

Anchor to step-6-deploy-releaseStep 6: Deploy and release

Refer to Deploy app extensions for more information.