@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.
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",
@@ -939,36 +938,30 @@ var trackBuyNowGA = ({
939
938
  lineItems = [],
940
939
  gtmParams = {}
941
940
  }) => {
942
- if (!lineItems.length || !lineItems[0]?.variant) {
941
+ if (!lineItems.length) {
943
942
  return;
944
943
  }
945
- const { variant } = lineItems[0];
946
- const currencyCode = variant.product?.price?.currencyCode || variant.price?.currencyCode;
947
- const totalPrice = lineItems?.reduce(
948
- (prev, { variant: variant2 }) => prev.plus(
949
- variant2?.finalPrice?.amount === void 0 ? Number(variant2?.price?.amount) || 0 : Number(variant2?.finalPrice?.amount) || 0
950
- ),
951
- new Decimal2(0)
952
- ).toNumber();
944
+ const currencyCode = lineItems[0].product?.price?.currencyCode;
945
+ const totalPrice = lineItems.reduce((prev, item) => prev.plus(item.totalAmount || 0), new Decimal3(0)).toNumber();
953
946
  gaTrack({
954
947
  event: "ga4Event",
955
948
  event_name: "begin_checkout",
956
949
  event_parameters: {
957
950
  page_group: gtmParams?.pageGroup,
958
- position: gtmParams?.position,
951
+ position: gtmParams?.position || "",
959
952
  currency: currencyCode,
960
953
  value: totalPrice,
961
954
  items: lineItems.map((item) => {
962
- const imageUrl = item.variant?.image?.url || item.variant?.product?.images?.[0]?.url;
955
+ const imageUrl = item.variant.image?.url || item.product?.images?.[0]?.url;
963
956
  const itemCategoryId = item.gtmParams?.item_category_id;
964
- const itemVariantId = item.variant?.id ? atobID(item.variant.id) : void 0;
957
+ const itemVariantId = item.variant.id ? atobID(item.variant.id) : void 0;
965
958
  return {
966
- item_id: item.variant?.sku,
967
- item_name: item.variant?.product?.title || item.variant?.title,
959
+ item_id: item.variant.sku,
960
+ item_name: item.product?.title,
968
961
  item_brand: gtmParams?.brand || "",
969
- item_category: item.variant?.product?.productType || "",
970
- item_variant: item.variant?.title,
971
- price: item.variant?.compareAtPrice?.amount ?? item.variant?.price?.amount,
962
+ item_category: item.product?.productType || "",
963
+ item_variant: item.variant.name,
964
+ price: item.variant.listPrice ?? item.variant.price,
972
965
  quantity: item.quantity || 1,
973
966
  ...imageUrl && { image_url: imageUrl },
974
967
  ...itemCategoryId !== void 0 && { item_category_id: itemCategoryId },
@@ -1205,7 +1198,7 @@ var initDiscountAttributes = ({ line }) => {
1205
1198
  key: CUSTOMER_ATTRIBUTE_KEY,
1206
1199
  value: JSON.stringify({
1207
1200
  is_gift: false,
1208
- discounted_amount: new Decimal2(priceAmount).times(line.quantity || 1).toNumber()
1201
+ discounted_amount: new Decimal3(priceAmount).times(line.quantity || 1).toNumber()
1209
1202
  })
1210
1203
  }
1211
1204
  ]);
@@ -1229,11 +1222,11 @@ var initDiscountAttributes = ({ line }) => {
1229
1222
  itemAttributes = itemAttributes.concat([
1230
1223
  {
1231
1224
  key: CODE_AMOUNT_KEY,
1232
- value: new Decimal2(coupon.amount).times(line.quantity || 1).toString()
1225
+ value: new Decimal3(coupon.amount).times(line.quantity || 1).toString()
1233
1226
  },
1234
1227
  {
1235
1228
  key: SCRIPT_CODE_AMOUNT_KEY,
1236
- value: new Decimal2(coupon.amount).times(line.quantity || 1).toString()
1229
+ value: new Decimal3(coupon.amount).times(line.quantity || 1).toString()
1237
1230
  }
1238
1231
  ]);
1239
1232
  }
@@ -1691,8 +1684,8 @@ function getCartBasicAttributes({
1691
1684
  {
1692
1685
  key: "_weight",
1693
1686
  value: cart?.lineItems?.reduce((acc, item) => {
1694
- const itemWeight = new Decimal2(item.variant.weight ?? 0).times(item.quantity);
1695
- 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();
1696
1689
  }, 0).toString()
1697
1690
  }
1698
1691
  ];
@@ -2717,7 +2710,16 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
2717
2710
  }
2718
2711
  if (withTrack && resultCart.lineItems) {
2719
2712
  trackBuyNowGA({
2720
- lineItems,
2713
+ lineItems: lineItems.map((item) => {
2714
+ const cartLine = resultCart.lineItems.find((line) => line.variant.id === item.variant?.id);
2715
+ if (!cartLine) {
2716
+ return null;
2717
+ }
2718
+ return {
2719
+ ...cartLine,
2720
+ quantity: item.quantity
2721
+ };
2722
+ }).filter(Boolean),
2721
2723
  gtmParams: { ...gtmParams, brand: config.getBrand() }
2722
2724
  });
2723
2725
  if (fbqTrackConfig) {