@ordergroove/offers 2.34.8-alpha-PR-784-7.8 → 2.34.9

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.
@@ -4,18 +4,14 @@ 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, 'USD');
7
+ const formattedPrice = money(price);
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, 'USD');
14
- expect(formattedPrice).toBe('$0.50');
15
- });
16
-
17
- it('Should format JPY values', () => {
18
- expect(money(310000, 'JPY')).toBe('¥3,100');
13
+ const formattedPrice = money(price);
14
+ expect(formattedPrice).toBe('$.50');
19
15
  });
20
16
  });
21
17
 
@@ -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, currency: string) => {
24
- return money(allocation.compare_at_price, currency);
23
+ export const getAllocationRegularPrice = (allocation: ShopifySellingPlanAllocationsEntity) => {
24
+ return money(allocation.compare_at_price);
25
25
  };
26
26
 
27
- export const getAllocationSubscriptionPrice = (allocation: ShopifySellingPlanAllocationsEntity, currency: string) => {
27
+ export const getAllocationSubscriptionPrice = (allocation: ShopifySellingPlanAllocationsEntity) => {
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, currency);
31
+ return money(pricePerShipment);
32
32
  }
33
33
 
34
- return money(allocation.price, currency);
34
+ return money(allocation.price);
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, currency: string) => {
41
+ export const getAllocationDiscountRate = (allocation: ShopifySellingPlanAllocationsEntity) => {
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, currency);
55
+ formatted_discount = money(allocation.price_adjustments[0].value);
56
56
  } else if (allocation.compare_at_price) {
57
- formatted_discount = money(allocation.compare_at_price - allocation.price, currency);
57
+ formatted_discount = money(allocation.compare_at_price - allocation.price);
58
58
  }
59
59
 
60
60
  return formatted_discount;
@@ -70,8 +70,7 @@ export const getAllocationNumberOfShipments = (allocation: ShopifySellingPlanAll
70
70
  export const addPrepaidPriceAndSavings = (
71
71
  allocation: ShopifySellingPlanAllocationsEntity,
72
72
  productPlan: ProductPlanEntity,
73
- payAsYouGoPlan: ShopifySellingPlansEntity,
74
- currency: string
73
+ payAsYouGoPlan: ShopifySellingPlansEntity
75
74
  ) => {
76
75
  const prepaidShipmentsPerBilling = getPrepaidShipmentsNumberFromOptions(allocation.selling_plan?.options);
77
76
  const pricePerShipment = allocation.price / prepaidShipmentsPerBilling;
@@ -81,9 +80,9 @@ export const addPrepaidPriceAndSavings = (
81
80
  const payAsYouGoPercentage =
82
81
  payAsYouGoAdjustment && payAsYouGoAdjustment.value_type === 'percentage' ? payAsYouGoAdjustment.value : null;
83
82
 
84
- productPlan['regularPrepaidPrice'] = money(allocation.price, currency);
85
- productPlan['prepaidSavingsPerShipment'] = money(Math.round(prepaidSaving), currency);
86
- productPlan['prepaidSavingsTotal'] = money(Math.round(prepaidSaving * prepaidShipmentsPerBilling), currency);
83
+ productPlan['regularPrepaidPrice'] = money(allocation.price);
84
+ productPlan['prepaidSavingsPerShipment'] = money(Math.round(prepaidSaving));
85
+ productPlan['prepaidSavingsTotal'] = money(Math.round(prepaidSaving * prepaidShipmentsPerBilling));
87
86
 
88
87
  if (payAsYouGoPercentage && prepaidPercentageSavings) {
89
88
  productPlan['prepaidExtraSavingsPercentage'] = percentage(prepaidPercentageSavings - payAsYouGoPercentage);
@@ -96,8 +95,7 @@ export const DEFAULT_PAY_AS_YOU_GO_GROUP_NAME = 'Subscribe and Save';
96
95
 
97
96
  export const mapSellingPlanToDiscount = (
98
97
  allocation: ShopifySellingPlanAllocationsEntity,
99
- sellingPlans: ShopifySellingPlansEntity[],
100
- currency: string
98
+ sellingPlans: ShopifySellingPlansEntity[] = []
101
99
  ) => {
102
100
  if (!allocation.selling_plan) {
103
101
  allocation.selling_plan = sellingPlans.find(plan => plan.id === allocation.selling_plan_id);
@@ -105,9 +103,9 @@ export const mapSellingPlanToDiscount = (
105
103
 
106
104
  const productPlan: ProductPlanEntity = {
107
105
  frequency: getAllocationFrequency(allocation),
108
- regularPrice: getAllocationRegularPrice(allocation, currency),
109
- subscriptionPrice: getAllocationSubscriptionPrice(allocation, currency),
110
- discountRate: getAllocationDiscountRate(allocation, currency),
106
+ regularPrice: getAllocationRegularPrice(allocation),
107
+ subscriptionPrice: getAllocationSubscriptionPrice(allocation),
108
+ discountRate: getAllocationDiscountRate(allocation),
111
109
  prepaidShipments: getAllocationNumberOfShipments(allocation)
112
110
  };
113
111
 
@@ -115,7 +113,7 @@ export const mapSellingPlanToDiscount = (
115
113
  const payAsYouGoPlan = sellingPlans.find(
116
114
  plan => plan.group_name === DEFAULT_PAY_AS_YOU_GO_GROUP_NAME && plan.options.length === 1
117
115
  );
118
- return addPrepaidPriceAndSavings(allocation, productPlan, payAsYouGoPlan, currency);
116
+ return addPrepaidPriceAndSavings(allocation, productPlan, payAsYouGoPlan);
119
117
  }
120
118
 
121
119
  return productPlan;
@@ -124,9 +122,8 @@ export const mapSellingPlanToDiscount = (
124
122
  export const sellingPlanAllocationsReducer = (
125
123
  acc: ProductPlanEntity[],
126
124
  cur: ShopifySellingPlanAllocationsEntity,
127
- sellingPlans: ShopifySellingPlansEntity[] = [],
128
- currency: string
129
- ) => [...acc, mapSellingPlanToDiscount(cur, sellingPlans, currency)];
125
+ sellingPlans: ShopifySellingPlansEntity[] = []
126
+ ) => [...acc, mapSellingPlanToDiscount(cur, sellingPlans)];
130
127
 
131
128
  export const getSellingPlans = (product: ShopifyProductEntity) =>
132
129
  product.selling_plan_groups.reduce<ShopifySellingPlansEntity[]>(
@@ -45,15 +45,7 @@ declare global {
45
45
  previewMode: boolean;
46
46
  };
47
47
  ogShopifyConfig: OgShopifyConfig;
48
- Shopify?: {
49
- routes?: {
50
- root: string;
51
- };
52
- currency?: {
53
- active: string;
54
- rate: string;
55
- };
56
- };
48
+ Shopify: { routes?: { root: string } };
57
49
  }
58
50
  }
59
51
 
@@ -28,7 +28,6 @@ const PRODUCTS_URL = `${SHOPIFY_ROOT}products/`;
28
28
  type SetupProductPayload = {
29
29
  product: ShopifyProductEntity;
30
30
  offer: any;
31
- currency: string;
32
31
  };
33
32
 
34
33
  type SetupCartPayload = ShopifyCart;
@@ -48,21 +47,12 @@ const makeSyncProductId = offer =>
48
47
  }
49
48
  });
50
49
 
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
-
60
50
  async function setupPdp(store, offer) {
61
51
  const handle = guessProductHandle(offer);
62
52
  if (handle) {
63
53
  try {
64
- const [product, currency] = await Promise.all([getProduct(handle), getCurrency()]);
65
- const payload: SetupProductPayload = { product, offer, currency: currency };
54
+ const product = await getProduct(handle);
55
+ const payload: SetupProductPayload = { product, offer };
66
56
  store.dispatch({ type: SETUP_PRODUCT, payload });
67
57
  } catch (err) {
68
58
  console.warn('OG: Unable to fetch product details for PDP', err);
@@ -162,7 +152,7 @@ async function setupCart(store, offer) {
162
152
 
163
153
  const products = await Promise.all(Array.from(new Set(items.map(({ handle }) => handle))).map(getProduct));
164
154
  products.forEach(product => {
165
- const payload: SetupProductPayload = { product, offer, currency: cart.currency };
155
+ const payload: SetupProductPayload = { product, offer };
166
156
  store.dispatch({ type: SETUP_PRODUCT, payload });
167
157
  });
168
158
  }
@@ -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, currency }
443
+ payload: { product }
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, currency),
454
+ (accumulator, current) => sellingPlanAllocationsReducer(accumulator, current, sellingPlans),
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, [], cart.currency)
470
+ [cur.key]: sellingPlanAllocationsReducer([], cur.selling_plan_allocation)
471
471
  }
472
472
  : acc,
473
473
  state
@@ -116,7 +116,7 @@ export interface ShopifyCart {
116
116
 
117
117
  interface CartItem {
118
118
  id: number;
119
- properties: unknown;
119
+ properties: Record<string, string>;
120
120
  quantity: number;
121
121
  variant_id: number;
122
122
  key: string;
@@ -184,7 +184,7 @@ interface DiscountApplication {
184
184
  type: string;
185
185
  key: string;
186
186
  title: string;
187
- description: any;
187
+ description: string;
188
188
  value: string;
189
189
  created_at: string;
190
190
  value_type: string;
@@ -196,7 +196,7 @@ interface DiscountApplication {
196
196
 
197
197
  interface QuantityRule {
198
198
  min: number;
199
- max: any;
199
+ max?: number;
200
200
  increment: number;
201
201
  }
202
202
 
@@ -1,9 +1,3 @@
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);
1
+ export const money = val => (val === null ? '' : `$${val.toString().replace(/(\d\d)$/, '.$1')}`);
8
2
 
9
3
  export const percentage = val => `${val}%`;