@lana-commerce/core 14.0.0-alpha.15 → 14.0.0-alpha.16

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 CHANGED
@@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning][semantic versioning].
11
11
  - OAuth APIs of "customer" module were changed in backwards incompatible way. To delete oauth provider you need to specify its type. This is due to the fact that you can connect multiple providers now.
12
12
  - Do not export "parseFormat" from "formatToJSX".
13
13
  - Use new data format for "slate" module. Bringing "slate" lib based editor up to date with upstream.
14
+ - New variant pricing model using pricing rules.
14
15
 
15
16
  ### Minor Changes
16
17
 
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = "fragment CommonCartVariant on StorefrontProductVariant{barcode country_of_origin created_at currency_prices{...CurrencyPrice}discountable expiration_days fulfillment_service grams height hs_code id media_files{...MediaFile}linked_media_files{...MediaFile}inventory{available low_stock locations{available low_stock inventory_location{id}}}inventory_policy is_default length map_price max_downloads options{...VariantOption}position price retail_price sale_price sale_price_eligible sale_price_from sale_price_to sku tax_class taxable type updated_at width customer_group_price favorite{id created_at}tiered_pricing{...TieredPricing}}\n";
3
+ exports.default = "fragment CommonCartVariant on StorefrontProductVariant{barcode country_of_origin created_at currency_prices{...CurrencyPrice}discountable expiration_days fulfillment_service grams height hs_code id media_files{...MediaFile}linked_media_files{...MediaFile}inventory{available low_stock locations{available low_stock inventory_location{id}}}inventory_policy is_default length map_price max_downloads options{...VariantOption}position price retail_price sku tax_class taxable type updated_at width favorite{id created_at}tiered_pricing{...TieredPricing}previous_pricing{price retail_price map_price currency_prices{...CurrencyPrice}tiered_pricing{...TieredPricing}}}\n";
@@ -1,3 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = "fragment CurrencyPrice on CurrencyPrice{currency map_price price retail_price sale_price tiered_pricing{...TieredPricing}}\n";
3
+ exports.default = "fragment CurrencyPrice on CurrencyPrice{currency map_price price retail_price tiered_pricing{...TieredPricing}}\n";
@@ -14,7 +14,6 @@ function applyDiscountPreview(pi, quantity, dp) {
14
14
  const iprice = lprice / quantity;
15
15
  return {
16
16
  sale: pi.sale,
17
- hide: pi.hide,
18
17
  kind: "price_discount",
19
18
  lineItemPrice: lprice,
20
19
  perItemPrice: iprice,
@@ -24,71 +23,52 @@ function applyDiscountPreview(pi, quantity, dp) {
24
23
  };
25
24
  }
26
25
  function lineItemPriceInfoPreDiscountPreview(vpi, quantity, tieredQuantity) {
27
- if (vpi.tieredPricing && vpi.tieredPricing.tiers.length > 0) {
28
- // tiered pricing
29
- if (vpi.sale) {
30
- const prevLPrice = (0, tieredPrice_js_1.tieredPrice)(vpi.tieredPricing, vpi.previousPrice, quantity, tieredQuantity);
31
- const prevIPrice = Math.round(prevLPrice / quantity);
26
+ if (vpi.previousPrice === 0 && vpi.tieredPricing && vpi.tieredPricing.tiers.length > 0) {
27
+ // previous price is not provided and tiered pricing is defined, let's use original price as previous price
28
+ const tieredLPrice = (0, tieredPrice_js_1.tieredPrice)(vpi.tieredPricing, vpi.price, quantity, tieredQuantity);
29
+ const tieredIPrice = Math.round(tieredLPrice / quantity);
30
+ if (tieredLPrice !== vpi.price * quantity) {
32
31
  return {
33
- sale: vpi.sale,
34
- hide: vpi.hide,
32
+ sale: false,
35
33
  kind: "price_discount",
36
- lineItemPrice: vpi.price * quantity,
37
- perItemPrice: vpi.price,
38
- previousLineItemPrice: prevLPrice,
39
- previousPerItemPrice: prevIPrice,
40
- percentageOff: (0, percentageOff_js_1.percentageOff)(prevLPrice, vpi.price * quantity),
34
+ lineItemPrice: tieredLPrice,
35
+ perItemPrice: tieredIPrice,
36
+ previousLineItemPrice: vpi.price * quantity,
37
+ previousPerItemPrice: vpi.price,
38
+ percentageOff: (0, percentageOff_js_1.percentageOff)(vpi.price * quantity, tieredLPrice),
41
39
  };
42
40
  }
43
41
  else {
44
- const tieredLPrice = (0, tieredPrice_js_1.tieredPrice)(vpi.tieredPricing, vpi.price, quantity, tieredQuantity);
45
- const tieredIPrice = Math.round(tieredLPrice / quantity);
46
- if (tieredLPrice !== vpi.price * quantity) {
47
- return {
48
- sale: vpi.sale,
49
- hide: vpi.hide,
50
- kind: "price_discount",
51
- lineItemPrice: tieredLPrice,
52
- perItemPrice: tieredIPrice,
53
- previousLineItemPrice: vpi.price * quantity,
54
- previousPerItemPrice: vpi.price,
55
- percentageOff: (0, percentageOff_js_1.percentageOff)(vpi.price * quantity, tieredLPrice),
56
- };
57
- }
58
- else {
59
- return {
60
- sale: vpi.sale,
61
- hide: vpi.hide,
62
- kind: "price",
63
- lineItemPrice: vpi.price * quantity,
64
- perItemPrice: vpi.price,
65
- };
66
- }
67
- }
68
- }
69
- else {
70
- // normal pricing
71
- if (vpi.previousPrice !== 0 && vpi.previousPrice !== vpi.price) {
72
42
  return {
73
- sale: vpi.sale,
74
- hide: vpi.hide,
75
- kind: "price_discount",
43
+ sale: false,
44
+ kind: "price",
76
45
  lineItemPrice: vpi.price * quantity,
77
46
  perItemPrice: vpi.price,
78
- previousLineItemPrice: vpi.previousPrice * quantity,
79
- previousPerItemPrice: vpi.previousPrice,
80
- percentageOff: (0, percentageOff_js_1.percentageOff)(vpi.previousPrice, vpi.price),
81
47
  };
82
48
  }
83
- else {
49
+ }
50
+ else {
51
+ const tieredLPrice = (0, tieredPrice_js_1.tieredPrice)(vpi.tieredPricing, vpi.price, quantity, tieredQuantity);
52
+ const tieredIPrice = Math.round(tieredLPrice / quantity);
53
+ if (vpi.previousPrice === 0) {
84
54
  return {
85
55
  sale: vpi.sale,
86
- hide: vpi.hide,
87
56
  kind: "price",
88
- lineItemPrice: vpi.price * quantity,
89
- perItemPrice: vpi.price,
57
+ lineItemPrice: tieredLPrice,
58
+ perItemPrice: tieredIPrice,
90
59
  };
91
60
  }
61
+ const previousTieredLPrice = (0, tieredPrice_js_1.tieredPrice)(vpi.previousTieredPricing, vpi.previousPrice, quantity, tieredQuantity);
62
+ const previousTieredIPrice = Math.round(previousTieredLPrice / quantity);
63
+ return {
64
+ sale: vpi.sale,
65
+ kind: "price_discount",
66
+ lineItemPrice: tieredLPrice,
67
+ perItemPrice: tieredIPrice,
68
+ previousLineItemPrice: previousTieredLPrice,
69
+ previousPerItemPrice: previousTieredIPrice,
70
+ percentageOff: (0, percentageOff_js_1.percentageOff)(previousTieredLPrice, tieredLPrice),
71
+ };
92
72
  }
93
73
  }
94
74
  function customItemPriceInfo(customItemPrice) {
@@ -98,6 +78,7 @@ function customItemPriceInfo(customItemPrice) {
98
78
  price: customItemPrice || 0,
99
79
  sale: false,
100
80
  tieredPricing: undefined,
81
+ previousTieredPricing: undefined,
101
82
  };
102
83
  }
103
84
  function lineItemPriceInfo(vpi, customItemPrice, quantity, tieredQuantity, discountPreview) {
package/cjs/slate.js CHANGED
@@ -228,6 +228,9 @@ function serializeBlock(ctx, buf, n) {
228
228
  else if (n.type === "separator") {
229
229
  // TODO
230
230
  }
231
+ else if (n.type === "mention") {
232
+ // TODO
233
+ }
231
234
  }
232
235
  function serializeNode(ctx, buf, n) {
233
236
  if (isLanaSlateText(n)) {
@@ -2,6 +2,49 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.variantPriceInfo = variantPriceInfo;
4
4
  const toMajor_js_1 = require("./toMajor.js");
5
+ function findPriceForCurrency(env, vp) {
6
+ const defaultPrice = {
7
+ sale: false,
8
+ price: vp.price,
9
+ mapPrice: vp.map_price,
10
+ previousPrice: 0,
11
+ hide: false,
12
+ tieredPricing: vp.tiered_pricing || undefined,
13
+ previousTieredPricing: undefined,
14
+ };
15
+ if (env.currency !== env.shopCurrency) {
16
+ // try to find appropriate price
17
+ const cp = vp.currency_prices.find((cp) => cp.currency === env.currency);
18
+ if (cp) {
19
+ return {
20
+ sale: false,
21
+ price: cp.price,
22
+ mapPrice: cp.map_price,
23
+ previousPrice: 0,
24
+ hide: false,
25
+ tieredPricing: cp.tiered_pricing || undefined,
26
+ previousTieredPricing: undefined,
27
+ };
28
+ }
29
+ else {
30
+ // otherwise convert the price
31
+ const tp = defaultPrice.tieredPricing;
32
+ defaultPrice.price = convertCurrency(env, defaultPrice.price);
33
+ defaultPrice.mapPrice = convertCurrency(env, defaultPrice.mapPrice);
34
+ defaultPrice.tieredPricing = tp
35
+ ? {
36
+ model: tp.model,
37
+ tiers: tp.tiers.map((t) => ({
38
+ from: t.from,
39
+ to: t.to,
40
+ price: convertCurrency(env, t.price),
41
+ })),
42
+ }
43
+ : undefined;
44
+ }
45
+ }
46
+ return defaultPrice;
47
+ }
5
48
  function convertCurrency(env, value) {
6
49
  const to = env.shopCurrencies[env.currency];
7
50
  if (!to) {
@@ -17,47 +60,37 @@ function convertCurrency(env, value) {
17
60
  const v = Math.ceil(major * xrate * to.currency.to_minor);
18
61
  return v + Math.ceil(v * (to.conversion_fee || 0));
19
62
  }
20
- function salePriceOrPrice(salePriceEligible, pi, tieredPricing) {
21
- return salePriceEligible
22
- ? { sale: true, price: pi.sale_price, previousPrice: pi.price, hide: pi.sale_price < pi.map_price, tieredPricing }
23
- : { sale: false, price: pi.price, previousPrice: pi.retail_price, hide: pi.price < pi.map_price, tieredPricing };
24
- }
25
63
  function variantPriceInfoImpl(env, p) {
26
64
  if (!p)
27
- return { sale: false, price: 0, previousPrice: 0, hide: false, tieredPricing: undefined };
28
- const defaultPrice = salePriceOrPrice(p.sale_price_eligible, p, p.tiered_pricing || undefined);
29
- if (env.currency !== env.shopCurrency) {
30
- // secondary currency, has priority over customer group prices
31
- const cp = p.currency_prices.find((cp) => cp.currency === env.currency);
32
- if (cp)
33
- return salePriceOrPrice(p.sale_price_eligible, cp, cp.tiered_pricing || undefined);
34
- // okay, when there is no provided currency price, we have to perform the conversion
35
- defaultPrice.price = convertCurrency(env, defaultPrice.price);
36
- defaultPrice.previousPrice = convertCurrency(env, defaultPrice.previousPrice);
37
- if (defaultPrice.tieredPricing) {
38
- const tp = defaultPrice.tieredPricing;
39
- defaultPrice.tieredPricing = {
40
- model: tp.model,
41
- tiers: tp.tiers.map((t) => ({
42
- from: t.from,
43
- to: t.to,
44
- price: convertCurrency(env, t.price),
45
- })),
46
- };
47
- }
48
- return defaultPrice;
49
- }
50
- if (p.customer_group_price !== 0) {
51
65
  return {
52
66
  sale: false,
53
- price: p.customer_group_price,
67
+ price: 0,
54
68
  previousPrice: 0,
55
69
  hide: false,
56
- tieredPricing: p.tiered_pricing || undefined,
70
+ tieredPricing: undefined,
71
+ previousTieredPricing: undefined,
72
+ };
73
+ const curr = findPriceForCurrency(env, p);
74
+ if (p.previous_pricing) {
75
+ const prev = findPriceForCurrency(env, p.previous_pricing);
76
+ return {
77
+ sale: true,
78
+ price: curr.price,
79
+ previousPrice: prev.price,
80
+ tieredPricing: curr.tieredPricing,
81
+ previousTieredPricing: prev.tieredPricing,
82
+ hide: curr.price < curr.mapPrice,
57
83
  };
58
84
  }
59
85
  else {
60
- return defaultPrice;
86
+ return {
87
+ sale: false,
88
+ price: curr.price,
89
+ previousPrice: 0,
90
+ tieredPricing: curr.tieredPricing,
91
+ previousTieredPricing: undefined,
92
+ hide: curr.price < curr.mapPrice,
93
+ };
61
94
  }
62
95
  }
63
96
  function applySubscriptionPlanPricing(oldPrice, plan, isShopCurrency) {
@@ -2,27 +2,30 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.variantPriceInfo = variantPriceInfo;
4
4
  const variantPrice_js_1 = require("./variantPrice.js");
5
- function variantLikeToStorefrontVariantPricer(v, customerGroupID) {
6
- let tiered_pricing = v.tiered_pricing;
7
- let customer_group_price = 0;
8
- for (const c of v.customer_group_prices) {
9
- if (c.customer_group && c.customer_group.id === customerGroupID) {
10
- customer_group_price = c.price;
11
- tiered_pricing = c.tiered_pricing;
5
+ function pricingForPricingRuleID(v, prID) {
6
+ const po = v.pricing_overrides.find((po) => { var _a; return ((_a = po.pricing_rule) === null || _a === void 0 ? void 0 : _a.id) === prID; });
7
+ return po
8
+ ? {
9
+ price: po.price,
10
+ map_price: po.map_price,
11
+ retail_price: po.retail_price,
12
+ tiered_pricing: po.tiered_pricing,
13
+ currency_prices: po.currency_prices,
12
14
  }
13
- }
14
- return {
15
- price: v.price,
16
- customer_group_price,
17
- sale_price: v.sale_price,
18
- sale_price_eligible: v.sale_price_eligible,
19
- retail_price: v.retail_price,
20
- map_price: v.map_price,
21
- currency_prices: v.currency_prices,
22
- tiered_pricing,
23
- };
15
+ : {
16
+ price: v.price,
17
+ map_price: v.map_price,
18
+ retail_price: v.retail_price,
19
+ tiered_pricing: v.tiered_pricing,
20
+ currency_prices: v.currency_prices,
21
+ };
24
22
  }
25
- function variantPriceInfo(env, variant, customerGroupID) {
26
- const v = variantLikeToStorefrontVariantPricer(variant, customerGroupID);
23
+ function variantLikeToStorefrontVariantPricer(v, pricingRuleID, parentPricingRuleID, displayPriceDifference) {
24
+ const current = pricingForPricingRuleID(v, pricingRuleID);
25
+ const previous = displayPriceDifference ? pricingForPricingRuleID(v, parentPricingRuleID) : null;
26
+ return Object.assign(Object.assign({}, current), { previous_pricing: previous });
27
+ }
28
+ function variantPriceInfo(env, variant, pricingRuleID, parentPricingRuleID, displayPriceDifference) {
29
+ const v = variantLikeToStorefrontVariantPricer(variant, pricingRuleID, parentPricingRuleID, displayPriceDifference);
27
30
  return (0, variantPrice_js_1.variantPriceInfo)(env, v);
28
31
  }
@@ -1 +1 @@
1
- export default "fragment CommonCartVariant on StorefrontProductVariant{barcode country_of_origin created_at currency_prices{...CurrencyPrice}discountable expiration_days fulfillment_service grams height hs_code id media_files{...MediaFile}linked_media_files{...MediaFile}inventory{available low_stock locations{available low_stock inventory_location{id}}}inventory_policy is_default length map_price max_downloads options{...VariantOption}position price retail_price sale_price sale_price_eligible sale_price_from sale_price_to sku tax_class taxable type updated_at width customer_group_price favorite{id created_at}tiered_pricing{...TieredPricing}}\n";
1
+ export default "fragment CommonCartVariant on StorefrontProductVariant{barcode country_of_origin created_at currency_prices{...CurrencyPrice}discountable expiration_days fulfillment_service grams height hs_code id media_files{...MediaFile}linked_media_files{...MediaFile}inventory{available low_stock locations{available low_stock inventory_location{id}}}inventory_policy is_default length map_price max_downloads options{...VariantOption}position price retail_price sku tax_class taxable type updated_at width favorite{id created_at}tiered_pricing{...TieredPricing}previous_pricing{price retail_price map_price currency_prices{...CurrencyPrice}tiered_pricing{...TieredPricing}}}\n";
@@ -1 +1 @@
1
- export default "fragment CurrencyPrice on CurrencyPrice{currency map_price price retail_price sale_price tiered_pricing{...TieredPricing}}\n";
1
+ export default "fragment CurrencyPrice on CurrencyPrice{currency map_price price retail_price tiered_pricing{...TieredPricing}}\n";
@@ -10,7 +10,6 @@ function applyDiscountPreview(pi, quantity, dp) {
10
10
  const iprice = lprice / quantity;
11
11
  return {
12
12
  sale: pi.sale,
13
- hide: pi.hide,
14
13
  kind: "price_discount",
15
14
  lineItemPrice: lprice,
16
15
  perItemPrice: iprice,
@@ -20,71 +19,52 @@ function applyDiscountPreview(pi, quantity, dp) {
20
19
  };
21
20
  }
22
21
  function lineItemPriceInfoPreDiscountPreview(vpi, quantity, tieredQuantity) {
23
- if (vpi.tieredPricing && vpi.tieredPricing.tiers.length > 0) {
24
- // tiered pricing
25
- if (vpi.sale) {
26
- const prevLPrice = tieredPrice(vpi.tieredPricing, vpi.previousPrice, quantity, tieredQuantity);
27
- const prevIPrice = Math.round(prevLPrice / quantity);
22
+ if (vpi.previousPrice === 0 && vpi.tieredPricing && vpi.tieredPricing.tiers.length > 0) {
23
+ // previous price is not provided and tiered pricing is defined, let's use original price as previous price
24
+ const tieredLPrice = tieredPrice(vpi.tieredPricing, vpi.price, quantity, tieredQuantity);
25
+ const tieredIPrice = Math.round(tieredLPrice / quantity);
26
+ if (tieredLPrice !== vpi.price * quantity) {
28
27
  return {
29
- sale: vpi.sale,
30
- hide: vpi.hide,
28
+ sale: false,
31
29
  kind: "price_discount",
32
- lineItemPrice: vpi.price * quantity,
33
- perItemPrice: vpi.price,
34
- previousLineItemPrice: prevLPrice,
35
- previousPerItemPrice: prevIPrice,
36
- percentageOff: percentageOff(prevLPrice, vpi.price * quantity),
30
+ lineItemPrice: tieredLPrice,
31
+ perItemPrice: tieredIPrice,
32
+ previousLineItemPrice: vpi.price * quantity,
33
+ previousPerItemPrice: vpi.price,
34
+ percentageOff: percentageOff(vpi.price * quantity, tieredLPrice),
37
35
  };
38
36
  }
39
37
  else {
40
- const tieredLPrice = tieredPrice(vpi.tieredPricing, vpi.price, quantity, tieredQuantity);
41
- const tieredIPrice = Math.round(tieredLPrice / quantity);
42
- if (tieredLPrice !== vpi.price * quantity) {
43
- return {
44
- sale: vpi.sale,
45
- hide: vpi.hide,
46
- kind: "price_discount",
47
- lineItemPrice: tieredLPrice,
48
- perItemPrice: tieredIPrice,
49
- previousLineItemPrice: vpi.price * quantity,
50
- previousPerItemPrice: vpi.price,
51
- percentageOff: percentageOff(vpi.price * quantity, tieredLPrice),
52
- };
53
- }
54
- else {
55
- return {
56
- sale: vpi.sale,
57
- hide: vpi.hide,
58
- kind: "price",
59
- lineItemPrice: vpi.price * quantity,
60
- perItemPrice: vpi.price,
61
- };
62
- }
63
- }
64
- }
65
- else {
66
- // normal pricing
67
- if (vpi.previousPrice !== 0 && vpi.previousPrice !== vpi.price) {
68
38
  return {
69
- sale: vpi.sale,
70
- hide: vpi.hide,
71
- kind: "price_discount",
39
+ sale: false,
40
+ kind: "price",
72
41
  lineItemPrice: vpi.price * quantity,
73
42
  perItemPrice: vpi.price,
74
- previousLineItemPrice: vpi.previousPrice * quantity,
75
- previousPerItemPrice: vpi.previousPrice,
76
- percentageOff: percentageOff(vpi.previousPrice, vpi.price),
77
43
  };
78
44
  }
79
- else {
45
+ }
46
+ else {
47
+ const tieredLPrice = tieredPrice(vpi.tieredPricing, vpi.price, quantity, tieredQuantity);
48
+ const tieredIPrice = Math.round(tieredLPrice / quantity);
49
+ if (vpi.previousPrice === 0) {
80
50
  return {
81
51
  sale: vpi.sale,
82
- hide: vpi.hide,
83
52
  kind: "price",
84
- lineItemPrice: vpi.price * quantity,
85
- perItemPrice: vpi.price,
53
+ lineItemPrice: tieredLPrice,
54
+ perItemPrice: tieredIPrice,
86
55
  };
87
56
  }
57
+ const previousTieredLPrice = tieredPrice(vpi.previousTieredPricing, vpi.previousPrice, quantity, tieredQuantity);
58
+ const previousTieredIPrice = Math.round(previousTieredLPrice / quantity);
59
+ return {
60
+ sale: vpi.sale,
61
+ kind: "price_discount",
62
+ lineItemPrice: tieredLPrice,
63
+ perItemPrice: tieredIPrice,
64
+ previousLineItemPrice: previousTieredLPrice,
65
+ previousPerItemPrice: previousTieredIPrice,
66
+ percentageOff: percentageOff(previousTieredLPrice, tieredLPrice),
67
+ };
88
68
  }
89
69
  }
90
70
  function customItemPriceInfo(customItemPrice) {
@@ -94,6 +74,7 @@ function customItemPriceInfo(customItemPrice) {
94
74
  price: customItemPrice || 0,
95
75
  sale: false,
96
76
  tieredPricing: undefined,
77
+ previousTieredPricing: undefined,
97
78
  };
98
79
  }
99
80
  export function lineItemPriceInfo(vpi, customItemPrice, quantity, tieredQuantity, discountPreview) {
package/esm/slate.js CHANGED
@@ -217,6 +217,9 @@ function serializeBlock(ctx, buf, n) {
217
217
  else if (n.type === "separator") {
218
218
  // TODO
219
219
  }
220
+ else if (n.type === "mention") {
221
+ // TODO
222
+ }
220
223
  }
221
224
  function serializeNode(ctx, buf, n) {
222
225
  if (isLanaSlateText(n)) {
@@ -1,4 +1,47 @@
1
1
  import { toMajor } from "./toMajor.js";
2
+ function findPriceForCurrency(env, vp) {
3
+ const defaultPrice = {
4
+ sale: false,
5
+ price: vp.price,
6
+ mapPrice: vp.map_price,
7
+ previousPrice: 0,
8
+ hide: false,
9
+ tieredPricing: vp.tiered_pricing || undefined,
10
+ previousTieredPricing: undefined,
11
+ };
12
+ if (env.currency !== env.shopCurrency) {
13
+ // try to find appropriate price
14
+ const cp = vp.currency_prices.find((cp) => cp.currency === env.currency);
15
+ if (cp) {
16
+ return {
17
+ sale: false,
18
+ price: cp.price,
19
+ mapPrice: cp.map_price,
20
+ previousPrice: 0,
21
+ hide: false,
22
+ tieredPricing: cp.tiered_pricing || undefined,
23
+ previousTieredPricing: undefined,
24
+ };
25
+ }
26
+ else {
27
+ // otherwise convert the price
28
+ const tp = defaultPrice.tieredPricing;
29
+ defaultPrice.price = convertCurrency(env, defaultPrice.price);
30
+ defaultPrice.mapPrice = convertCurrency(env, defaultPrice.mapPrice);
31
+ defaultPrice.tieredPricing = tp
32
+ ? {
33
+ model: tp.model,
34
+ tiers: tp.tiers.map((t) => ({
35
+ from: t.from,
36
+ to: t.to,
37
+ price: convertCurrency(env, t.price),
38
+ })),
39
+ }
40
+ : undefined;
41
+ }
42
+ }
43
+ return defaultPrice;
44
+ }
2
45
  function convertCurrency(env, value) {
3
46
  const to = env.shopCurrencies[env.currency];
4
47
  if (!to) {
@@ -14,47 +57,37 @@ function convertCurrency(env, value) {
14
57
  const v = Math.ceil(major * xrate * to.currency.to_minor);
15
58
  return v + Math.ceil(v * (to.conversion_fee || 0));
16
59
  }
17
- function salePriceOrPrice(salePriceEligible, pi, tieredPricing) {
18
- return salePriceEligible
19
- ? { sale: true, price: pi.sale_price, previousPrice: pi.price, hide: pi.sale_price < pi.map_price, tieredPricing }
20
- : { sale: false, price: pi.price, previousPrice: pi.retail_price, hide: pi.price < pi.map_price, tieredPricing };
21
- }
22
60
  function variantPriceInfoImpl(env, p) {
23
61
  if (!p)
24
- return { sale: false, price: 0, previousPrice: 0, hide: false, tieredPricing: undefined };
25
- const defaultPrice = salePriceOrPrice(p.sale_price_eligible, p, p.tiered_pricing || undefined);
26
- if (env.currency !== env.shopCurrency) {
27
- // secondary currency, has priority over customer group prices
28
- const cp = p.currency_prices.find((cp) => cp.currency === env.currency);
29
- if (cp)
30
- return salePriceOrPrice(p.sale_price_eligible, cp, cp.tiered_pricing || undefined);
31
- // okay, when there is no provided currency price, we have to perform the conversion
32
- defaultPrice.price = convertCurrency(env, defaultPrice.price);
33
- defaultPrice.previousPrice = convertCurrency(env, defaultPrice.previousPrice);
34
- if (defaultPrice.tieredPricing) {
35
- const tp = defaultPrice.tieredPricing;
36
- defaultPrice.tieredPricing = {
37
- model: tp.model,
38
- tiers: tp.tiers.map((t) => ({
39
- from: t.from,
40
- to: t.to,
41
- price: convertCurrency(env, t.price),
42
- })),
43
- };
44
- }
45
- return defaultPrice;
46
- }
47
- if (p.customer_group_price !== 0) {
48
62
  return {
49
63
  sale: false,
50
- price: p.customer_group_price,
64
+ price: 0,
51
65
  previousPrice: 0,
52
66
  hide: false,
53
- tieredPricing: p.tiered_pricing || undefined,
67
+ tieredPricing: undefined,
68
+ previousTieredPricing: undefined,
69
+ };
70
+ const curr = findPriceForCurrency(env, p);
71
+ if (p.previous_pricing) {
72
+ const prev = findPriceForCurrency(env, p.previous_pricing);
73
+ return {
74
+ sale: true,
75
+ price: curr.price,
76
+ previousPrice: prev.price,
77
+ tieredPricing: curr.tieredPricing,
78
+ previousTieredPricing: prev.tieredPricing,
79
+ hide: curr.price < curr.mapPrice,
54
80
  };
55
81
  }
56
82
  else {
57
- return defaultPrice;
83
+ return {
84
+ sale: false,
85
+ price: curr.price,
86
+ previousPrice: 0,
87
+ tieredPricing: curr.tieredPricing,
88
+ previousTieredPricing: undefined,
89
+ hide: curr.price < curr.mapPrice,
90
+ };
58
91
  }
59
92
  }
60
93
  function applySubscriptionPlanPricing(oldPrice, plan, isShopCurrency) {
@@ -1,25 +1,31 @@
1
1
  import { variantPriceInfo as storefrontVariantPriceInfo } from "./variantPrice.js";
2
- function variantLikeToStorefrontVariantPricer(v, customerGroupID) {
3
- let tiered_pricing = v.tiered_pricing;
4
- let customer_group_price = 0;
5
- for (const c of v.customer_group_prices) {
6
- if (c.customer_group && c.customer_group.id === customerGroupID) {
7
- customer_group_price = c.price;
8
- tiered_pricing = c.tiered_pricing;
2
+ function pricingForPricingRuleID(v, prID) {
3
+ const po = v.pricing_overrides.find((po) => po.pricing_rule?.id === prID);
4
+ return po
5
+ ? {
6
+ price: po.price,
7
+ map_price: po.map_price,
8
+ retail_price: po.retail_price,
9
+ tiered_pricing: po.tiered_pricing,
10
+ currency_prices: po.currency_prices,
9
11
  }
10
- }
12
+ : {
13
+ price: v.price,
14
+ map_price: v.map_price,
15
+ retail_price: v.retail_price,
16
+ tiered_pricing: v.tiered_pricing,
17
+ currency_prices: v.currency_prices,
18
+ };
19
+ }
20
+ function variantLikeToStorefrontVariantPricer(v, pricingRuleID, parentPricingRuleID, displayPriceDifference) {
21
+ const current = pricingForPricingRuleID(v, pricingRuleID);
22
+ const previous = displayPriceDifference ? pricingForPricingRuleID(v, parentPricingRuleID) : null;
11
23
  return {
12
- price: v.price,
13
- customer_group_price,
14
- sale_price: v.sale_price,
15
- sale_price_eligible: v.sale_price_eligible,
16
- retail_price: v.retail_price,
17
- map_price: v.map_price,
18
- currency_prices: v.currency_prices,
19
- tiered_pricing,
24
+ ...current,
25
+ previous_pricing: previous,
20
26
  };
21
27
  }
22
- export function variantPriceInfo(env, variant, customerGroupID) {
23
- const v = variantLikeToStorefrontVariantPricer(variant, customerGroupID);
28
+ export function variantPriceInfo(env, variant, pricingRuleID, parentPricingRuleID, displayPriceDifference) {
29
+ const v = variantLikeToStorefrontVariantPricer(variant, pricingRuleID, parentPricingRuleID, displayPriceDifference);
24
30
  return storefrontVariantPriceInfo(env, v);
25
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lana-commerce/core",
3
- "version": "14.0.0-alpha.15",
3
+ "version": "14.0.0-alpha.16",
4
4
  "description": "Lana JS Core",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -1,2 +1,2 @@
1
- declare const _default: "fragment CommonCartVariant on StorefrontProductVariant{barcode country_of_origin created_at currency_prices{...CurrencyPrice}discountable expiration_days fulfillment_service grams height hs_code id media_files{...MediaFile}linked_media_files{...MediaFile}inventory{available low_stock locations{available low_stock inventory_location{id}}}inventory_policy is_default length map_price max_downloads options{...VariantOption}position price retail_price sale_price sale_price_eligible sale_price_from sale_price_to sku tax_class taxable type updated_at width customer_group_price favorite{id created_at}tiered_pricing{...TieredPricing}}\n";
1
+ declare const _default: "fragment CommonCartVariant on StorefrontProductVariant{barcode country_of_origin created_at currency_prices{...CurrencyPrice}discountable expiration_days fulfillment_service grams height hs_code id media_files{...MediaFile}linked_media_files{...MediaFile}inventory{available low_stock locations{available low_stock inventory_location{id}}}inventory_policy is_default length map_price max_downloads options{...VariantOption}position price retail_price sku tax_class taxable type updated_at width favorite{id created_at}tiered_pricing{...TieredPricing}previous_pricing{price retail_price map_price currency_prices{...CurrencyPrice}tiered_pricing{...TieredPricing}}}\n";
2
2
  export default _default;
@@ -1,2 +1,2 @@
1
- declare const _default: "fragment CurrencyPrice on CurrencyPrice{currency map_price price retail_price sale_price tiered_pricing{...TieredPricing}}\n";
1
+ declare const _default: "fragment CurrencyPrice on CurrencyPrice{currency map_price price retail_price tiered_pricing{...TieredPricing}}\n";
2
2
  export default _default;