Skip to content

Commit a6edcf6

Browse files
add new discount and network call templates
1 parent 7fbd371 commit a6edcf6

37 files changed

+16629
-778
lines changed

discounts/javascript/discount/default/schema.graphql

Lines changed: 5172 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
api_version = "unstable"
2+
3+
[[extensions]]
4+
name = "t:name"
5+
handle = "{{handle}}"
6+
type = "function"
7+
{% if uid %}uid = "{{ uid }}"{% endif %}
8+
description = "t:description"
9+
10+
[[extensions.targeting]]
11+
target = "cart.lines.discounts.generate.run"
12+
input_query = "src/generate_cart_run.graphql"
13+
export = "generate-cart-run"
14+
15+
[[extensions.targeting]]
16+
target = "cart.delivery-options.discounts.generate.run"
17+
input_query = "src/generate_delivery_run.graphql"
18+
export = "generate-delivery-run"
19+
20+
[extensions.build]
21+
command = "npm run build"
22+
path = "dist/index.js"
23+
watch = [ "src/**/*.ts", "src/**/*.tsx" ]
24+
25+
[extensions.ui]
26+
handle = "{{handle}}"
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
{%- if flavor contains "vanilla-js" -%}
2+
// @ts-check
3+
/**
4+
* @typedef {import("../generated/api").CartInput} CartInput
5+
* @typedef {import("../generated/api").FunctionCartRunResult} FunctionCartRunResult
6+
* @typedef {import("../generated/api").OrderDiscountSelectionStrategy} OrderDiscountSelectionStrategy
7+
* @typedef {import("../generated/api").ProductDiscountSelectionStrategy} ProductDiscountSelectionStrategy
8+
*/
9+
/**
10+
* generateCartRun
11+
* @param {CartInput} input - The CartInput
12+
* @returns {FunctionCartRunResult} - The function result with discounts.
13+
*/
14+
export function generateCartRun(input) {
15+
if (!input.cart.lines.length) {
16+
throw new Error('No cart lines found');
17+
}
18+
const maxCartLine = input.cart.lines.reduce((maxLine, line) => {
19+
if (line.cost.subtotalAmount > maxLine.cost.subtotalAmount) {
20+
return line;
21+
}
22+
return maxLine;
23+
}, input.cart.lines[0]);
24+
return {
25+
operations: [
26+
{
27+
addOrderDiscounts: {
28+
candidates: [
29+
{
30+
message: '10% OFF ORDER',
31+
targets: [
32+
{
33+
orderSubtotal: {
34+
excludedCartLineIds: [],
35+
},
36+
},
37+
],
38+
value: {
39+
percentage: {
40+
value: 10,
41+
},
42+
},
43+
},
44+
],
45+
selectionStrategy: OrderDiscountSelectionStrategy.First,
46+
},
47+
},
48+
{
49+
addProductDiscounts: {
50+
candidates: [
51+
{
52+
message: '20% OFF PRODUCT',
53+
targets: [
54+
{
55+
cartLine: {
56+
id: maxCartLine.id,
57+
},
58+
},
59+
],
60+
value: {
61+
percentage: {
62+
value: 20,
63+
},
64+
},
65+
},
66+
],
67+
selectionStrategy: ProductDiscountSelectionStrategy.First,
68+
},
69+
},
70+
],
71+
};
72+
}
73+
{%- elsif flavor contains "typescript" -%}
74+
import {
75+
OrderDiscountSelectionStrategy,
76+
ProductDiscountSelectionStrategy,
77+
CartInput,
78+
FunctionCartRunResult,
79+
} from '../generated/api';
80+
81+
export function generateCartRun(input: CartInput): FunctionCartRunResult {
82+
if (!input.cart.lines.length) {
83+
throw new Error('No cart lines found');
84+
}
85+
86+
const maxCartLine = input.cart.lines.reduce((maxLine, line) => {
87+
if (line.cost.subtotalAmount > maxLine.cost.subtotalAmount) {
88+
return line;
89+
}
90+
return maxLine;
91+
}, input.cart.lines[0]);
92+
93+
return {
94+
operations: [
95+
{
96+
addOrderDiscounts: {
97+
candidates: [
98+
{
99+
message: '10% OFF ORDER',
100+
targets: [
101+
{
102+
orderSubtotal: {
103+
excludedCartLineIds: [],
104+
},
105+
},
106+
],
107+
value: {
108+
percentage: {
109+
value: 10,
110+
},
111+
},
112+
},
113+
],
114+
selectionStrategy: OrderDiscountSelectionStrategy.First,
115+
},
116+
},
117+
{
118+
addProductDiscounts: {
119+
candidates: [
120+
{
121+
message: '20% OFF PRODUCT',
122+
targets: [
123+
{
124+
cartLine: {
125+
id: maxCartLine.id,
126+
},
127+
},
128+
],
129+
value: {
130+
percentage: {
131+
value: 20,
132+
},
133+
},
134+
},
135+
],
136+
selectionStrategy: ProductDiscountSelectionStrategy.First,
137+
},
138+
},
139+
],
140+
};
141+
}
142+
{%- endif -%}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{%- if flavor contains "vanilla-js" -%}
2+
// @ts-check
3+
/**
4+
* @typedef {import("../generated/api").CartInput} DeliveryInput
5+
* @typedef {import("../generated/api").FunctionCartRunResult} FunctionDeliveryRunResult
6+
* @typedef {import("../generated/api").OrderDiscountSelectionStrategy} DeliveryDiscountSelectionStrategy
7+
*/
8+
/**
9+
* generateCartRun
10+
* @param {DeliveryInput} input - The DeliveryInput
11+
* @returns {FunctionDeliveryRunResult} - The function result with discounts.
12+
*/
13+
export function generateDeliveryRun(input) {
14+
const firstDeliveryGroup = input.cart.deliveryGroups[0];
15+
if (!firstDeliveryGroup) {
16+
throw new Error('No delivery groups found');
17+
}
18+
return {
19+
operations: [
20+
{
21+
addDeliveryDiscounts: {
22+
candidates: [
23+
{
24+
message: 'FREE DELIVERY',
25+
targets: [
26+
{
27+
deliveryGroup: {
28+
id: firstDeliveryGroup.id,
29+
},
30+
},
31+
],
32+
value: {
33+
percentage: {
34+
value: 100,
35+
},
36+
},
37+
},
38+
],
39+
selectionStrategy: DeliveryDiscountSelectionStrategy.All,
40+
},
41+
},
42+
],
43+
};
44+
}
45+
{%- elsif flavor contains "typescript" -%}
46+
import {
47+
DeliveryDiscountSelectionStrategy,
48+
DeliveryInput,
49+
FunctionDeliveryRunResult,
50+
} from '../generated/api';
51+
52+
export function generateDeliveryRun(
53+
input: DeliveryInput,
54+
): FunctionDeliveryRunResult {
55+
const firstDeliveryGroup = input.cart.deliveryGroups[0];
56+
if (!firstDeliveryGroup) {
57+
throw new Error('No delivery groups found');
58+
}
59+
60+
return {
61+
operations: [
62+
{
63+
addDeliveryDiscounts: {
64+
candidates: [
65+
{
66+
message: 'FREE DELIVERY',
67+
targets: [
68+
{
69+
deliveryGroup: {
70+
id: firstDeliveryGroup.id,
71+
},
72+
},
73+
],
74+
value: {
75+
percentage: {
76+
value: 100,
77+
},
78+
},
79+
},
80+
],
81+
selectionStrategy: DeliveryDiscountSelectionStrategy.All,
82+
},
83+
},
84+
],
85+
};
86+
}
87+
{%- endif -%}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{%- if flavor contains "vanilla-js" -%}
2+
export { generateCartRun } from './generate_cart_run';
3+
export { generateDeliveryRun } from './generate_delivery_run';
4+
{%- elsif flavor contains "typescript" -%}
5+
export {generateCartRun} from './generate_cart_run';
6+
export {generateDeliveryRun} from './generate_delivery_run';
7+
{%- endif -%}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
dist/
3+
.env
4+
*.log

0 commit comments

Comments
 (0)