@zoxllc/shopify-checkout-extensions 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.ts +0 -1
- package/src/lineItem/DiscountCodeList.ts +0 -91
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export { ConditionalDiscount } from './lineItem/ConditionalDiscount';
|
|
2
|
-
export { DiscountCodeList } from './lineItem/DiscountCodeList';
|
|
3
2
|
export { FixedItemDiscount } from './lineItem/FixedItemDiscount';
|
|
4
3
|
export { PercentageDiscount } from './lineItem/PercentageDiscount';
|
|
5
4
|
export { AndSelector } from './common/AndSelector';
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import type { DiscountCart } from '../common/DiscountCart';
|
|
2
|
-
import type {
|
|
3
|
-
QualifierBehavior,
|
|
4
|
-
Qualifier,
|
|
5
|
-
} from '../common/Qualifier';
|
|
6
|
-
import type { Selector } from '../common/Selector';
|
|
7
|
-
import { Campaign } from '../common/Campaign';
|
|
8
|
-
import type { CartLine, Discount } from '../generated/api';
|
|
9
|
-
import { AndSelector } from '~/common/AndSelector';
|
|
10
|
-
import { OrSelector } from '~/common/OrSelector';
|
|
11
|
-
|
|
12
|
-
export class DiscountCodeList extends Campaign {
|
|
13
|
-
discountList: string[];
|
|
14
|
-
|
|
15
|
-
constructor(
|
|
16
|
-
behavior: QualifierBehavior,
|
|
17
|
-
qualifiers: [Qualifier | AndSelector | OrSelector],
|
|
18
|
-
lineItemSelector?: Selector,
|
|
19
|
-
discountList?: string[]
|
|
20
|
-
) {
|
|
21
|
-
super(behavior, qualifiers);
|
|
22
|
-
this.lineItemSelector = lineItemSelector;
|
|
23
|
-
this.discountList = discountList ? discountList : [];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
run(discountCart: DiscountCart) {
|
|
27
|
-
// First we see if this cart qualifies for this discount
|
|
28
|
-
if (this.qualifies(discountCart)) {
|
|
29
|
-
// Now that it qualifies, we can determine the applicableLineItems
|
|
30
|
-
// to apply discounts to
|
|
31
|
-
|
|
32
|
-
// figure out applicable lineItems...
|
|
33
|
-
const applicableLineItems = discountCart.cart.lines
|
|
34
|
-
.filter((lineItem) => {
|
|
35
|
-
if (this.lineItemSelector === undefined) {
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
return this.lineItemSelector.match(
|
|
39
|
-
lineItem as CartLine
|
|
40
|
-
);
|
|
41
|
-
})
|
|
42
|
-
.sort((a, b) => {
|
|
43
|
-
const priceA = a.cost.amountPerQuantity
|
|
44
|
-
.amount as number;
|
|
45
|
-
const priceB = b.cost.amountPerQuantity
|
|
46
|
-
.amount as number;
|
|
47
|
-
|
|
48
|
-
if (priceA < priceB) return -1;
|
|
49
|
-
if (priceA == priceB) return 0;
|
|
50
|
-
return 1;
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
// Loop over the applicable line items and deduct the itemsToDiscount count
|
|
54
|
-
// as they are applied to not exceed a maximum.
|
|
55
|
-
applicableLineItems.forEach((lineItem) => {
|
|
56
|
-
// If there are no more items to discount, return/skip
|
|
57
|
-
if (this.itemsToDiscount === 0) return;
|
|
58
|
-
|
|
59
|
-
let discount: Discount;
|
|
60
|
-
|
|
61
|
-
// If items to discount is not undefined, apply as many as possible
|
|
62
|
-
if (this.itemsToDiscount !== undefined) {
|
|
63
|
-
// There are more than enough qty to apply, apply the maximum
|
|
64
|
-
if (lineItem.quantity >= this.itemsToDiscount) {
|
|
65
|
-
discount = this.discount.apply(
|
|
66
|
-
lineItem as CartLine,
|
|
67
|
-
this.itemsToDiscount
|
|
68
|
-
);
|
|
69
|
-
this.itemsToDiscount = 0;
|
|
70
|
-
} else {
|
|
71
|
-
discount = this.discount.apply(
|
|
72
|
-
lineItem as CartLine
|
|
73
|
-
);
|
|
74
|
-
this.itemsToDiscount -= lineItem.quantity;
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
// items to discount is undefined, so apply to entire item count
|
|
78
|
-
discount = this.discount.apply(
|
|
79
|
-
lineItem as CartLine
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (discount) {
|
|
84
|
-
// append the discount to the discountCart singleton
|
|
85
|
-
discountCart.addDiscount(discount);
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
return discountCart;
|
|
90
|
-
}
|
|
91
|
-
}
|