@ordergroove/offers 2.34.9 → 2.35.0
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/CHANGELOG.md +11 -0
- package/dist/bundle-report.html +11 -11
- package/dist/offers.js +26 -26
- package/dist/offers.js.map +3 -3
- package/package.json +2 -2
- package/src/shopify/__tests__/productPlan.spec.js +13 -13
- package/src/shopify/__tests__/shopifyReducer.spec.js +417 -350
- package/src/shopify/__tests__/utils.spec.js +7 -3
- package/src/shopify/reducers/productPlans.ts +22 -19
- package/src/shopify/shopifyBootstrap.ts +9 -1
- package/src/shopify/shopifyMiddleware.ts +13 -3
- package/src/shopify/shopifyReducer.js +3 -3
- package/src/shopify/utils.ts +7 -1
|
@@ -4,14 +4,18 @@ describe('Shopify Utils', () => {
|
|
|
4
4
|
describe('Money', () => {
|
|
5
5
|
it('Should return formatted greater than $1 money price', () => {
|
|
6
6
|
const price = 1000;
|
|
7
|
-
const formattedPrice = money(price);
|
|
7
|
+
const formattedPrice = money(price, 'USD');
|
|
8
8
|
expect(formattedPrice).toBe('$10.00');
|
|
9
9
|
});
|
|
10
10
|
|
|
11
11
|
it('Should return formatted lower than $1 money price', () => {
|
|
12
12
|
const price = 50;
|
|
13
|
-
const formattedPrice = money(price);
|
|
14
|
-
expect(formattedPrice).toBe('
|
|
13
|
+
const formattedPrice = money(price, 'USD');
|
|
14
|
+
expect(formattedPrice).toBe('$0.50');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('Should format JPY values', () => {
|
|
18
|
+
expect(money(310000, 'JPY')).toBe('¥3,100');
|
|
15
19
|
});
|
|
16
20
|
});
|
|
17
21
|
|
|
@@ -20,25 +20,25 @@ export const getAllocationFrequency = (allocation: ShopifySellingPlanAllocations
|
|
|
20
20
|
return (allocation.selling_plan_id || (allocation.selling_plan?.id ?? '')).toString();
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
export const getAllocationRegularPrice = (allocation: ShopifySellingPlanAllocationsEntity) => {
|
|
24
|
-
return money(allocation.compare_at_price);
|
|
23
|
+
export const getAllocationRegularPrice = (allocation: ShopifySellingPlanAllocationsEntity, currency: string) => {
|
|
24
|
+
return money(allocation.compare_at_price, currency);
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
-
export const getAllocationSubscriptionPrice = (allocation: ShopifySellingPlanAllocationsEntity) => {
|
|
27
|
+
export const getAllocationSubscriptionPrice = (allocation: ShopifySellingPlanAllocationsEntity, currency: string) => {
|
|
28
28
|
if (isPrepaidAllocation(allocation)) {
|
|
29
29
|
const prepaidShipmentsPerBilling = getPrepaidShipmentsNumberFromOptions(allocation.selling_plan?.options);
|
|
30
30
|
const pricePerShipment = Math.round(allocation.price / prepaidShipmentsPerBilling);
|
|
31
|
-
return money(pricePerShipment);
|
|
31
|
+
return money(pricePerShipment, currency);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
return money(allocation.price);
|
|
34
|
+
return money(allocation.price, currency);
|
|
35
35
|
};
|
|
36
36
|
|
|
37
37
|
const getPrepaidPercentage = (allocation: ShopifySellingPlanAllocationsEntity, pricePerShipment: number) => {
|
|
38
38
|
return Math.round(((allocation.compare_at_price - pricePerShipment) * 100) / allocation.compare_at_price);
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
-
export const getAllocationDiscountRate = (allocation: ShopifySellingPlanAllocationsEntity) => {
|
|
41
|
+
export const getAllocationDiscountRate = (allocation: ShopifySellingPlanAllocationsEntity, currency: string) => {
|
|
42
42
|
if (isPrepaidAllocation(allocation)) {
|
|
43
43
|
const prepaidShipmentsPerBilling = getPrepaidShipmentsNumberFromOptions(allocation.selling_plan?.options);
|
|
44
44
|
const pricePerShipment = allocation.price / prepaidShipmentsPerBilling;
|
|
@@ -52,9 +52,9 @@ export const getAllocationDiscountRate = (allocation: ShopifySellingPlanAllocati
|
|
|
52
52
|
if (allocation.price_adjustments[0]?.value_type === 'percentage') {
|
|
53
53
|
formatted_discount = percentage(allocation.price_adjustments[0].value);
|
|
54
54
|
} else if (allocation.price_adjustments[0]?.value) {
|
|
55
|
-
formatted_discount = money(allocation.price_adjustments[0].value);
|
|
55
|
+
formatted_discount = money(allocation.price_adjustments[0].value, currency);
|
|
56
56
|
} else if (allocation.compare_at_price) {
|
|
57
|
-
formatted_discount = money(allocation.compare_at_price - allocation.price);
|
|
57
|
+
formatted_discount = money(allocation.compare_at_price - allocation.price, currency);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
return formatted_discount;
|
|
@@ -70,7 +70,8 @@ export const getAllocationNumberOfShipments = (allocation: ShopifySellingPlanAll
|
|
|
70
70
|
export const addPrepaidPriceAndSavings = (
|
|
71
71
|
allocation: ShopifySellingPlanAllocationsEntity,
|
|
72
72
|
productPlan: ProductPlanEntity,
|
|
73
|
-
payAsYouGoPlan: ShopifySellingPlansEntity
|
|
73
|
+
payAsYouGoPlan: ShopifySellingPlansEntity,
|
|
74
|
+
currency: string
|
|
74
75
|
) => {
|
|
75
76
|
const prepaidShipmentsPerBilling = getPrepaidShipmentsNumberFromOptions(allocation.selling_plan?.options);
|
|
76
77
|
const pricePerShipment = allocation.price / prepaidShipmentsPerBilling;
|
|
@@ -80,9 +81,9 @@ export const addPrepaidPriceAndSavings = (
|
|
|
80
81
|
const payAsYouGoPercentage =
|
|
81
82
|
payAsYouGoAdjustment && payAsYouGoAdjustment.value_type === 'percentage' ? payAsYouGoAdjustment.value : null;
|
|
82
83
|
|
|
83
|
-
productPlan['regularPrepaidPrice'] = money(allocation.price);
|
|
84
|
-
productPlan['prepaidSavingsPerShipment'] = money(Math.round(prepaidSaving));
|
|
85
|
-
productPlan['prepaidSavingsTotal'] = money(Math.round(prepaidSaving * prepaidShipmentsPerBilling));
|
|
84
|
+
productPlan['regularPrepaidPrice'] = money(allocation.price, currency);
|
|
85
|
+
productPlan['prepaidSavingsPerShipment'] = money(Math.round(prepaidSaving), currency);
|
|
86
|
+
productPlan['prepaidSavingsTotal'] = money(Math.round(prepaidSaving * prepaidShipmentsPerBilling), currency);
|
|
86
87
|
|
|
87
88
|
if (payAsYouGoPercentage && prepaidPercentageSavings) {
|
|
88
89
|
productPlan['prepaidExtraSavingsPercentage'] = percentage(prepaidPercentageSavings - payAsYouGoPercentage);
|
|
@@ -95,7 +96,8 @@ export const DEFAULT_PAY_AS_YOU_GO_GROUP_NAME = 'Subscribe and Save';
|
|
|
95
96
|
|
|
96
97
|
export const mapSellingPlanToDiscount = (
|
|
97
98
|
allocation: ShopifySellingPlanAllocationsEntity,
|
|
98
|
-
sellingPlans: ShopifySellingPlansEntity[]
|
|
99
|
+
sellingPlans: ShopifySellingPlansEntity[],
|
|
100
|
+
currency: string
|
|
99
101
|
) => {
|
|
100
102
|
if (!allocation.selling_plan) {
|
|
101
103
|
allocation.selling_plan = sellingPlans.find(plan => plan.id === allocation.selling_plan_id);
|
|
@@ -103,9 +105,9 @@ export const mapSellingPlanToDiscount = (
|
|
|
103
105
|
|
|
104
106
|
const productPlan: ProductPlanEntity = {
|
|
105
107
|
frequency: getAllocationFrequency(allocation),
|
|
106
|
-
regularPrice: getAllocationRegularPrice(allocation),
|
|
107
|
-
subscriptionPrice: getAllocationSubscriptionPrice(allocation),
|
|
108
|
-
discountRate: getAllocationDiscountRate(allocation),
|
|
108
|
+
regularPrice: getAllocationRegularPrice(allocation, currency),
|
|
109
|
+
subscriptionPrice: getAllocationSubscriptionPrice(allocation, currency),
|
|
110
|
+
discountRate: getAllocationDiscountRate(allocation, currency),
|
|
109
111
|
prepaidShipments: getAllocationNumberOfShipments(allocation)
|
|
110
112
|
};
|
|
111
113
|
|
|
@@ -113,7 +115,7 @@ export const mapSellingPlanToDiscount = (
|
|
|
113
115
|
const payAsYouGoPlan = sellingPlans.find(
|
|
114
116
|
plan => plan.group_name === DEFAULT_PAY_AS_YOU_GO_GROUP_NAME && plan.options.length === 1
|
|
115
117
|
);
|
|
116
|
-
return addPrepaidPriceAndSavings(allocation, productPlan, payAsYouGoPlan);
|
|
118
|
+
return addPrepaidPriceAndSavings(allocation, productPlan, payAsYouGoPlan, currency);
|
|
117
119
|
}
|
|
118
120
|
|
|
119
121
|
return productPlan;
|
|
@@ -122,8 +124,9 @@ export const mapSellingPlanToDiscount = (
|
|
|
122
124
|
export const sellingPlanAllocationsReducer = (
|
|
123
125
|
acc: ProductPlanEntity[],
|
|
124
126
|
cur: ShopifySellingPlanAllocationsEntity,
|
|
125
|
-
sellingPlans: ShopifySellingPlansEntity[] = []
|
|
126
|
-
|
|
127
|
+
sellingPlans: ShopifySellingPlansEntity[] = [],
|
|
128
|
+
currency: string
|
|
129
|
+
) => [...acc, mapSellingPlanToDiscount(cur, sellingPlans, currency)];
|
|
127
130
|
|
|
128
131
|
export const getSellingPlans = (product: ShopifyProductEntity) =>
|
|
129
132
|
product.selling_plan_groups.reduce<ShopifySellingPlansEntity[]>(
|
|
@@ -45,7 +45,15 @@ declare global {
|
|
|
45
45
|
previewMode: boolean;
|
|
46
46
|
};
|
|
47
47
|
ogShopifyConfig: OgShopifyConfig;
|
|
48
|
-
Shopify
|
|
48
|
+
Shopify?: {
|
|
49
|
+
routes?: {
|
|
50
|
+
root: string;
|
|
51
|
+
};
|
|
52
|
+
currency?: {
|
|
53
|
+
active: string;
|
|
54
|
+
rate: string;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
49
57
|
}
|
|
50
58
|
}
|
|
51
59
|
|
|
@@ -28,6 +28,7 @@ const PRODUCTS_URL = `${SHOPIFY_ROOT}products/`;
|
|
|
28
28
|
type SetupProductPayload = {
|
|
29
29
|
product: ShopifyProductEntity;
|
|
30
30
|
offer: any;
|
|
31
|
+
currency: string;
|
|
31
32
|
};
|
|
32
33
|
|
|
33
34
|
type SetupCartPayload = ShopifyCart;
|
|
@@ -47,12 +48,21 @@ const makeSyncProductId = offer =>
|
|
|
47
48
|
}
|
|
48
49
|
});
|
|
49
50
|
|
|
51
|
+
async function getCurrency() {
|
|
52
|
+
const windowCurrency = window.Shopify?.currency?.active;
|
|
53
|
+
if (windowCurrency) {
|
|
54
|
+
return windowCurrency;
|
|
55
|
+
}
|
|
56
|
+
const cart = await getCart();
|
|
57
|
+
return cart.currency;
|
|
58
|
+
}
|
|
59
|
+
|
|
50
60
|
async function setupPdp(store, offer) {
|
|
51
61
|
const handle = guessProductHandle(offer);
|
|
52
62
|
if (handle) {
|
|
53
63
|
try {
|
|
54
|
-
const product = await getProduct(handle);
|
|
55
|
-
const payload: SetupProductPayload = { product, offer };
|
|
64
|
+
const [product, currency] = await Promise.all([getProduct(handle), getCurrency()]);
|
|
65
|
+
const payload: SetupProductPayload = { product, offer, currency: currency };
|
|
56
66
|
store.dispatch({ type: SETUP_PRODUCT, payload });
|
|
57
67
|
} catch (err) {
|
|
58
68
|
console.warn('OG: Unable to fetch product details for PDP', err);
|
|
@@ -152,7 +162,7 @@ async function setupCart(store, offer) {
|
|
|
152
162
|
|
|
153
163
|
const products = await Promise.all(Array.from(new Set(items.map(({ handle }) => handle))).map(getProduct));
|
|
154
164
|
products.forEach(product => {
|
|
155
|
-
const payload: SetupProductPayload = { product, offer };
|
|
165
|
+
const payload: SetupProductPayload = { product, offer, currency: cart.currency };
|
|
156
166
|
store.dispatch({ type: SETUP_PRODUCT, payload });
|
|
157
167
|
});
|
|
158
168
|
}
|
|
@@ -440,7 +440,7 @@ export const productOffer = (state = {}, _action) => state;
|
|
|
440
440
|
export const productPlans = (state = {}, action) => {
|
|
441
441
|
if (constants.SETUP_PRODUCT === action.type) {
|
|
442
442
|
const {
|
|
443
|
-
payload: { product }
|
|
443
|
+
payload: { product, currency }
|
|
444
444
|
} = action;
|
|
445
445
|
|
|
446
446
|
const sellingPlans = getSellingPlans(product);
|
|
@@ -451,7 +451,7 @@ export const productPlans = (state = {}, action) => {
|
|
|
451
451
|
(acc, cur) => ({
|
|
452
452
|
...acc,
|
|
453
453
|
[cur.id]: cur.selling_plan_allocations?.reduce(
|
|
454
|
-
(accumulator, current) => sellingPlanAllocationsReducer(accumulator, current, sellingPlans),
|
|
454
|
+
(accumulator, current) => sellingPlanAllocationsReducer(accumulator, current, sellingPlans, currency),
|
|
455
455
|
[]
|
|
456
456
|
)
|
|
457
457
|
}),
|
|
@@ -467,7 +467,7 @@ export const productPlans = (state = {}, action) => {
|
|
|
467
467
|
cur.selling_plan_allocation
|
|
468
468
|
? {
|
|
469
469
|
...acc,
|
|
470
|
-
[cur.key]: sellingPlanAllocationsReducer([], cur.selling_plan_allocation)
|
|
470
|
+
[cur.key]: sellingPlanAllocationsReducer([], cur.selling_plan_allocation, [], cart.currency)
|
|
471
471
|
}
|
|
472
472
|
: acc,
|
|
473
473
|
state
|
package/src/shopify/utils.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
export const money =
|
|
1
|
+
export const money = (val: number, currency: string) =>
|
|
2
|
+
val === null
|
|
3
|
+
? ''
|
|
4
|
+
: new Intl.NumberFormat(navigator.language, {
|
|
5
|
+
style: 'currency',
|
|
6
|
+
currency
|
|
7
|
+
}).format(val / 100);
|
|
2
8
|
|
|
3
9
|
export const percentage = val => `${val}%`;
|