@anker-in/shopify-react 1.2.2-beta.6 → 1.2.2-beta.7

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/dist/index.mjs CHANGED
@@ -3,7 +3,7 @@ import { createShopifyClient, getProductsByHandles, createCart, updateCartCodes,
3
3
  export * from '@anker-in/shopify-sdk';
4
4
  import Cookies5 from 'js-cookie';
5
5
  import { jsx } from 'react/jsx-runtime';
6
- import Decimal2 from 'decimal.js';
6
+ import Decimal3 from 'decimal.js';
7
7
  import { atobID, btoaID } from '@anker-in/shopify-core';
8
8
  import useSWR from 'swr';
9
9
  import useSWRMutation from 'swr/mutation';
@@ -152,8 +152,6 @@ var CODE_AMOUNT_KEY = "_sku_code_money";
152
152
  var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
153
153
  var MEMBER_PRICE_ATTRIBUTE_KEY = "_member_price";
154
154
  var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
155
-
156
- // src/hooks/cart/utils/normalize-add-to-cart-lines.ts
157
155
  function normalizeAddToCartLines(lines) {
158
156
  return lines.filter((line) => line.variant?.id).map((line, index) => {
159
157
  const variant = line.variant;
@@ -161,8 +159,8 @@ function normalizeAddToCartLines(lines) {
161
159
  const quantity = line.quantity || 1;
162
160
  const originalPrice = variant.price?.amount ? Number(variant.price.amount) : 0;
163
161
  const finalPrice = variant.finalPrice?.amount === void 0 ? originalPrice : Number(variant.finalPrice?.amount);
164
- const subtotalAmount = originalPrice * quantity;
165
- const totalAmount = finalPrice * quantity;
162
+ const subtotalAmount = new Decimal3(originalPrice).times(quantity).toNumber();
163
+ const totalAmount = new Decimal3(finalPrice).times(quantity).toNumber();
166
164
  return {
167
165
  id: `temp-line-${index}-${variant.id}`,
168
166
  // Temporary ID for pre-cart lines
@@ -170,6 +168,7 @@ function normalizeAddToCartLines(lines) {
170
168
  quantity,
171
169
  variantId: variant.id,
172
170
  productId: product?.id || variant.id.split("/").slice(0, -2).join("/"),
171
+ amountPerQuantity: finalPrice,
173
172
  totalAmount,
174
173
  subtotalAmount,
175
174
  discountAllocations: [],
@@ -354,7 +353,7 @@ var formatScriptAutoFreeGift = ({
354
353
  path: product?.handle || "",
355
354
  variant,
356
355
  totalAmount: 0,
357
- subtotalAmount: new Decimal2(
356
+ subtotalAmount: new Decimal3(
358
357
  typeof variant?.price === "object" ? variant?.price?.amount || 0 : variant?.price || 0
359
358
  ).toNumber(),
360
359
  options: [],
@@ -414,7 +413,7 @@ var formatFunctionAutoFreeGift = ({
414
413
  path: product?.handle || "",
415
414
  variant,
416
415
  totalAmount: 0,
417
- subtotalAmount: new Decimal2(
416
+ subtotalAmount: new Decimal3(
418
417
  typeof variant?.price === "object" ? variant?.price?.amount || 0 : variant?.price || 0
419
418
  ).toNumber(),
420
419
  options: [],
@@ -644,7 +643,7 @@ var useScriptAutoFreeGift = ({
644
643
  const breakpoints = useMemo(() => {
645
644
  if (!isActivityAvailable) return [];
646
645
  return (campaign?.breakpoints || []).map((item) => ({
647
- breakpoint: new Decimal2(item.breakpoint).minus(new Decimal2(upgrade_value)).dividedBy(new Decimal2(upgrade_multiple)).toFixed(2, Decimal2.ROUND_DOWN),
646
+ breakpoint: new Decimal3(item.breakpoint).minus(new Decimal3(upgrade_value)).dividedBy(new Decimal3(upgrade_multiple)).toFixed(2, Decimal3.ROUND_DOWN),
648
647
  giveawayProducts: item.giveawayProducts || []
649
648
  }));
650
649
  }, [campaign, upgrade_multiple, upgrade_value]);
@@ -679,11 +678,11 @@ var useScriptAutoFreeGift = ({
679
678
  });
680
679
  }, [effectiveCart?.lineItems, isActivityAvailable, _giveaway]);
681
680
  const involvedSubTotal = useMemo(() => {
682
- if (!isActivityAvailable) return new Decimal2(0);
681
+ if (!isActivityAvailable) return new Decimal3(0);
683
682
  return involvedLines.reduce((prev, item) => {
684
683
  const amount = campaign?.useTotalAmount ? item.totalAmount : item.subtotalAmount;
685
- return new Decimal2(prev).plus(new Decimal2(amount || 0));
686
- }, new Decimal2(0));
684
+ return new Decimal3(prev).plus(new Decimal3(amount || 0));
685
+ }, new Decimal3(0));
687
686
  }, [involvedLines, isActivityAvailable]);
688
687
  const [freeGiftLevel, nextFreeGiftLevel] = useMemo(() => {
689
688
  if (!isActivityAvailable) return [null, null];
@@ -691,7 +690,7 @@ var useScriptAutoFreeGift = ({
691
690
  (a, b) => Number(b.breakpoint) - Number(a.breakpoint)
692
691
  );
693
692
  const levelIndex = sortedLevels.findIndex(
694
- (level) => involvedSubTotal.gte(new Decimal2(level.breakpoint)) && involvedLines.length > 0
693
+ (level) => involvedSubTotal.gte(new Decimal3(level.breakpoint)) && involvedLines.length > 0
695
694
  );
696
695
  if (levelIndex === -1) {
697
696
  return [
@@ -862,7 +861,7 @@ var trackAddToCartGA = ({
862
861
  return;
863
862
  }
864
863
  const currencyCode = lineItems[0].product?.price?.currencyCode;
865
- const totalPrice = lineItems.reduce((prev, item) => prev.plus(item.variant.price || 0), new Decimal2(0)).toNumber();
864
+ const totalPrice = lineItems.reduce((prev, item) => prev.plus(item.totalAmount || 0), new Decimal3(0)).toNumber();
866
865
  gaTrack({
867
866
  event: "ga4Event",
868
867
  event_name: "add_to_cart",
@@ -904,7 +903,7 @@ var trackBeginCheckoutGA = ({
904
903
  (prev, { variant }) => prev.plus(
905
904
  variant?.finalPrice?.amount === void 0 ? Number(variant?.price?.amount) || 0 : Number(variant?.finalPrice?.amount) || 0
906
905
  ),
907
- new Decimal2(0)
906
+ new Decimal3(0)
908
907
  ).toNumber();
909
908
  gaTrack({
910
909
  event: "ga4Event",
@@ -943,7 +942,7 @@ var trackBuyNowGA = ({
943
942
  return;
944
943
  }
945
944
  const currencyCode = lineItems[0].product?.price?.currencyCode;
946
- const totalPrice = lineItems.reduce((prev, item) => prev.plus(item.variant.price || 0), new Decimal2(0)).toNumber();
945
+ const totalPrice = lineItems.reduce((prev, item) => prev.plus(item.totalAmount || 0), new Decimal3(0)).toNumber();
947
946
  gaTrack({
948
947
  event: "ga4Event",
949
948
  event_name: "begin_checkout",
@@ -1199,7 +1198,7 @@ var initDiscountAttributes = ({ line }) => {
1199
1198
  key: CUSTOMER_ATTRIBUTE_KEY,
1200
1199
  value: JSON.stringify({
1201
1200
  is_gift: false,
1202
- discounted_amount: new Decimal2(priceAmount).times(line.quantity || 1).toNumber()
1201
+ discounted_amount: new Decimal3(priceAmount).times(line.quantity || 1).toNumber()
1203
1202
  })
1204
1203
  }
1205
1204
  ]);
@@ -1223,11 +1222,11 @@ var initDiscountAttributes = ({ line }) => {
1223
1222
  itemAttributes = itemAttributes.concat([
1224
1223
  {
1225
1224
  key: CODE_AMOUNT_KEY,
1226
- value: new Decimal2(coupon.amount).times(line.quantity || 1).toString()
1225
+ value: new Decimal3(coupon.amount).times(line.quantity || 1).toString()
1227
1226
  },
1228
1227
  {
1229
1228
  key: SCRIPT_CODE_AMOUNT_KEY,
1230
- value: new Decimal2(coupon.amount).times(line.quantity || 1).toString()
1229
+ value: new Decimal3(coupon.amount).times(line.quantity || 1).toString()
1231
1230
  }
1232
1231
  ]);
1233
1232
  }
@@ -1685,8 +1684,8 @@ function getCartBasicAttributes({
1685
1684
  {
1686
1685
  key: "_weight",
1687
1686
  value: cart?.lineItems?.reduce((acc, item) => {
1688
- const itemWeight = new Decimal2(item.variant.weight ?? 0).times(item.quantity);
1689
- return new Decimal2(acc).plus(itemWeight).toNumber();
1687
+ const itemWeight = new Decimal3(item.variant.weight ?? 0).times(item.quantity);
1688
+ return new Decimal3(acc).plus(itemWeight).toNumber();
1690
1689
  }, 0).toString()
1691
1690
  }
1692
1691
  ];