@anker-in/shopify-react 0.1.1-beta.21 → 0.1.1-beta.23

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.
@@ -1,9 +1,10 @@
1
1
  import { createContext, useMemo, useRef, useState, useEffect, useCallback, useContext } from 'react';
2
2
  import useSWRMutation from 'swr/mutation';
3
- import { atobID, btoaID, getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, updateCartAttributes, getLocalStorage, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage, updateCartDeliveryOptions } from '@anker-in/shopify-sdk';
3
+ import { getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, updateCartAttributes, getLocalStorage, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage, updateCartDeliveryOptions } from '@anker-in/shopify-sdk';
4
4
  import Cookies5 from 'js-cookie';
5
5
  import { jsx } from 'react/jsx-runtime';
6
6
  import Decimal2 from 'decimal.js';
7
+ import { atobID, btoaID } from '@anker-in/shopify-core';
7
8
  import useSWR from 'swr';
8
9
  import { useRequest } from 'ahooks';
9
10
 
@@ -68,9 +69,10 @@ function normalizeAddToCartLines(lines) {
68
69
  const variant = line.variant;
69
70
  const product = variant.product;
70
71
  const quantity = line.quantity || 1;
71
- const price = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
72
- const subtotalAmount = price * quantity;
73
- const totalAmount = subtotalAmount;
72
+ const originalPrice = variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
73
+ const finalPrice = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : originalPrice;
74
+ const subtotalAmount = originalPrice * quantity;
75
+ const totalAmount = finalPrice * quantity;
74
76
  return {
75
77
  id: `temp-line-${index}-${variant.id}`,
76
78
  // Temporary ID for pre-cart lines
@@ -84,7 +86,7 @@ function normalizeAddToCartLines(lines) {
84
86
  customAttributes: line.attributes || [],
85
87
  variant: {
86
88
  id: variant.id,
87
- price,
89
+ price: variant.price?.amount ? Number(variant.price.amount) : 0,
88
90
  listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
89
91
  sku: variant.sku || "",
90
92
  name: variant.title || "",
@@ -901,6 +903,61 @@ function useRemoveCartCodes(options) {
901
903
  return useSWRMutation("remove-codes", removeCodes, options);
902
904
  }
903
905
 
906
+ // src/hooks/cart/utils/add-to-cart.ts
907
+ var getLinesWithAttributes = ({
908
+ cart,
909
+ lineItems
910
+ }) => {
911
+ return lineItems.map((line) => {
912
+ const sameLineInCart = cart?.lineItems.find(
913
+ (lineInCart) => lineInCart.variant.sku === line.variant?.sku && lineInCart.product?.handle === line.variant?.product?.handle
914
+ );
915
+ const codeAmountAttribute = sameLineInCart?.customAttributes?.find(
916
+ (attr) => attr.key === CODE_AMOUNT_KEY
917
+ );
918
+ const scriptCodeAmountAttribute = sameLineInCart?.customAttributes?.find(
919
+ (attr) => attr.key === SCRIPT_CODE_AMOUNT_KEY
920
+ );
921
+ let functionAttribute = null;
922
+ try {
923
+ functionAttribute = sameLineInCart?.customAttributes?.find(
924
+ (attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY && JSON.parse(attr.value)?.discounted_amount
925
+ );
926
+ } catch (error) {
927
+ }
928
+ if (codeAmountAttribute || functionAttribute || scriptCodeAmountAttribute) {
929
+ return {
930
+ ...line,
931
+ attributes: [
932
+ ...line.attributes || [],
933
+ codeAmountAttribute,
934
+ functionAttribute,
935
+ scriptCodeAmountAttribute
936
+ ].filter(Boolean)
937
+ };
938
+ }
939
+ return line;
940
+ });
941
+ };
942
+ var getLinesWithFunctionAttributes = (lineItems) => {
943
+ return lineItems.map((line) => {
944
+ let itemAttributes = line.attributes || [];
945
+ const functionEnvAttribute = itemAttributes.find((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY);
946
+ if (!functionEnvAttribute) {
947
+ itemAttributes = itemAttributes.concat([
948
+ {
949
+ key: CUSTOMER_ATTRIBUTE_KEY,
950
+ value: JSON.stringify({
951
+ is_gift: false,
952
+ discounted_amount: Number(line.variant?.finalPrice?.amount || line.variant?.price?.amount) * (line.quantity || 1)
953
+ })
954
+ }
955
+ ]);
956
+ }
957
+ return { ...line, attributes: itemAttributes };
958
+ });
959
+ };
960
+
904
961
  // src/hooks/cart/use-add-to-cart.ts
905
962
  function useAddToCart({ withTrack = true } = {}, swrOptions) {
906
963
  const { client, config, locale, cartCookieAdapter, userAdapter } = useShopify();
@@ -924,7 +981,12 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
924
981
  if (!lineItems || lineItems.length === 0) {
925
982
  return;
926
983
  }
927
- const lines = lineItems.map((item) => ({
984
+ const linesWithAttributes = getLinesWithAttributes({
985
+ cart,
986
+ lineItems
987
+ });
988
+ const linesWithFunctionAttributes = getLinesWithFunctionAttributes(linesWithAttributes);
989
+ const lines = linesWithFunctionAttributes.map((item) => ({
928
990
  merchandiseId: item.variant?.id || "",
929
991
  quantity: item.quantity || 1,
930
992
  attributes: item.attributes,
@@ -1075,7 +1137,8 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
1075
1137
  if (!lineItems || lineItems.length === 0) {
1076
1138
  return;
1077
1139
  }
1078
- const lines = lineItems.map((item) => ({
1140
+ const linesWithFunctionAttributes = getLinesWithFunctionAttributes(lineItems);
1141
+ const lines = linesWithFunctionAttributes.map((item) => ({
1079
1142
  merchandiseId: item.variant?.id || "",
1080
1143
  quantity: item.quantity || 1,
1081
1144
  attributes: item.attributes,
@@ -1528,7 +1591,7 @@ var useUpdateLineCodeAmountAttributes = ({
1528
1591
  );
1529
1592
  const functionEnvValue = getDiscountEnvAttributeValue(line.customAttributes);
1530
1593
  const hasSameFunctionEnvAttribute = Number(functionEnvValue.discounted_amount) === Number(line.totalAmount);
1531
- if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute) {
1594
+ if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute && !functionEnvValue.is_gift) {
1532
1595
  attrNeedUpdate.push({
1533
1596
  key: CUSTOMER_ATTRIBUTE_KEY,
1534
1597
  value: JSON.stringify({