Skip to content

Commit f347f17

Browse files
Merge pull request #615 from Shopify/04-30-update_to_include_discount_class_filtering
Update to include discount class filtering
2 parents dcebf24 + d6c611a commit f347f17

40 files changed

+489
-11367
lines changed

discounts/javascript/discount/default/src/generate_cart_run.graphql.liquid

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ query CartInput {
99
}
1010
}
1111
}
12+
discount {
13+
discountClasses
14+
}
1215
}

discounts/javascript/discount/default/src/generate_cart_run.liquid

Lines changed: 122 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// @ts-check
33

44
import {
5+
DiscountClass,
56
OrderDiscountSelectionStrategy,
67
ProductDiscountSelectionStrategy,
78
} from '../generated/api';
@@ -20,63 +21,85 @@ export function generateCartRun(input) {
2021
if (!input.cart.lines.length) {
2122
throw new Error('No cart lines found');
2223
}
24+
25+
const hasOrderDiscountClass = input.discount.discountClasses.includes(
26+
DiscountClass.Order
27+
);
28+
29+
const hasProductDiscountClass = input.discount.discountClasses.includes(
30+
DiscountClass.Product
31+
);
32+
33+
if (!hasOrderDiscountClass && !hasProductDiscountClass) {
34+
return { operations: [] };
35+
}
36+
2337
const maxCartLine = input.cart.lines.reduce((maxLine, line) => {
2438
if (line.cost.subtotalAmount.amount > maxLine.cost.subtotalAmount.amount) {
2539
return line;
2640
}
2741
return maxLine;
2842
}, input.cart.lines[0]);
29-
return {
30-
operations: [
31-
{
32-
orderDiscountsAdd: {
33-
candidates: [
34-
{
35-
message: '10% OFF ORDER',
36-
targets: [
37-
{
38-
orderSubtotal: {
39-
excludedCartLineIds: [],
40-
},
41-
},
42-
],
43-
value: {
44-
percentage: {
45-
value: 10,
43+
44+
const operations = [];
45+
46+
if(hasOrderDiscountClass) {
47+
operations.push({
48+
orderDiscountsAdd: {
49+
candidates: [
50+
{
51+
message: '10% OFF ORDER',
52+
targets: [
53+
{
54+
orderSubtotal: {
55+
excludedCartLineIds: [],
4656
},
4757
},
58+
],
59+
value: {
60+
percentage: {
61+
value: 10,
62+
},
4863
},
49-
],
50-
selectionStrategy: OrderDiscountSelectionStrategy.First,
51-
},
64+
},
65+
],
66+
selectionStrategy: OrderDiscountSelectionStrategy.First,
5267
},
53-
{
54-
productDiscountsAdd: {
55-
candidates: [
56-
{
57-
message: '20% OFF PRODUCT',
58-
targets: [
59-
{
60-
cartLine: {
61-
id: maxCartLine.id,
62-
},
63-
},
64-
],
65-
value: {
66-
percentage: {
67-
value: 20,
68+
});
69+
}
70+
71+
if (hasProductDiscountClass) {
72+
operations.push({
73+
productDiscountsAdd: {
74+
candidates: [
75+
{
76+
message: '20% OFF PRODUCT',
77+
targets: [
78+
{
79+
cartLine: {
80+
id: maxCartLine.id,
6881
},
6982
},
83+
],
84+
value: {
85+
percentage: {
86+
value: 20,
87+
},
7088
},
71-
],
72-
selectionStrategy: ProductDiscountSelectionStrategy.First,
73-
},
89+
},
90+
],
91+
selectionStrategy: ProductDiscountSelectionStrategy.First,
7492
},
75-
],
93+
});
94+
}
95+
96+
return {
97+
operations,
7698
};
7799
}
78100
{%- elsif flavor contains "typescript" -%}
79101
import {
102+
DiscountClass,
80103
OrderDiscountSelectionStrategy,
81104
ProductDiscountSelectionStrategy,
82105
CartInput,
@@ -88,60 +111,80 @@ export function generateCartRun(input: CartInput): CartLinesDiscountsGenerateRun
88111
throw new Error('No cart lines found');
89112
}
90113

114+
const hasOrderDiscountClass = input.discount.discountClasses.includes(
115+
DiscountClass.Order
116+
);
117+
118+
const hasProductDiscountClass = input.discount.discountClasses.includes(
119+
DiscountClass.Product
120+
);
121+
122+
if (!hasOrderDiscountClass && !hasProductDiscountClass) {
123+
return { operations: [] };
124+
}
125+
91126
const maxCartLine = input.cart.lines.reduce((maxLine, line) => {
92127
if (line.cost.subtotalAmount.amount > maxLine.cost.subtotalAmount.amount) {
93128
return line;
94129
}
95130
return maxLine;
96131
}, input.cart.lines[0]);
97132

98-
return {
99-
operations: [
100-
{
101-
orderDiscountsAdd: {
102-
candidates: [
103-
{
104-
message: '10% OFF ORDER',
105-
targets: [
106-
{
107-
orderSubtotal: {
108-
excludedCartLineIds: [],
109-
},
110-
},
111-
],
112-
value: {
113-
percentage: {
114-
value: 10,
133+
const operations: CartLinesDiscountsGenerateRunResult['operations'] = [];
134+
135+
if (hasOrderDiscountClass) {
136+
operations.push({
137+
orderDiscountsAdd: {
138+
candidates: [
139+
{
140+
message: '10% OFF ORDER',
141+
targets: [
142+
{
143+
orderSubtotal: {
144+
excludedCartLineIds: [],
115145
},
116146
},
147+
],
148+
value: {
149+
percentage: {
150+
value: 10,
151+
},
117152
},
118-
],
119-
selectionStrategy: OrderDiscountSelectionStrategy.First,
120-
},
121-
},
122-
{
123-
productDiscountsAdd: {
124-
candidates: [
125-
{
126-
message: '20% OFF PRODUCT',
127-
targets: [
128-
{
129-
cartLine: {
130-
id: maxCartLine.id,
131-
},
132-
},
133-
],
134-
value: {
135-
percentage: {
136-
value: 20,
153+
},
154+
],
155+
selectionStrategy: OrderDiscountSelectionStrategy.First,
156+
}
157+
});
158+
}
159+
160+
if (hasProductDiscountClass) {
161+
operations.push({
162+
productDiscountsAdd: {
163+
candidates: [
164+
{
165+
message: '20% OFF PRODUCT',
166+
targets: [
167+
{
168+
cartLine: {
169+
id: maxCartLine.id,
137170
},
138171
},
172+
],
173+
value: {
174+
percentage: {
175+
value: 20,
176+
},
139177
},
140-
],
141-
selectionStrategy: ProductDiscountSelectionStrategy.First,
142-
},
178+
},
179+
],
180+
selectionStrategy: ProductDiscountSelectionStrategy.First,
143181
},
144-
],
182+
});
183+
};
184+
185+
return {
186+
operations,
145187
};
146188
}
189+
147190
{%- endif -%}

0 commit comments

Comments
 (0)