@anker-in/shopify-react 1.1.1 → 1.2.0-beta.1
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/hooks/index.d.mts +39 -44
- package/dist/hooks/index.d.ts +39 -44
- package/dist/hooks/index.js +349 -93
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +342 -94
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +438 -116
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +427 -117
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +26 -14
- package/dist/provider/index.d.ts +26 -14
- package/dist/provider/index.js +155 -32
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +155 -32
- package/dist/provider/index.mjs.map +1 -1
- package/dist/{types-CMMWxyUF.d.mts → types-C4qc-wG4.d.mts} +167 -7
- package/dist/{types-CMMWxyUF.d.ts → types-C4qc-wG4.d.ts} +167 -7
- package/package.json +17 -17
package/dist/hooks/index.js
CHANGED
|
@@ -797,15 +797,23 @@ var trackAddToCartGA = ({
|
|
|
797
797
|
currency: currencyCode,
|
|
798
798
|
value: totalPrice,
|
|
799
799
|
position: gtmParams?.position || "",
|
|
800
|
-
items: lineItems.map((
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
800
|
+
items: lineItems.map((item) => {
|
|
801
|
+
const imageUrl = item.variant?.image?.url || item.variant?.product?.images?.[0]?.url;
|
|
802
|
+
const itemCategoryId = item.gtmParams?.item_category_id;
|
|
803
|
+
const itemVariantId = item.variant?.id ? shopifyCore.atobID(item.variant.id) : void 0;
|
|
804
|
+
return {
|
|
805
|
+
item_id: item.variant?.sku,
|
|
806
|
+
item_name: item.variant?.product?.title || item.variant?.product?.title,
|
|
807
|
+
item_brand: gtmParams?.brand || "",
|
|
808
|
+
item_category: item.variant?.product?.productType || "",
|
|
809
|
+
item_variant: item.variant?.title || item.variant?.title,
|
|
810
|
+
price: item.variant?.compareAtPrice?.amount ?? item.variant?.price?.amount,
|
|
811
|
+
quantity: item.quantity || 1,
|
|
812
|
+
...imageUrl && { image_url: imageUrl },
|
|
813
|
+
...itemCategoryId && { item_category_id: itemCategoryId },
|
|
814
|
+
...itemVariantId && { item_variant_id: itemVariantId }
|
|
815
|
+
};
|
|
816
|
+
}),
|
|
809
817
|
...gtmParams?.ga4Params
|
|
810
818
|
}
|
|
811
819
|
});
|
|
@@ -833,19 +841,73 @@ var trackBuyNowGA = ({
|
|
|
833
841
|
position: gtmParams?.position,
|
|
834
842
|
currency: currencyCode,
|
|
835
843
|
value: totalPrice,
|
|
836
|
-
items: lineItems.map((item) =>
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
844
|
+
items: lineItems.map((item) => {
|
|
845
|
+
const imageUrl = item.variant?.image?.url || item.variant?.product?.images?.[0]?.url;
|
|
846
|
+
const itemCategoryId = item.gtmParams?.item_category_id;
|
|
847
|
+
const itemVariantId = item.variant?.id ? shopifyCore.atobID(item.variant.id) : void 0;
|
|
848
|
+
return {
|
|
849
|
+
item_id: item.variant?.sku,
|
|
850
|
+
item_name: item.variant?.product?.title || item.variant?.title,
|
|
851
|
+
item_brand: gtmParams?.brand || "",
|
|
852
|
+
item_category: item.variant?.product?.productType || "",
|
|
853
|
+
item_variant: item.variant?.title,
|
|
854
|
+
price: item.variant?.compareAtPrice?.amount ?? item.variant?.price?.amount,
|
|
855
|
+
quantity: item.quantity || 1,
|
|
856
|
+
...imageUrl && { image_url: imageUrl },
|
|
857
|
+
...itemCategoryId !== void 0 && { item_category_id: itemCategoryId },
|
|
858
|
+
...itemVariantId && { item_variant_id: itemVariantId }
|
|
859
|
+
};
|
|
860
|
+
}),
|
|
845
861
|
...gtmParams?.ga4Params
|
|
846
862
|
}
|
|
847
863
|
});
|
|
848
864
|
};
|
|
865
|
+
function waitForGtagReady(timeout = 1e4) {
|
|
866
|
+
return new Promise((resolve, reject) => {
|
|
867
|
+
const start = Date.now();
|
|
868
|
+
function check() {
|
|
869
|
+
if (typeof window !== "undefined" && typeof window.gtag !== "undefined") {
|
|
870
|
+
resolve();
|
|
871
|
+
} else if (Date.now() - start > timeout) {
|
|
872
|
+
reject(new Error("GA4 gtag not loaded"));
|
|
873
|
+
} else {
|
|
874
|
+
setTimeout(check, 50);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
check();
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
var getGA4Data = async (measurementId = "G-R0BRMRK4CY") => {
|
|
881
|
+
try {
|
|
882
|
+
await waitForGtagReady();
|
|
883
|
+
const timeoutPromise = new Promise((resolve) => {
|
|
884
|
+
setTimeout(() => {
|
|
885
|
+
resolve({ clientId: "", sessionId: "" });
|
|
886
|
+
}, 300);
|
|
887
|
+
});
|
|
888
|
+
const dataPromise = new Promise((resolve) => {
|
|
889
|
+
if (!window.gtag) {
|
|
890
|
+
resolve({ clientId: "", sessionId: "" });
|
|
891
|
+
return;
|
|
892
|
+
}
|
|
893
|
+
window.gtag("get", measurementId, "client_id", (clientId) => {
|
|
894
|
+
window.gtag("get", measurementId, "session_id", (sessionId) => {
|
|
895
|
+
resolve({
|
|
896
|
+
clientId: clientId || "",
|
|
897
|
+
sessionId: sessionId || ""
|
|
898
|
+
});
|
|
899
|
+
});
|
|
900
|
+
});
|
|
901
|
+
});
|
|
902
|
+
return Promise.race([dataPromise, timeoutPromise]);
|
|
903
|
+
} catch (error) {
|
|
904
|
+
console.error("Failed to get GA4 data:", error);
|
|
905
|
+
return {
|
|
906
|
+
clientId: "",
|
|
907
|
+
sessionId: ""
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
};
|
|
849
911
|
|
|
850
912
|
// src/tracking/fbq.ts
|
|
851
913
|
var trackAddToCartFBQ = ({ lineItems = [] }) => {
|
|
@@ -1028,16 +1090,155 @@ var getLinesWithAttributes = ({
|
|
|
1028
1090
|
return functionLine;
|
|
1029
1091
|
});
|
|
1030
1092
|
};
|
|
1093
|
+
function useRemoveCartLines(options) {
|
|
1094
|
+
const { client, locale, cartCookieAdapter } = useShopify();
|
|
1095
|
+
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
1096
|
+
const removeLines = react.useCallback(
|
|
1097
|
+
async (_key, { arg }) => {
|
|
1098
|
+
const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
|
|
1099
|
+
let updatedCart = await shopifySdk.removeCartLines(client, {
|
|
1100
|
+
cartId,
|
|
1101
|
+
lineIds,
|
|
1102
|
+
metafieldIdentifiers,
|
|
1103
|
+
cookieAdapter: cartCookieAdapter
|
|
1104
|
+
});
|
|
1105
|
+
if (updatedCart && autoRemoveInvalidCodes) {
|
|
1106
|
+
const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
1107
|
+
if (unApplicableCodes.length > 0) {
|
|
1108
|
+
if (onCodesRemoved) {
|
|
1109
|
+
const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
|
|
1110
|
+
if (handledCart) {
|
|
1111
|
+
updatedCart = handledCart;
|
|
1112
|
+
}
|
|
1113
|
+
} else {
|
|
1114
|
+
updatedCart = await shopifySdk.updateCartCodes(client, {
|
|
1115
|
+
cartId: updatedCart.id,
|
|
1116
|
+
discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
|
|
1117
|
+
metafieldIdentifiers,
|
|
1118
|
+
cookieAdapter: cartCookieAdapter
|
|
1119
|
+
}) || updatedCart;
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
if (updatedCart) {
|
|
1124
|
+
mutateCart(updatedCart);
|
|
1125
|
+
}
|
|
1126
|
+
return updatedCart;
|
|
1127
|
+
},
|
|
1128
|
+
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
1129
|
+
);
|
|
1130
|
+
return useSWRMutation__default.default("remove-cart-lines", removeLines, options);
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
// src/hooks/cart/feature/use-auto-remove-free-gifts.ts
|
|
1134
|
+
function useAutoRemoveFreeGifts(options = {}) {
|
|
1135
|
+
const {
|
|
1136
|
+
removeFunctionGifts = true,
|
|
1137
|
+
removeScriptGifts = true,
|
|
1138
|
+
isGiftLineItem
|
|
1139
|
+
} = options;
|
|
1140
|
+
const [isRemoving, setIsRemoving] = react.useState(false);
|
|
1141
|
+
const { cart } = useCartContext();
|
|
1142
|
+
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
1143
|
+
const giftsToRemove = react.useMemo(() => {
|
|
1144
|
+
if (!cart?.lineItems) {
|
|
1145
|
+
return [];
|
|
1146
|
+
}
|
|
1147
|
+
return cart.lineItems.filter((item) => {
|
|
1148
|
+
if (removeFunctionGifts) {
|
|
1149
|
+
const functionAttr = item.customAttributes?.find(
|
|
1150
|
+
(attr) => attr.key === "_discounts_function_env"
|
|
1151
|
+
)?.value;
|
|
1152
|
+
if (functionAttr) {
|
|
1153
|
+
try {
|
|
1154
|
+
const functionAttrObj = JSON.parse(functionAttr);
|
|
1155
|
+
if (functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money) {
|
|
1156
|
+
return true;
|
|
1157
|
+
}
|
|
1158
|
+
} catch (error) {
|
|
1159
|
+
console.error("Failed to parse _discounts_function_env:", error);
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
if (removeScriptGifts) {
|
|
1164
|
+
const scriptGiftAttr = item.customAttributes?.find(
|
|
1165
|
+
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
1166
|
+
);
|
|
1167
|
+
if (scriptGiftAttr) {
|
|
1168
|
+
return true;
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
if (isGiftLineItem && isGiftLineItem(item)) {
|
|
1172
|
+
return true;
|
|
1173
|
+
}
|
|
1174
|
+
return false;
|
|
1175
|
+
});
|
|
1176
|
+
}, [cart, removeFunctionGifts, removeScriptGifts, isGiftLineItem]);
|
|
1177
|
+
react.useEffect(() => {
|
|
1178
|
+
if (isRemoving || giftsToRemove.length === 0) {
|
|
1179
|
+
return;
|
|
1180
|
+
}
|
|
1181
|
+
const performRemoval = async () => {
|
|
1182
|
+
setIsRemoving(true);
|
|
1183
|
+
try {
|
|
1184
|
+
await removeCartLines2({
|
|
1185
|
+
lineIds: giftsToRemove.map((item) => item.id)
|
|
1186
|
+
});
|
|
1187
|
+
} catch (error) {
|
|
1188
|
+
console.error("Failed to remove free gifts:", error);
|
|
1189
|
+
} finally {
|
|
1190
|
+
setIsRemoving(false);
|
|
1191
|
+
}
|
|
1192
|
+
};
|
|
1193
|
+
performRemoval();
|
|
1194
|
+
}, [
|
|
1195
|
+
isRemoving,
|
|
1196
|
+
giftsToRemove,
|
|
1197
|
+
removeCartLines2
|
|
1198
|
+
]);
|
|
1199
|
+
return {
|
|
1200
|
+
isRemoving
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
function isFunctionGift(line) {
|
|
1204
|
+
const functionAttr = line.customAttributes?.find(
|
|
1205
|
+
(attr) => attr.key === "_discounts_function_env"
|
|
1206
|
+
)?.value;
|
|
1207
|
+
if (!functionAttr) {
|
|
1208
|
+
return false;
|
|
1209
|
+
}
|
|
1210
|
+
try {
|
|
1211
|
+
const functionAttrObj = JSON.parse(functionAttr);
|
|
1212
|
+
return Boolean(
|
|
1213
|
+
functionAttrObj.is_gift && functionAttrObj.rule_id && functionAttrObj.spend_sum_money
|
|
1214
|
+
);
|
|
1215
|
+
} catch {
|
|
1216
|
+
return false;
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
function isScriptGift(line) {
|
|
1220
|
+
return line.customAttributes?.some(
|
|
1221
|
+
(attr) => attr.key === "_giveaway_gradient_gifts"
|
|
1222
|
+
) ?? false;
|
|
1223
|
+
}
|
|
1224
|
+
function isBuyGetGift(line) {
|
|
1225
|
+
return line.customAttributes?.some(
|
|
1226
|
+
(attr) => attr.key === "_freegift_related_handlesku"
|
|
1227
|
+
) ?? false;
|
|
1228
|
+
}
|
|
1229
|
+
function isAnyGift(line) {
|
|
1230
|
+
return isFunctionGift(line) || isScriptGift(line) || isBuyGetGift(line);
|
|
1231
|
+
}
|
|
1031
1232
|
function useCalcGiftsFromLines({
|
|
1032
1233
|
lines,
|
|
1033
1234
|
customer,
|
|
1034
1235
|
scriptGiveawayKey = CUSTOMER_SCRIPT_GIFT_KEY
|
|
1035
1236
|
}) {
|
|
1036
1237
|
const { locale } = useShopify();
|
|
1037
|
-
const { cart,
|
|
1038
|
-
const functionGift = useCalcAutoFreeGift(cart,
|
|
1238
|
+
const { cart, functionAutoFreeGiftConfig, scriptAutoFreeGiftConfig } = useCartContext();
|
|
1239
|
+
const functionGift = useCalcAutoFreeGift(cart, functionAutoFreeGiftConfig || [], customer, lines);
|
|
1039
1240
|
const scriptGift = useScriptAutoFreeGift({
|
|
1040
|
-
campaign:
|
|
1241
|
+
campaign: scriptAutoFreeGiftConfig,
|
|
1041
1242
|
_giveaway: scriptGiveawayKey,
|
|
1042
1243
|
cart,
|
|
1043
1244
|
locale,
|
|
@@ -1072,7 +1273,9 @@ function useCalcGiftsFromLines({
|
|
|
1072
1273
|
const variants = product?.variants;
|
|
1073
1274
|
const variant = Array.isArray(variants) ? variants.find((v) => v.sku === item.sku) : void 0;
|
|
1074
1275
|
if (!variant) {
|
|
1075
|
-
console.warn(
|
|
1276
|
+
console.warn(
|
|
1277
|
+
`Script gift: Variant not found for handle=${item.handle}, sku=${item.sku}`
|
|
1278
|
+
);
|
|
1076
1279
|
return null;
|
|
1077
1280
|
}
|
|
1078
1281
|
return {
|
|
@@ -1215,6 +1418,19 @@ var getReferralAttributes = () => {
|
|
|
1215
1418
|
}
|
|
1216
1419
|
return [];
|
|
1217
1420
|
};
|
|
1421
|
+
var getOperatingSystem = () => {
|
|
1422
|
+
if (typeof window === "undefined" || typeof navigator === "undefined") {
|
|
1423
|
+
return "Unknown";
|
|
1424
|
+
}
|
|
1425
|
+
const userAgent = navigator.userAgent || navigator.vendor || window.opera;
|
|
1426
|
+
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
|
|
1427
|
+
return "IOS";
|
|
1428
|
+
}
|
|
1429
|
+
if (/android/i.test(userAgent)) {
|
|
1430
|
+
return "Android";
|
|
1431
|
+
}
|
|
1432
|
+
return "Unknown";
|
|
1433
|
+
};
|
|
1218
1434
|
var getUserType = (customer) => {
|
|
1219
1435
|
let userInfo = Cookies5__default.default.get("userInfo");
|
|
1220
1436
|
if (userInfo) {
|
|
@@ -1236,12 +1452,42 @@ var getUserType = (customer) => {
|
|
|
1236
1452
|
}
|
|
1237
1453
|
return "new_user_login";
|
|
1238
1454
|
};
|
|
1239
|
-
function
|
|
1455
|
+
function getGA4Attributes(ga4Data) {
|
|
1456
|
+
if (!ga4Data?.clientId && !ga4Data?.sessionId) {
|
|
1457
|
+
return [];
|
|
1458
|
+
}
|
|
1459
|
+
const attributes = [];
|
|
1460
|
+
if (ga4Data.clientId) {
|
|
1461
|
+
attributes.push({
|
|
1462
|
+
key: "_ga4_client_id",
|
|
1463
|
+
value: ga4Data.clientId
|
|
1464
|
+
});
|
|
1465
|
+
}
|
|
1466
|
+
if (ga4Data.sessionId) {
|
|
1467
|
+
attributes.push({
|
|
1468
|
+
key: "_ga4_session_id",
|
|
1469
|
+
value: ga4Data.sessionId
|
|
1470
|
+
});
|
|
1471
|
+
}
|
|
1472
|
+
return attributes;
|
|
1473
|
+
}
|
|
1474
|
+
async function getGA4AttributesAsync() {
|
|
1475
|
+
try {
|
|
1476
|
+
const ga4Data = await getGA4Data();
|
|
1477
|
+
return getGA4Attributes(ga4Data);
|
|
1478
|
+
} catch (error) {
|
|
1479
|
+
console.error("Failed to get GA4 attributes:", error);
|
|
1480
|
+
return [];
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
function getCartBasicAttributes({
|
|
1240
1484
|
profile,
|
|
1241
1485
|
customer,
|
|
1242
1486
|
cart,
|
|
1243
1487
|
memberType,
|
|
1244
|
-
currentUrl = ""
|
|
1488
|
+
currentUrl = "",
|
|
1489
|
+
appContext,
|
|
1490
|
+
buyPath
|
|
1245
1491
|
}) {
|
|
1246
1492
|
const userType = getUserType(customer);
|
|
1247
1493
|
const memberAttributes = [
|
|
@@ -1268,12 +1514,10 @@ function getCartAttributes({
|
|
|
1268
1514
|
value: "1"
|
|
1269
1515
|
});
|
|
1270
1516
|
}
|
|
1271
|
-
const discountCodes = cart?.discountCodes.map((item) => item.code).filter((code) => code) || [];
|
|
1272
1517
|
const functionAttributes = [
|
|
1273
1518
|
{
|
|
1274
1519
|
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
1275
1520
|
value: JSON.stringify({
|
|
1276
|
-
discount_code: discountCodes,
|
|
1277
1521
|
user_tags: customer?.tags || []
|
|
1278
1522
|
})
|
|
1279
1523
|
}
|
|
@@ -1281,26 +1525,38 @@ function getCartAttributes({
|
|
|
1281
1525
|
const presellAttributes = [
|
|
1282
1526
|
{
|
|
1283
1527
|
key: "_presale",
|
|
1284
|
-
value: cart?.lineItems
|
|
1528
|
+
value: cart?.lineItems?.some((item) => item?.variant?.metafields?.presell === "presell")
|
|
1285
1529
|
}
|
|
1286
1530
|
];
|
|
1287
1531
|
const weightAttributes = [
|
|
1288
1532
|
{
|
|
1289
1533
|
key: "_weight",
|
|
1290
|
-
value: cart?.lineItems
|
|
1534
|
+
value: cart?.lineItems?.reduce((acc, item) => {
|
|
1291
1535
|
const itemWeight = new Decimal2__default.default(item.variant.weight ?? 0).times(item.quantity);
|
|
1292
1536
|
return new Decimal2__default.default(acc).plus(itemWeight).toNumber();
|
|
1293
1537
|
}, 0).toString()
|
|
1294
|
-
},
|
|
1295
|
-
{
|
|
1296
|
-
key: "_app_source_name",
|
|
1297
|
-
value: "dtc"
|
|
1298
1538
|
}
|
|
1299
1539
|
];
|
|
1300
1540
|
const trackingAttributes = [
|
|
1301
1541
|
{
|
|
1302
1542
|
key: "utm_params",
|
|
1303
1543
|
value: currentUrl
|
|
1544
|
+
},
|
|
1545
|
+
{
|
|
1546
|
+
key: "_app_source_name",
|
|
1547
|
+
value: appContext?.isInApp && appContext?.appName ? appContext.appName : "dtc"
|
|
1548
|
+
},
|
|
1549
|
+
{
|
|
1550
|
+
key: "_operating_system",
|
|
1551
|
+
value: getOperatingSystem()
|
|
1552
|
+
},
|
|
1553
|
+
{
|
|
1554
|
+
key: "_cart_id",
|
|
1555
|
+
value: cart?.id
|
|
1556
|
+
},
|
|
1557
|
+
{
|
|
1558
|
+
key: "_buy_path",
|
|
1559
|
+
value: buyPath
|
|
1304
1560
|
}
|
|
1305
1561
|
];
|
|
1306
1562
|
const commonAttributes = [
|
|
@@ -1320,21 +1576,41 @@ var useCartAttributes = ({
|
|
|
1320
1576
|
profile,
|
|
1321
1577
|
customer,
|
|
1322
1578
|
cart,
|
|
1323
|
-
memberType
|
|
1579
|
+
memberType,
|
|
1580
|
+
appContext,
|
|
1581
|
+
buyPath
|
|
1324
1582
|
}) => {
|
|
1325
1583
|
const [currentUrl, setCurrentUrl] = react.useState("");
|
|
1584
|
+
const [ga4Data, setGa4Data] = react.useState(null);
|
|
1326
1585
|
react.useEffect(() => {
|
|
1327
1586
|
setCurrentUrl(window.location.href);
|
|
1328
1587
|
}, []);
|
|
1588
|
+
react.useEffect(() => {
|
|
1589
|
+
let isMounted = true;
|
|
1590
|
+
getGA4Data().then((data) => {
|
|
1591
|
+
if (isMounted) {
|
|
1592
|
+
setGa4Data(data);
|
|
1593
|
+
}
|
|
1594
|
+
}).catch((error) => {
|
|
1595
|
+
console.error("Failed to get GA4 data in useCartAttributes:", error);
|
|
1596
|
+
});
|
|
1597
|
+
return () => {
|
|
1598
|
+
isMounted = false;
|
|
1599
|
+
};
|
|
1600
|
+
}, []);
|
|
1329
1601
|
const attributes = react.useMemo(() => {
|
|
1330
|
-
|
|
1602
|
+
const basicAttributes = getCartBasicAttributes({
|
|
1331
1603
|
profile,
|
|
1332
1604
|
customer,
|
|
1333
1605
|
cart,
|
|
1334
1606
|
memberType,
|
|
1335
|
-
currentUrl
|
|
1607
|
+
currentUrl,
|
|
1608
|
+
appContext,
|
|
1609
|
+
buyPath
|
|
1336
1610
|
});
|
|
1337
|
-
|
|
1611
|
+
const ga4Attributes = getGA4Attributes(ga4Data);
|
|
1612
|
+
return [...basicAttributes, ...ga4Attributes];
|
|
1613
|
+
}, [profile, customer, cart, memberType, currentUrl, appContext, buyPath, ga4Data]);
|
|
1338
1614
|
return react.useMemo(
|
|
1339
1615
|
() => ({
|
|
1340
1616
|
attributes
|
|
@@ -1397,12 +1673,9 @@ var useUpdateLineCodeAmountAttributes = ({
|
|
|
1397
1673
|
const codeDiscount = line.discountAllocations?.find(
|
|
1398
1674
|
(allocation) => mainProductDiscountCodes?.includes(allocation.code)
|
|
1399
1675
|
);
|
|
1400
|
-
const hasFunctionEnvAttribute = line.customAttributes?.find(
|
|
1401
|
-
(attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY
|
|
1402
|
-
);
|
|
1403
1676
|
const functionEnvValue = getDiscountEnvAttributeValue(line.customAttributes);
|
|
1404
1677
|
const hasSameFunctionEnvAttribute = Number(functionEnvValue.discounted_amount) === Number(line.totalAmount);
|
|
1405
|
-
if (!hasSameFunctionEnvAttribute &&
|
|
1678
|
+
if (!hasSameFunctionEnvAttribute && !functionEnvValue.is_gift) {
|
|
1406
1679
|
attrNeedUpdate.push({
|
|
1407
1680
|
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
1408
1681
|
value: JSON.stringify({
|
|
@@ -1823,47 +2096,6 @@ var usePlusMemberCheckoutCustomAttributes = ({
|
|
|
1823
2096
|
return checkoutCustomAttributes;
|
|
1824
2097
|
}, [profile, selectedShippingMethod, selectedPlusMemberMode, isPresaleContains]);
|
|
1825
2098
|
};
|
|
1826
|
-
function useRemoveCartLines(options) {
|
|
1827
|
-
const { client, locale, cartCookieAdapter } = useShopify();
|
|
1828
|
-
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
1829
|
-
const removeLines = react.useCallback(
|
|
1830
|
-
async (_key, { arg }) => {
|
|
1831
|
-
const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
|
|
1832
|
-
let updatedCart = await shopifySdk.removeCartLines(client, {
|
|
1833
|
-
cartId,
|
|
1834
|
-
lineIds,
|
|
1835
|
-
metafieldIdentifiers,
|
|
1836
|
-
cookieAdapter: cartCookieAdapter
|
|
1837
|
-
});
|
|
1838
|
-
if (updatedCart && autoRemoveInvalidCodes) {
|
|
1839
|
-
const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
1840
|
-
if (unApplicableCodes.length > 0) {
|
|
1841
|
-
if (onCodesRemoved) {
|
|
1842
|
-
const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
|
|
1843
|
-
if (handledCart) {
|
|
1844
|
-
updatedCart = handledCart;
|
|
1845
|
-
}
|
|
1846
|
-
} else {
|
|
1847
|
-
updatedCart = await shopifySdk.updateCartCodes(client, {
|
|
1848
|
-
cartId: updatedCart.id,
|
|
1849
|
-
discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
|
|
1850
|
-
metafieldIdentifiers,
|
|
1851
|
-
cookieAdapter: cartCookieAdapter
|
|
1852
|
-
}) || updatedCart;
|
|
1853
|
-
}
|
|
1854
|
-
}
|
|
1855
|
-
}
|
|
1856
|
-
if (updatedCart) {
|
|
1857
|
-
mutateCart(updatedCart);
|
|
1858
|
-
}
|
|
1859
|
-
return updatedCart;
|
|
1860
|
-
},
|
|
1861
|
-
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
1862
|
-
);
|
|
1863
|
-
return useSWRMutation__default.default("remove-cart-lines", removeLines, options);
|
|
1864
|
-
}
|
|
1865
|
-
|
|
1866
|
-
// src/hooks/member/plus/use-auto-remove-plus-member-in-cart.ts
|
|
1867
2099
|
function useAutoRemovePlusMemberInCart({
|
|
1868
2100
|
cart,
|
|
1869
2101
|
profile,
|
|
@@ -2078,7 +2310,8 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
2078
2310
|
profile,
|
|
2079
2311
|
customer,
|
|
2080
2312
|
cart,
|
|
2081
|
-
memberType: hasPlusMember ? "2" : String(profile?.memberType ?? 0)
|
|
2313
|
+
memberType: hasPlusMember ? "2" : String(profile?.memberType ?? 0),
|
|
2314
|
+
buyPath: "hasCart"
|
|
2082
2315
|
});
|
|
2083
2316
|
const addToCart = react.useCallback(
|
|
2084
2317
|
async (_key, { arg }) => {
|
|
@@ -2171,7 +2404,8 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
2171
2404
|
applyCartCodes,
|
|
2172
2405
|
removeInvalidCodes,
|
|
2173
2406
|
addCustomAttributes,
|
|
2174
|
-
config
|
|
2407
|
+
config,
|
|
2408
|
+
cartAttributes
|
|
2175
2409
|
]
|
|
2176
2410
|
);
|
|
2177
2411
|
return useSWRMutation__default.default("add-to-cart", addToCart, swrOptions);
|
|
@@ -2250,7 +2484,7 @@ function useUpdateBuyerIdentity({
|
|
|
2250
2484
|
}
|
|
2251
2485
|
function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
2252
2486
|
const { client, config, locale, cartCookieAdapter, userAdapter } = useShopify();
|
|
2253
|
-
const { profile, customer, memberSetting } = useCartContext();
|
|
2487
|
+
const { profile, customer, memberSetting, appContext } = useCartContext();
|
|
2254
2488
|
const isLoggedIn = userAdapter?.isLoggedIn || false;
|
|
2255
2489
|
const buyNow = react.useCallback(
|
|
2256
2490
|
async (_key, { arg }) => {
|
|
@@ -2272,12 +2506,6 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
2272
2506
|
lines: lineItems
|
|
2273
2507
|
});
|
|
2274
2508
|
const memberType = hasPlusMember ? "2" : String(profile?.memberType ?? 0);
|
|
2275
|
-
const cartAttributes = getCartAttributes({
|
|
2276
|
-
profile,
|
|
2277
|
-
customer,
|
|
2278
|
-
memberType,
|
|
2279
|
-
currentUrl: window.location.href
|
|
2280
|
-
});
|
|
2281
2509
|
const linesWithFunctionAttributes = getLinesWithAttributes({
|
|
2282
2510
|
lineItems
|
|
2283
2511
|
});
|
|
@@ -2290,13 +2518,23 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
2290
2518
|
if (lines.length === 0) {
|
|
2291
2519
|
return;
|
|
2292
2520
|
}
|
|
2521
|
+
const basicCartAttributes = getCartBasicAttributes({
|
|
2522
|
+
profile,
|
|
2523
|
+
customer,
|
|
2524
|
+
memberType,
|
|
2525
|
+
cart: { lineItems },
|
|
2526
|
+
currentUrl: window.location.href,
|
|
2527
|
+
appContext,
|
|
2528
|
+
buyPath: "buyNow"
|
|
2529
|
+
});
|
|
2530
|
+
const ga4Attributes = await getGA4AttributesAsync();
|
|
2293
2531
|
const resultCart = await shopifySdk.createCart(client, {
|
|
2294
2532
|
lines,
|
|
2295
2533
|
metafieldIdentifiers,
|
|
2296
2534
|
cookieAdapter: cartCookieAdapter,
|
|
2297
2535
|
buyerIdentity,
|
|
2298
2536
|
discountCodes,
|
|
2299
|
-
customAttributes: [...
|
|
2537
|
+
customAttributes: [...basicCartAttributes, ...ga4Attributes, ...customAttributes || []]
|
|
2300
2538
|
});
|
|
2301
2539
|
if (!resultCart) {
|
|
2302
2540
|
throw new Error("Failed to create cart for buy now");
|
|
@@ -2321,7 +2559,17 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
2321
2559
|
}
|
|
2322
2560
|
return resultCart;
|
|
2323
2561
|
},
|
|
2324
|
-
[
|
|
2562
|
+
[
|
|
2563
|
+
client,
|
|
2564
|
+
locale,
|
|
2565
|
+
isLoggedIn,
|
|
2566
|
+
cartCookieAdapter,
|
|
2567
|
+
withTrack,
|
|
2568
|
+
customer,
|
|
2569
|
+
profile,
|
|
2570
|
+
memberSetting,
|
|
2571
|
+
appContext
|
|
2572
|
+
]
|
|
2325
2573
|
);
|
|
2326
2574
|
return useSWRMutation__default.default("buy-now", buyNow, swrOptions);
|
|
2327
2575
|
}
|
|
@@ -3140,14 +3388,21 @@ exports.defaultSWRMutationConfiguration = defaultSWRMutationConfiguration;
|
|
|
3140
3388
|
exports.formatFunctionAutoFreeGift = formatFunctionAutoFreeGift;
|
|
3141
3389
|
exports.formatScriptAutoFreeGift = formatScriptAutoFreeGift;
|
|
3142
3390
|
exports.getCachedGeoLocation = getCachedGeoLocation;
|
|
3143
|
-
exports.
|
|
3391
|
+
exports.getCartBasicAttributes = getCartBasicAttributes;
|
|
3144
3392
|
exports.getDiscountEnvAttributeValue = getDiscountEnvAttributeValue;
|
|
3393
|
+
exports.getGA4Attributes = getGA4Attributes;
|
|
3394
|
+
exports.getGA4AttributesAsync = getGA4AttributesAsync;
|
|
3145
3395
|
exports.getMatchedMainProductSubTotal = getMatchedMainProductSubTotal;
|
|
3396
|
+
exports.getOperatingSystem = getOperatingSystem;
|
|
3146
3397
|
exports.getQuery = getQuery;
|
|
3147
3398
|
exports.getReferralAttributes = getReferralAttributes;
|
|
3148
3399
|
exports.getUserType = getUserType;
|
|
3149
3400
|
exports.hasPlusMemberInCart = hasPlusMemberInCart;
|
|
3150
3401
|
exports.hasPlusMemberInLines = hasPlusMemberInLines;
|
|
3402
|
+
exports.isAnyGift = isAnyGift;
|
|
3403
|
+
exports.isBuyGetGift = isBuyGetGift;
|
|
3404
|
+
exports.isFunctionGift = isFunctionGift;
|
|
3405
|
+
exports.isScriptGift = isScriptGift;
|
|
3151
3406
|
exports.normalizeAddToCartLines = normalizeAddToCartLines;
|
|
3152
3407
|
exports.preCheck = preCheck;
|
|
3153
3408
|
exports.safeParse = safeParse;
|
|
@@ -3160,6 +3415,7 @@ exports.useApplyCartCodes = useApplyCartCodes;
|
|
|
3160
3415
|
exports.useArticle = useArticle;
|
|
3161
3416
|
exports.useArticles = useArticles;
|
|
3162
3417
|
exports.useArticlesInBlog = useArticlesInBlog;
|
|
3418
|
+
exports.useAutoRemoveFreeGifts = useAutoRemoveFreeGifts;
|
|
3163
3419
|
exports.useAutoRemovePlusMemberInCart = useAutoRemovePlusMemberInCart;
|
|
3164
3420
|
exports.useAvailableDeliveryCoupon = useAvailableDeliveryCoupon;
|
|
3165
3421
|
exports.useBlog = useBlog;
|