@anker-in/shopify-react 1.2.2-beta.5 → 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.
@@ -3,7 +3,7 @@ import useSWRMutation from 'swr/mutation';
3
3
  import { getProductsByHandles, createCart, updateCartCodes, addCartLines, removeCartLines, getLocalStorage, updateCartLines, updateCartAttributes, updateBuyerIdentity, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage } 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 { useRequest } from 'ahooks';
@@ -63,8 +63,6 @@ var CODE_AMOUNT_KEY = "_sku_code_money";
63
63
  var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
64
64
  var MEMBER_PRICE_ATTRIBUTE_KEY = "_member_price";
65
65
  var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
66
-
67
- // src/hooks/cart/utils/normalize-add-to-cart-lines.ts
68
66
  function normalizeAddToCartLines(lines) {
69
67
  return lines.filter((line) => line.variant?.id).map((line, index) => {
70
68
  const variant = line.variant;
@@ -72,8 +70,8 @@ function normalizeAddToCartLines(lines) {
72
70
  const quantity = line.quantity || 1;
73
71
  const originalPrice = variant.price?.amount ? Number(variant.price.amount) : 0;
74
72
  const finalPrice = variant.finalPrice?.amount === void 0 ? originalPrice : Number(variant.finalPrice?.amount);
75
- const subtotalAmount = originalPrice * quantity;
76
- const totalAmount = finalPrice * quantity;
73
+ const subtotalAmount = new Decimal3(originalPrice).times(quantity).toNumber();
74
+ const totalAmount = new Decimal3(finalPrice).times(quantity).toNumber();
77
75
  return {
78
76
  id: `temp-line-${index}-${variant.id}`,
79
77
  // Temporary ID for pre-cart lines
@@ -81,6 +79,7 @@ function normalizeAddToCartLines(lines) {
81
79
  quantity,
82
80
  variantId: variant.id,
83
81
  productId: product?.id || variant.id.split("/").slice(0, -2).join("/"),
82
+ amountPerQuantity: finalPrice,
84
83
  totalAmount,
85
84
  subtotalAmount,
86
85
  discountAllocations: [],
@@ -265,7 +264,7 @@ var formatScriptAutoFreeGift = ({
265
264
  path: product?.handle || "",
266
265
  variant,
267
266
  totalAmount: 0,
268
- subtotalAmount: new Decimal2(
267
+ subtotalAmount: new Decimal3(
269
268
  typeof variant?.price === "object" ? variant?.price?.amount || 0 : variant?.price || 0
270
269
  ).toNumber(),
271
270
  options: [],
@@ -325,7 +324,7 @@ var formatFunctionAutoFreeGift = ({
325
324
  path: product?.handle || "",
326
325
  variant,
327
326
  totalAmount: 0,
328
- subtotalAmount: new Decimal2(
327
+ subtotalAmount: new Decimal3(
329
328
  typeof variant?.price === "object" ? variant?.price?.amount || 0 : variant?.price || 0
330
329
  ).toNumber(),
331
330
  options: [],
@@ -555,7 +554,7 @@ var useScriptAutoFreeGift = ({
555
554
  const breakpoints = useMemo(() => {
556
555
  if (!isActivityAvailable) return [];
557
556
  return (campaign?.breakpoints || []).map((item) => ({
558
- breakpoint: new Decimal2(item.breakpoint).minus(new Decimal2(upgrade_value)).dividedBy(new Decimal2(upgrade_multiple)).toFixed(2, Decimal2.ROUND_DOWN),
557
+ breakpoint: new Decimal3(item.breakpoint).minus(new Decimal3(upgrade_value)).dividedBy(new Decimal3(upgrade_multiple)).toFixed(2, Decimal3.ROUND_DOWN),
559
558
  giveawayProducts: item.giveawayProducts || []
560
559
  }));
561
560
  }, [campaign, upgrade_multiple, upgrade_value]);
@@ -590,11 +589,11 @@ var useScriptAutoFreeGift = ({
590
589
  });
591
590
  }, [effectiveCart?.lineItems, isActivityAvailable, _giveaway]);
592
591
  const involvedSubTotal = useMemo(() => {
593
- if (!isActivityAvailable) return new Decimal2(0);
592
+ if (!isActivityAvailable) return new Decimal3(0);
594
593
  return involvedLines.reduce((prev, item) => {
595
594
  const amount = campaign?.useTotalAmount ? item.totalAmount : item.subtotalAmount;
596
- return new Decimal2(prev).plus(new Decimal2(amount || 0));
597
- }, new Decimal2(0));
595
+ return new Decimal3(prev).plus(new Decimal3(amount || 0));
596
+ }, new Decimal3(0));
598
597
  }, [involvedLines, isActivityAvailable]);
599
598
  const [freeGiftLevel, nextFreeGiftLevel] = useMemo(() => {
600
599
  if (!isActivityAvailable) return [null, null];
@@ -602,7 +601,7 @@ var useScriptAutoFreeGift = ({
602
601
  (a, b) => Number(b.breakpoint) - Number(a.breakpoint)
603
602
  );
604
603
  const levelIndex = sortedLevels.findIndex(
605
- (level) => involvedSubTotal.gte(new Decimal2(level.breakpoint)) && involvedLines.length > 0
604
+ (level) => involvedSubTotal.gte(new Decimal3(level.breakpoint)) && involvedLines.length > 0
606
605
  );
607
606
  if (levelIndex === -1) {
608
607
  return [
@@ -773,7 +772,7 @@ var trackAddToCartGA = ({
773
772
  return;
774
773
  }
775
774
  const currencyCode = lineItems[0].product?.price?.currencyCode;
776
- const totalPrice = lineItems.reduce((prev, item) => prev.plus(item.variant.price || 0), new Decimal2(0)).toNumber();
775
+ const totalPrice = lineItems.reduce((prev, item) => prev.plus(item.totalAmount || 0), new Decimal3(0)).toNumber();
777
776
  gaTrack({
778
777
  event: "ga4Event",
779
778
  event_name: "add_to_cart",
@@ -807,36 +806,30 @@ var trackBuyNowGA = ({
807
806
  lineItems = [],
808
807
  gtmParams = {}
809
808
  }) => {
810
- if (!lineItems.length || !lineItems[0]?.variant) {
809
+ if (!lineItems.length) {
811
810
  return;
812
811
  }
813
- const { variant } = lineItems[0];
814
- const currencyCode = variant.product?.price?.currencyCode || variant.price?.currencyCode;
815
- const totalPrice = lineItems?.reduce(
816
- (prev, { variant: variant2 }) => prev.plus(
817
- variant2?.finalPrice?.amount === void 0 ? Number(variant2?.price?.amount) || 0 : Number(variant2?.finalPrice?.amount) || 0
818
- ),
819
- new Decimal2(0)
820
- ).toNumber();
812
+ const currencyCode = lineItems[0].product?.price?.currencyCode;
813
+ const totalPrice = lineItems.reduce((prev, item) => prev.plus(item.totalAmount || 0), new Decimal3(0)).toNumber();
821
814
  gaTrack({
822
815
  event: "ga4Event",
823
816
  event_name: "begin_checkout",
824
817
  event_parameters: {
825
818
  page_group: gtmParams?.pageGroup,
826
- position: gtmParams?.position,
819
+ position: gtmParams?.position || "",
827
820
  currency: currencyCode,
828
821
  value: totalPrice,
829
822
  items: lineItems.map((item) => {
830
- const imageUrl = item.variant?.image?.url || item.variant?.product?.images?.[0]?.url;
823
+ const imageUrl = item.variant.image?.url || item.product?.images?.[0]?.url;
831
824
  const itemCategoryId = item.gtmParams?.item_category_id;
832
- const itemVariantId = item.variant?.id ? atobID(item.variant.id) : void 0;
825
+ const itemVariantId = item.variant.id ? atobID(item.variant.id) : void 0;
833
826
  return {
834
- item_id: item.variant?.sku,
835
- item_name: item.variant?.product?.title || item.variant?.title,
827
+ item_id: item.variant.sku,
828
+ item_name: item.product?.title,
836
829
  item_brand: gtmParams?.brand || "",
837
- item_category: item.variant?.product?.productType || "",
838
- item_variant: item.variant?.title,
839
- price: item.variant?.compareAtPrice?.amount ?? item.variant?.price?.amount,
830
+ item_category: item.product?.productType || "",
831
+ item_variant: item.variant.name,
832
+ price: item.variant.listPrice ?? item.variant.price,
840
833
  quantity: item.quantity || 1,
841
834
  ...imageUrl && { image_url: imageUrl },
842
835
  ...itemCategoryId !== void 0 && { item_category_id: itemCategoryId },
@@ -1035,7 +1028,7 @@ var initDiscountAttributes = ({ line }) => {
1035
1028
  key: CUSTOMER_ATTRIBUTE_KEY,
1036
1029
  value: JSON.stringify({
1037
1030
  is_gift: false,
1038
- discounted_amount: new Decimal2(priceAmount).times(line.quantity || 1).toNumber()
1031
+ discounted_amount: new Decimal3(priceAmount).times(line.quantity || 1).toNumber()
1039
1032
  })
1040
1033
  }
1041
1034
  ]);
@@ -1059,11 +1052,11 @@ var initDiscountAttributes = ({ line }) => {
1059
1052
  itemAttributes = itemAttributes.concat([
1060
1053
  {
1061
1054
  key: CODE_AMOUNT_KEY,
1062
- value: new Decimal2(coupon.amount).times(line.quantity || 1).toString()
1055
+ value: new Decimal3(coupon.amount).times(line.quantity || 1).toString()
1063
1056
  },
1064
1057
  {
1065
1058
  key: SCRIPT_CODE_AMOUNT_KEY,
1066
- value: new Decimal2(coupon.amount).times(line.quantity || 1).toString()
1059
+ value: new Decimal3(coupon.amount).times(line.quantity || 1).toString()
1067
1060
  }
1068
1061
  ]);
1069
1062
  }
@@ -1521,8 +1514,8 @@ function getCartBasicAttributes({
1521
1514
  {
1522
1515
  key: "_weight",
1523
1516
  value: cart?.lineItems?.reduce((acc, item) => {
1524
- const itemWeight = new Decimal2(item.variant.weight ?? 0).times(item.quantity);
1525
- return new Decimal2(acc).plus(itemWeight).toNumber();
1517
+ const itemWeight = new Decimal3(item.variant.weight ?? 0).times(item.quantity);
1518
+ return new Decimal3(acc).plus(itemWeight).toNumber();
1526
1519
  }, 0).toString()
1527
1520
  }
1528
1521
  ];
@@ -2547,7 +2540,16 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
2547
2540
  }
2548
2541
  if (withTrack && resultCart.lineItems) {
2549
2542
  trackBuyNowGA({
2550
- lineItems,
2543
+ lineItems: lineItems.map((item) => {
2544
+ const cartLine = resultCart.lineItems.find((line) => line.variant.id === item.variant?.id);
2545
+ if (!cartLine) {
2546
+ return null;
2547
+ }
2548
+ return {
2549
+ ...cartLine,
2550
+ quantity: item.quantity
2551
+ };
2552
+ }).filter(Boolean),
2551
2553
  gtmParams: { ...gtmParams, brand: config.getBrand() }
2552
2554
  });
2553
2555
  if (fbqTrackConfig) {