@anker-in/shopify-react 0.1.1-beta.1 → 0.1.1-beta.3
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 +1 -1
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.js +242 -47
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +237 -46
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/{index-RevQokdZ.d.mts → index-Utuz9i5x.d.mts} +165 -49
- package/dist/{index-CCMIeIUh.d.ts → index-aSsTcW2O.d.ts} +165 -49
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +265 -57
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +260 -56
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +2 -0
- package/dist/provider/index.d.ts +2 -0
- package/dist/provider/index.js +138 -34
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +138 -34
- package/dist/provider/index.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -96,6 +96,8 @@ interface CartContextValue {
|
|
|
96
96
|
giftNeedAddToCartLines: any[];
|
|
97
97
|
/** Auto free gift config */
|
|
98
98
|
autoFreeGiftConfig?: any;
|
|
99
|
+
/** Gradient gifts config */
|
|
100
|
+
gradientGiftsConfig?: any;
|
|
99
101
|
/** Metafield identifiers */
|
|
100
102
|
metafieldIdentifiers?: {
|
|
101
103
|
variant: HasMetafieldsIdentifier[];
|
package/dist/provider/index.d.ts
CHANGED
|
@@ -96,6 +96,8 @@ interface CartContextValue {
|
|
|
96
96
|
giftNeedAddToCartLines: any[];
|
|
97
97
|
/** Auto free gift config */
|
|
98
98
|
autoFreeGiftConfig?: any;
|
|
99
|
+
/** Gradient gifts config */
|
|
100
|
+
gradientGiftsConfig?: any;
|
|
99
101
|
/** Metafield identifiers */
|
|
100
102
|
metafieldIdentifiers?: {
|
|
101
103
|
variant: HasMetafieldsIdentifier[];
|
package/dist/provider/index.js
CHANGED
|
@@ -104,6 +104,81 @@ var CODE_AMOUNT_KEY = "_sku_code_money";
|
|
|
104
104
|
var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
|
|
105
105
|
var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
|
|
106
106
|
|
|
107
|
+
// src/hooks/cart/utils/normalize-add-to-cart-lines.ts
|
|
108
|
+
function normalizeAddToCartLines(lines) {
|
|
109
|
+
return lines.filter((line) => line.variant?.id).map((line, index) => {
|
|
110
|
+
const variant = line.variant;
|
|
111
|
+
const product = variant.product;
|
|
112
|
+
const quantity = line.quantity || 1;
|
|
113
|
+
const price = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
|
|
114
|
+
const subtotalAmount = price * quantity;
|
|
115
|
+
const totalAmount = subtotalAmount;
|
|
116
|
+
return {
|
|
117
|
+
id: `temp-line-${index}-${variant.id}`,
|
|
118
|
+
// Temporary ID for pre-cart lines
|
|
119
|
+
name: product?.title || variant.title || "",
|
|
120
|
+
quantity,
|
|
121
|
+
variantId: variant.id,
|
|
122
|
+
productId: product?.id || variant.id.split("/").slice(0, -2).join("/"),
|
|
123
|
+
totalAmount,
|
|
124
|
+
subtotalAmount,
|
|
125
|
+
discountAllocations: [],
|
|
126
|
+
customAttributes: line.attributes || [],
|
|
127
|
+
variant: {
|
|
128
|
+
id: variant.id,
|
|
129
|
+
price,
|
|
130
|
+
listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
|
|
131
|
+
sku: variant.sku || "",
|
|
132
|
+
name: variant.title || "",
|
|
133
|
+
image: variant.image ? {
|
|
134
|
+
url: variant.image.url,
|
|
135
|
+
altText: variant.image.altText || void 0
|
|
136
|
+
} : void 0,
|
|
137
|
+
requiresShipping: false,
|
|
138
|
+
// Default value, not available in NormalizedProductVariant
|
|
139
|
+
availableForSale: variant.availableForSale ?? true,
|
|
140
|
+
quantityAvailable: variant.quantityAvailable ?? 0,
|
|
141
|
+
currentlyNotInStock: false,
|
|
142
|
+
// Default value, will be updated when added to cart
|
|
143
|
+
weight: variant.weight,
|
|
144
|
+
metafields: variant.metafields
|
|
145
|
+
},
|
|
146
|
+
product,
|
|
147
|
+
path: product?.handle ? `/products/${product.handle}` : "",
|
|
148
|
+
discounts: [],
|
|
149
|
+
options: variant.selectedOptions?.map((opt) => ({
|
|
150
|
+
name: opt.name,
|
|
151
|
+
value: opt.value
|
|
152
|
+
}))
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function createMockCartFromLines(lines, existingCart) {
|
|
157
|
+
const normalizedLines = normalizeAddToCartLines(lines);
|
|
158
|
+
const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
|
|
159
|
+
const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
|
|
160
|
+
return {
|
|
161
|
+
id: existingCart?.id || "temp-cart-id",
|
|
162
|
+
customerId: existingCart?.customerId,
|
|
163
|
+
email: existingCart?.email,
|
|
164
|
+
createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
165
|
+
currency: existingCart?.currency || { code: "USD" },
|
|
166
|
+
taxesIncluded: existingCart?.taxesIncluded,
|
|
167
|
+
lineItems: normalizedLines,
|
|
168
|
+
totallineItemsDiscount: 0,
|
|
169
|
+
orderDiscounts: 0,
|
|
170
|
+
lineItemsSubtotalPrice: subtotalPrice,
|
|
171
|
+
subtotalPrice,
|
|
172
|
+
totalPrice,
|
|
173
|
+
totalTaxAmount: 0,
|
|
174
|
+
discountCodes: existingCart?.discountCodes || [],
|
|
175
|
+
discountAllocations: [],
|
|
176
|
+
url: existingCart?.url || "",
|
|
177
|
+
ready: true,
|
|
178
|
+
customAttributes: existingCart?.customAttributes
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
107
182
|
// src/hooks/cart/utils/index.ts
|
|
108
183
|
var getQuery = () => {
|
|
109
184
|
const url = typeof window !== "undefined" ? window.location.search : "";
|
|
@@ -143,25 +218,25 @@ var getMatchedMainProductSubTotal = (cartData, variant_list, main_product) => {
|
|
|
143
218
|
return acc + (main_product?.spend_money_type === 1 /* ORIGIN_PRICE */ ? Number(line.subtotalAmount) || 0 : Number(line.totalAmount) || 0);
|
|
144
219
|
}, 0) || 0;
|
|
145
220
|
};
|
|
146
|
-
var
|
|
147
|
-
const attr = attributes.find((attr2) => attr2.key === CUSTOMER_ATTRIBUTE_KEY);
|
|
148
|
-
return safeParseJson(attr?.value ?? "") ?? {};
|
|
149
|
-
};
|
|
150
|
-
var isAttributesEqual = (attrs1 = [], attrs2 = []) => {
|
|
151
|
-
if (attrs1.length !== attrs2.length) return false;
|
|
152
|
-
const sorted1 = [...attrs1].sort((a, b) => a.key.localeCompare(b.key));
|
|
153
|
-
const sorted2 = [...attrs2].sort((a, b) => a.key.localeCompare(b.key));
|
|
154
|
-
return sorted1.every(
|
|
155
|
-
(attr, i) => attr.key === sorted2[i]?.key && attr.value === sorted2[i]?.value
|
|
156
|
-
);
|
|
157
|
-
};
|
|
158
|
-
var safeParseJson = (str) => {
|
|
221
|
+
var safeParse = (str) => {
|
|
159
222
|
try {
|
|
160
223
|
return JSON.parse(str);
|
|
161
224
|
} catch (err) {
|
|
162
225
|
return {};
|
|
163
226
|
}
|
|
164
227
|
};
|
|
228
|
+
var getDiscountEnvAttributeValue = (attributes = []) => {
|
|
229
|
+
const attr = attributes.find((attr2) => attr2.key === CUSTOMER_ATTRIBUTE_KEY);
|
|
230
|
+
return safeParse(attr?.value ?? "") ?? {};
|
|
231
|
+
};
|
|
232
|
+
var checkAttributesUpdateNeeded = (oldAttributes, newAttributes, customAttributesNeedRemove) => {
|
|
233
|
+
return oldAttributes.some((attr) => {
|
|
234
|
+
const newAttr = newAttributes.find((newAttr2) => newAttr2.key === attr.key);
|
|
235
|
+
return newAttr ? newAttr.value !== attr.value : true;
|
|
236
|
+
}) || newAttributes.some((attr) => !oldAttributes.some((oldAttr) => oldAttr.key === attr.key)) || customAttributesNeedRemove.some(
|
|
237
|
+
(removeAttr) => oldAttributes.some((oldAttr) => oldAttr.key === removeAttr.key)
|
|
238
|
+
);
|
|
239
|
+
};
|
|
165
240
|
var containsAll = (source, requiredItems = []) => {
|
|
166
241
|
if (!requiredItems?.length) return true;
|
|
167
242
|
const sourceSet = new Set(source);
|
|
@@ -328,12 +403,15 @@ var formatFunctionAutoFreeGift = ({
|
|
|
328
403
|
};
|
|
329
404
|
return result;
|
|
330
405
|
};
|
|
331
|
-
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
406
|
+
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
332
407
|
const tags = react.useMemo(() => customer?.tags || [], [customer?.tags]);
|
|
333
408
|
const isCustomerLoading = react.useMemo(() => !customer ? true : false, [customer]);
|
|
334
409
|
const dealsType = "";
|
|
335
410
|
const { client, locale } = useShopify();
|
|
336
411
|
const giftProductsCache = react.useRef(null);
|
|
412
|
+
const effectiveCart = react.useMemo(() => {
|
|
413
|
+
return cart;
|
|
414
|
+
}, [lines, cart]);
|
|
337
415
|
const { activeCampaign, subtotal } = react.useMemo(() => {
|
|
338
416
|
for (const campaign of autoFreeGiftConfig) {
|
|
339
417
|
const { rule_conditions = [], rule_result } = campaign;
|
|
@@ -341,7 +419,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
341
419
|
const isPreCheckPassed = preCheck(rule_conditions, tags, []);
|
|
342
420
|
if (isPreCheckPassed && spend_get_reward) {
|
|
343
421
|
const matchedSubtotal = getMatchedMainProductSubTotal(
|
|
344
|
-
|
|
422
|
+
effectiveCart,
|
|
345
423
|
spend_get_reward.main_product?.variant_list?.map((v) => v.variant_id) || [],
|
|
346
424
|
{
|
|
347
425
|
spend_money_type: spend_get_reward.main_product?.spend_money_type || 1,
|
|
@@ -355,7 +433,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
355
433
|
}
|
|
356
434
|
}
|
|
357
435
|
return { activeCampaign: null, subtotal: 0 };
|
|
358
|
-
}, [autoFreeGiftConfig,
|
|
436
|
+
}, [autoFreeGiftConfig, effectiveCart, tags, dealsType]);
|
|
359
437
|
const { qualifyingGift, nextTierGoal } = react.useMemo(() => {
|
|
360
438
|
if (!activeCampaign || !activeCampaign.rule_result?.spend_get_reward?.gift_product) {
|
|
361
439
|
return { qualifyingGift: null, nextTierGoal: null };
|
|
@@ -438,18 +516,33 @@ var useScriptAutoFreeGift = ({
|
|
|
438
516
|
campaign,
|
|
439
517
|
_giveaway,
|
|
440
518
|
cart,
|
|
441
|
-
locale: providedLocale
|
|
519
|
+
locale: providedLocale,
|
|
520
|
+
lines
|
|
442
521
|
}) => {
|
|
443
522
|
const { client, locale: contextLocale } = useShopify();
|
|
444
523
|
const locale = providedLocale || contextLocale;
|
|
445
524
|
const [points_subscribe, set_points_subscribe] = react.useState(false);
|
|
446
525
|
const giftProductsCache = react.useRef(null);
|
|
526
|
+
const effectiveCart = react.useMemo(() => {
|
|
527
|
+
if (lines && lines.length > 0) {
|
|
528
|
+
return createMockCartFromLines(lines, cart);
|
|
529
|
+
}
|
|
530
|
+
return cart;
|
|
531
|
+
}, [lines, cart]);
|
|
447
532
|
react.useEffect(() => {
|
|
448
533
|
if (locale === "au") {
|
|
449
534
|
const isPointsSubscribe = Cookies5__default.default.get("points_subscribe");
|
|
450
535
|
set_points_subscribe(!!isPointsSubscribe);
|
|
451
536
|
}
|
|
452
537
|
}, [locale]);
|
|
538
|
+
const isActivityAvailable = react.useMemo(() => {
|
|
539
|
+
if (!campaign) return false;
|
|
540
|
+
const query = getQuery();
|
|
541
|
+
const utmCampaign = Cookies5__default.default.get("utm_campaign") || query?.utm_campaign;
|
|
542
|
+
if (campaign.activityAvailableQuery && !utmCampaign?.includes(campaign.activityAvailableQuery))
|
|
543
|
+
return false;
|
|
544
|
+
return true;
|
|
545
|
+
}, [campaign]);
|
|
453
546
|
const [upgrade_multiple, upgrade_value] = react.useMemo(() => {
|
|
454
547
|
let upgrade_multiple2 = 1;
|
|
455
548
|
let upgrade_value2 = 0;
|
|
@@ -457,17 +550,17 @@ var useScriptAutoFreeGift = ({
|
|
|
457
550
|
upgrade_multiple2 = 1.2;
|
|
458
551
|
upgrade_value2 = 40;
|
|
459
552
|
}
|
|
460
|
-
|
|
553
|
+
effectiveCart?.lineItems?.forEach(({ customAttributes }) => {
|
|
461
554
|
customAttributes?.forEach(({ key, value }) => {
|
|
462
555
|
if (key === "_amount_upgrade_multiple") upgrade_multiple2 = Number(value) || 1;
|
|
463
556
|
if (key === "_amount_upgrade_value") upgrade_value2 = Number(value) || 0;
|
|
464
557
|
});
|
|
465
558
|
});
|
|
466
559
|
return [upgrade_multiple2, upgrade_value2];
|
|
467
|
-
}, [
|
|
560
|
+
}, [effectiveCart?.lineItems, points_subscribe]);
|
|
468
561
|
const breakpoints = react.useMemo(() => {
|
|
469
|
-
if (!
|
|
470
|
-
return (campaign
|
|
562
|
+
if (!isActivityAvailable) return [];
|
|
563
|
+
return (campaign?.breakpoints || []).map((item) => ({
|
|
471
564
|
breakpoint: new Decimal2__default.default(item.breakpoint).minus(new Decimal2__default.default(upgrade_value)).dividedBy(new Decimal2__default.default(upgrade_multiple)).toFixed(2, Decimal2__default.default.ROUND_DOWN),
|
|
472
565
|
giveawayProducts: item.giveawayProducts || []
|
|
473
566
|
}));
|
|
@@ -491,25 +584,26 @@ var useScriptAutoFreeGift = ({
|
|
|
491
584
|
return true;
|
|
492
585
|
}, [giftHandles]);
|
|
493
586
|
const involvedLines = react.useMemo(() => {
|
|
494
|
-
if (!
|
|
495
|
-
return (
|
|
587
|
+
if (!isActivityAvailable) return [];
|
|
588
|
+
return (effectiveCart?.lineItems || []).filter((line) => {
|
|
496
589
|
const isNotGift = line?.totalAmount && Number(line.totalAmount) > 0 && line.customAttributes?.every(
|
|
497
590
|
(item) => item.key !== _giveaway
|
|
498
591
|
);
|
|
499
592
|
const hasCampaignTag = line.product?.tags?.some(
|
|
500
|
-
(tag) => campaign
|
|
593
|
+
(tag) => campaign?.includeTags?.includes(tag.trim()) && line.variant?.availableForSale
|
|
501
594
|
);
|
|
502
595
|
return isNotGift && hasCampaignTag;
|
|
503
596
|
});
|
|
504
|
-
}, [
|
|
597
|
+
}, [effectiveCart?.lineItems, isActivityAvailable, _giveaway]);
|
|
505
598
|
const involvedSubTotal = react.useMemo(() => {
|
|
506
|
-
if (!
|
|
599
|
+
if (!isActivityAvailable) return new Decimal2__default.default(0);
|
|
507
600
|
return involvedLines.reduce((prev, item) => {
|
|
508
|
-
const amount = campaign
|
|
601
|
+
const amount = campaign?.useTotalAmount ? item.totalAmount : item.subtotalAmount;
|
|
509
602
|
return new Decimal2__default.default(prev).plus(new Decimal2__default.default(amount || 0));
|
|
510
603
|
}, new Decimal2__default.default(0));
|
|
511
|
-
}, [involvedLines,
|
|
604
|
+
}, [involvedLines, isActivityAvailable]);
|
|
512
605
|
const [freeGiftLevel, nextFreeGiftLevel] = react.useMemo(() => {
|
|
606
|
+
if (!isActivityAvailable) return [null, null];
|
|
513
607
|
const sortedLevels = [...breakpoints].sort(
|
|
514
608
|
(a, b) => Number(b.breakpoint) - Number(a.breakpoint)
|
|
515
609
|
);
|
|
@@ -933,6 +1027,9 @@ function CartProvider({
|
|
|
933
1027
|
}) {
|
|
934
1028
|
const { client, cartCookieAdapter } = useShopify();
|
|
935
1029
|
const [customAttributes, setCustomAttributes] = react.useState([]);
|
|
1030
|
+
const [customAttributesNeedDelete, setCustomAttributesNeedDelete] = react.useState(
|
|
1031
|
+
[]
|
|
1032
|
+
);
|
|
936
1033
|
const [isCodeChanging, setIsCodeChanging] = react.useState(false);
|
|
937
1034
|
const [loadingState, setLoadingState] = react.useState({
|
|
938
1035
|
editLineQuantityLoading: false,
|
|
@@ -963,7 +1060,11 @@ function CartProvider({
|
|
|
963
1060
|
ahooks.useRequest(
|
|
964
1061
|
() => {
|
|
965
1062
|
const newAttributes = [...attributes, ...customAttributes];
|
|
966
|
-
const needUpdate = cart && !
|
|
1063
|
+
const needUpdate = cart && !checkAttributesUpdateNeeded(
|
|
1064
|
+
cart.customAttributes,
|
|
1065
|
+
newAttributes,
|
|
1066
|
+
customAttributesNeedDelete
|
|
1067
|
+
);
|
|
967
1068
|
if (needUpdate) {
|
|
968
1069
|
return updateAttributes({ attributes: newAttributes });
|
|
969
1070
|
} else {
|
|
@@ -984,11 +1085,12 @@ function CartProvider({
|
|
|
984
1085
|
isCartLoading: isCartLoading || isCodeChanging,
|
|
985
1086
|
setLoadingState
|
|
986
1087
|
});
|
|
987
|
-
const removeCustomAttributes = react.useCallback(
|
|
988
|
-
|
|
989
|
-
(
|
|
990
|
-
|
|
991
|
-
|
|
1088
|
+
const removeCustomAttributes = react.useCallback(
|
|
1089
|
+
(attributes2) => {
|
|
1090
|
+
setCustomAttributesNeedDelete(attributes2);
|
|
1091
|
+
},
|
|
1092
|
+
[setCustomAttributesNeedDelete]
|
|
1093
|
+
);
|
|
992
1094
|
const addCustomAttributes = react.useCallback(
|
|
993
1095
|
(attributes2) => {
|
|
994
1096
|
const sameAttributes = attributes2.filter(
|
|
@@ -1075,6 +1177,7 @@ function CartProvider({
|
|
|
1075
1177
|
isCodeChanging,
|
|
1076
1178
|
setIsCodeChanging,
|
|
1077
1179
|
autoFreeGiftConfig,
|
|
1180
|
+
gradientGiftsConfig,
|
|
1078
1181
|
setLoadingState,
|
|
1079
1182
|
loadingState,
|
|
1080
1183
|
// function满赠
|
|
@@ -1098,6 +1201,7 @@ function CartProvider({
|
|
|
1098
1201
|
locale,
|
|
1099
1202
|
isCodeChanging,
|
|
1100
1203
|
autoFreeGiftConfig,
|
|
1204
|
+
gradientGiftsConfig,
|
|
1101
1205
|
loadingState,
|
|
1102
1206
|
// function满赠
|
|
1103
1207
|
functionAutoFreeGift,
|