Skip to content

Commit 459dd2a

Browse files
committed
Add examples for localized discounts tutorial
1 parent 1380fdb commit 459dd2a

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {render} from 'preact';
2+
3+
export default async () => {
4+
render(<Extension />, document.body);
5+
};
6+
7+
function Extension() {
8+
const {i18n} = shopify;
9+
// [START localization.format-points]
10+
const percentageDiscount = 25.5;
11+
const formattedPercentageDiscount = i18n.formatNumber(percentageDiscount);
12+
// [END localization.format-points]
13+
// [START localization.format-balance]
14+
const fixedDiscountAmount = 10;
15+
const formattedFixedDiscountAmount = i18n.formatCurrency(fixedDiscountAmount, {currency: 'CAD'});
16+
// [END localization.format-balance]
17+
// [START localization.translate-points]
18+
const itemCount = shopify.cart.current.value.lineItems.length;
19+
// [END localization.translate-points]
20+
const onButtonClick = (type, title, amount) => {
21+
shopify.cart.applyCartDiscount(type, title, amount);
22+
// [START localization.translate-balance]
23+
shopify.toast.show(i18n.translate('discountApplied'));
24+
// [END localization.translate-balance]
25+
};
26+
27+
return (
28+
// [START localization.build-ui]
29+
<s-page heading={i18n.translate('modalTitle')}>
30+
<s-scroll-box>
31+
<s-stack gap="base">
32+
<s-text>
33+
{i18n.translate('itemCount', {count: itemCount})}
34+
</s-text>
35+
<s-button onClick={() => onButtonClick('Percentage', '25% off', '25')}>
36+
{formattedPercentageDiscount}%
37+
</s-button>
38+
<s-button onClick={() => onButtonClick('FixedAmount', '$10 off', '10')}>
39+
{formattedFixedDiscountAmount}
40+
</s-button>
41+
</s-stack>
42+
</s-scroll-box>
43+
</s-page>
44+
);
45+
}
46+
// [END localization.build-ui]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {render} from 'preact';
2+
import {useState, useEffect} from 'preact/hooks';
3+
4+
export default async () => {
5+
render(<Extension />, document.body);
6+
}
7+
8+
function Extension() {
9+
const {i18n} = shopify;
10+
11+
const shouldDisable = (subtotal) => {
12+
return Number(subtotal) <= 100;
13+
};
14+
15+
const [disabled, setDisabled] = useState(shouldDisable(shopify.cart.current.value.subtotal));
16+
17+
useEffect(() => {
18+
const unsubscribe = shopify.cart.current.subscribe((cart) => {
19+
setDisabled(shouldDisable(cart.subtotal));
20+
});
21+
return unsubscribe;
22+
}, []);
23+
24+
return (
25+
<s-tile
26+
heading={i18n.translate('name')}
27+
subheading={i18n.translate('availableDiscounts')}
28+
onClick={() => shopify.action.presentModal()}
29+
disabled={disabled}
30+
/>
31+
);
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "Discount",
3+
"itemCount": {
4+
"zero": "No items in cart",
5+
"one": "{{count}} item in cart",
6+
"other": "{{count}} items in cart"
7+
},
8+
"availableDiscounts": "View discount options",
9+
"discountApplied": "Discount applied!",
10+
"modalTitle": "Choose a discount"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "Réduction",
3+
"itemCount": {
4+
"zero": "Aucun article dans le panier",
5+
"one": "{{count}} article dans le panier",
6+
"other": "{{count}} articles dans le panier"
7+
},
8+
"availableDiscounts": "Voir les options de réduction",
9+
"discountApplied": "Réduction appliquée!",
10+
"modalTitle": "Choisir une réduction"
11+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
api_version = "2025-10"
2+
3+
[[extensions]]
4+
type = "ui_extension"
5+
# Change the merchant-facing name of the extension in locales/en.default.json
6+
name = "t:name"
7+
uid = "11fd348b-1de3-4793-6892-baa416fc3df29b4377b5"
8+
handle = "discount"
9+
description = "Add discount"
10+
11+
# module: file that contains your extension’s source code
12+
# target: location where your extension appears in POS
13+
# [START config.setup-targets]
14+
[[extensions.targeting]]
15+
module = "./src/Tile.jsx"
16+
target = "pos.home.tile.render"
17+
18+
[[extensions.targeting]]
19+
module = "./src/Modal.jsx"
20+
target = "pos.home.modal.render"
21+
# [END config.setup-targets]

0 commit comments

Comments
 (0)