@anker-in/shopify-react 0.1.1-beta.22 → 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.
package/dist/index.mjs CHANGED
@@ -1000,6 +1000,61 @@ function useRemoveCartCodes(options) {
1000
1000
  return useSWRMutation("remove-codes", removeCodes, options);
1001
1001
  }
1002
1002
 
1003
+ // src/hooks/cart/utils/add-to-cart.ts
1004
+ var getLinesWithAttributes = ({
1005
+ cart,
1006
+ lineItems
1007
+ }) => {
1008
+ return lineItems.map((line) => {
1009
+ const sameLineInCart = cart?.lineItems.find(
1010
+ (lineInCart) => lineInCart.variant.sku === line.variant?.sku && lineInCart.product?.handle === line.variant?.product?.handle
1011
+ );
1012
+ const codeAmountAttribute = sameLineInCart?.customAttributes?.find(
1013
+ (attr) => attr.key === CODE_AMOUNT_KEY
1014
+ );
1015
+ const scriptCodeAmountAttribute = sameLineInCart?.customAttributes?.find(
1016
+ (attr) => attr.key === SCRIPT_CODE_AMOUNT_KEY
1017
+ );
1018
+ let functionAttribute = null;
1019
+ try {
1020
+ functionAttribute = sameLineInCart?.customAttributes?.find(
1021
+ (attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY && JSON.parse(attr.value)?.discounted_amount
1022
+ );
1023
+ } catch (error) {
1024
+ }
1025
+ if (codeAmountAttribute || functionAttribute || scriptCodeAmountAttribute) {
1026
+ return {
1027
+ ...line,
1028
+ attributes: [
1029
+ ...line.attributes || [],
1030
+ codeAmountAttribute,
1031
+ functionAttribute,
1032
+ scriptCodeAmountAttribute
1033
+ ].filter(Boolean)
1034
+ };
1035
+ }
1036
+ return line;
1037
+ });
1038
+ };
1039
+ var getLinesWithFunctionAttributes = (lineItems) => {
1040
+ return lineItems.map((line) => {
1041
+ let itemAttributes = line.attributes || [];
1042
+ const functionEnvAttribute = itemAttributes.find((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY);
1043
+ if (!functionEnvAttribute) {
1044
+ itemAttributes = itemAttributes.concat([
1045
+ {
1046
+ key: CUSTOMER_ATTRIBUTE_KEY,
1047
+ value: JSON.stringify({
1048
+ is_gift: false,
1049
+ discounted_amount: Number(line.variant?.finalPrice?.amount || line.variant?.price?.amount) * (line.quantity || 1)
1050
+ })
1051
+ }
1052
+ ]);
1053
+ }
1054
+ return { ...line, attributes: itemAttributes };
1055
+ });
1056
+ };
1057
+
1003
1058
  // src/hooks/cart/use-add-to-cart.ts
1004
1059
  function useAddToCart({ withTrack = true } = {}, swrOptions) {
1005
1060
  const { client, config, locale, cartCookieAdapter, userAdapter } = useShopify();
@@ -1023,7 +1078,12 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1023
1078
  if (!lineItems || lineItems.length === 0) {
1024
1079
  return;
1025
1080
  }
1026
- const lines = lineItems.map((item) => ({
1081
+ const linesWithAttributes = getLinesWithAttributes({
1082
+ cart,
1083
+ lineItems
1084
+ });
1085
+ const linesWithFunctionAttributes = getLinesWithFunctionAttributes(linesWithAttributes);
1086
+ const lines = linesWithFunctionAttributes.map((item) => ({
1027
1087
  merchandiseId: item.variant?.id || "",
1028
1088
  quantity: item.quantity || 1,
1029
1089
  attributes: item.attributes,
@@ -1174,7 +1234,8 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
1174
1234
  if (!lineItems || lineItems.length === 0) {
1175
1235
  return;
1176
1236
  }
1177
- const lines = lineItems.map((item) => ({
1237
+ const linesWithFunctionAttributes = getLinesWithFunctionAttributes(lineItems);
1238
+ const lines = linesWithFunctionAttributes.map((item) => ({
1178
1239
  merchandiseId: item.variant?.id || "",
1179
1240
  quantity: item.quantity || 1,
1180
1241
  attributes: item.attributes,
@@ -1627,7 +1688,7 @@ var useUpdateLineCodeAmountAttributes = ({
1627
1688
  );
1628
1689
  const functionEnvValue = getDiscountEnvAttributeValue(line.customAttributes);
1629
1690
  const hasSameFunctionEnvAttribute = Number(functionEnvValue.discounted_amount) === Number(line.totalAmount);
1630
- if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute) {
1691
+ if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute && !functionEnvValue.is_gift) {
1631
1692
  attrNeedUpdate.push({
1632
1693
  key: CUSTOMER_ATTRIBUTE_KEY,
1633
1694
  value: JSON.stringify({