@anker-in/shopify-react 0.1.1-beta.1 → 0.1.1-beta.11
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 +2 -2
- package/dist/hooks/index.d.ts +2 -2
- package/dist/hooks/index.js +337 -113
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +332 -113
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/{index-CCMIeIUh.d.ts → index-BOsx-Rx3.d.ts} +196 -64
- package/dist/{index-RevQokdZ.d.mts → index-D5VVTzBT.d.mts} +196 -64
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +360 -123
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +355 -123
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +3 -1
- package/dist/provider/index.d.ts +3 -1
- package/dist/provider/index.js +159 -42
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +159 -42
- package/dist/provider/index.mjs.map +1 -1
- package/dist/{types-CICUnw0v.d.mts → types-CUv-lzQk.d.mts} +5 -3
- package/dist/{types-CICUnw0v.d.ts → types-CUv-lzQk.d.ts} +5 -3
- package/package.json +3 -3
package/dist/hooks/index.js
CHANGED
|
@@ -71,6 +71,81 @@ var CODE_AMOUNT_KEY = "_sku_code_money";
|
|
|
71
71
|
var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
|
|
72
72
|
var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
|
|
73
73
|
|
|
74
|
+
// src/hooks/cart/utils/normalize-add-to-cart-lines.ts
|
|
75
|
+
function normalizeAddToCartLines(lines) {
|
|
76
|
+
return lines.filter((line) => line.variant?.id).map((line, index) => {
|
|
77
|
+
const variant = line.variant;
|
|
78
|
+
const product = variant.product;
|
|
79
|
+
const quantity = line.quantity || 1;
|
|
80
|
+
const price = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
|
|
81
|
+
const subtotalAmount = price * quantity;
|
|
82
|
+
const totalAmount = subtotalAmount;
|
|
83
|
+
return {
|
|
84
|
+
id: `temp-line-${index}-${variant.id}`,
|
|
85
|
+
// Temporary ID for pre-cart lines
|
|
86
|
+
name: product?.title || variant.title || "",
|
|
87
|
+
quantity,
|
|
88
|
+
variantId: variant.id,
|
|
89
|
+
productId: product?.id || variant.id.split("/").slice(0, -2).join("/"),
|
|
90
|
+
totalAmount,
|
|
91
|
+
subtotalAmount,
|
|
92
|
+
discountAllocations: [],
|
|
93
|
+
customAttributes: line.attributes || [],
|
|
94
|
+
variant: {
|
|
95
|
+
id: variant.id,
|
|
96
|
+
price,
|
|
97
|
+
listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
|
|
98
|
+
sku: variant.sku || "",
|
|
99
|
+
name: variant.title || "",
|
|
100
|
+
image: variant.image ? {
|
|
101
|
+
url: variant.image.url,
|
|
102
|
+
altText: variant.image.altText || void 0
|
|
103
|
+
} : void 0,
|
|
104
|
+
requiresShipping: false,
|
|
105
|
+
// Default value, not available in NormalizedProductVariant
|
|
106
|
+
availableForSale: variant.availableForSale ?? true,
|
|
107
|
+
quantityAvailable: variant.quantityAvailable ?? 0,
|
|
108
|
+
currentlyNotInStock: false,
|
|
109
|
+
// Default value, will be updated when added to cart
|
|
110
|
+
weight: variant.weight,
|
|
111
|
+
metafields: variant.metafields
|
|
112
|
+
},
|
|
113
|
+
product,
|
|
114
|
+
path: product?.handle ? `/products/${product.handle}` : "",
|
|
115
|
+
discounts: [],
|
|
116
|
+
options: variant.selectedOptions?.map((opt) => ({
|
|
117
|
+
name: opt.name,
|
|
118
|
+
value: opt.value
|
|
119
|
+
}))
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
function createMockCartFromLines(lines, existingCart) {
|
|
124
|
+
const normalizedLines = normalizeAddToCartLines(lines);
|
|
125
|
+
const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
|
|
126
|
+
const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
|
|
127
|
+
return {
|
|
128
|
+
id: existingCart?.id || "temp-cart-id",
|
|
129
|
+
customerId: existingCart?.customerId,
|
|
130
|
+
email: existingCart?.email,
|
|
131
|
+
createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
132
|
+
currency: existingCart?.currency || { code: "USD" },
|
|
133
|
+
taxesIncluded: existingCart?.taxesIncluded,
|
|
134
|
+
lineItems: normalizedLines,
|
|
135
|
+
totalLineItemsDiscount: 0,
|
|
136
|
+
orderDiscounts: 0,
|
|
137
|
+
lineItemsSubtotalPrice: subtotalPrice,
|
|
138
|
+
subtotalPrice,
|
|
139
|
+
totalPrice,
|
|
140
|
+
totalTaxAmount: 0,
|
|
141
|
+
discountCodes: existingCart?.discountCodes || [],
|
|
142
|
+
discountAllocations: [],
|
|
143
|
+
url: existingCart?.url || "",
|
|
144
|
+
ready: true,
|
|
145
|
+
customAttributes: existingCart?.customAttributes
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
74
149
|
// src/hooks/cart/utils/index.ts
|
|
75
150
|
var getQuery = () => {
|
|
76
151
|
const url = typeof window !== "undefined" ? window.location.search : "";
|
|
@@ -110,25 +185,25 @@ var getMatchedMainProductSubTotal = (cartData, variant_list, main_product) => {
|
|
|
110
185
|
return acc + (main_product?.spend_money_type === 1 /* ORIGIN_PRICE */ ? Number(line.subtotalAmount) || 0 : Number(line.totalAmount) || 0);
|
|
111
186
|
}, 0) || 0;
|
|
112
187
|
};
|
|
113
|
-
var
|
|
114
|
-
const attr = attributes.find((attr2) => attr2.key === CUSTOMER_ATTRIBUTE_KEY);
|
|
115
|
-
return safeParseJson(attr?.value ?? "") ?? {};
|
|
116
|
-
};
|
|
117
|
-
var isAttributesEqual = (attrs1 = [], attrs2 = []) => {
|
|
118
|
-
if (attrs1.length !== attrs2.length) return false;
|
|
119
|
-
const sorted1 = [...attrs1].sort((a, b) => a.key.localeCompare(b.key));
|
|
120
|
-
const sorted2 = [...attrs2].sort((a, b) => a.key.localeCompare(b.key));
|
|
121
|
-
return sorted1.every(
|
|
122
|
-
(attr, i) => attr.key === sorted2[i]?.key && attr.value === sorted2[i]?.value
|
|
123
|
-
);
|
|
124
|
-
};
|
|
125
|
-
var safeParseJson = (str) => {
|
|
188
|
+
var safeParse = (str) => {
|
|
126
189
|
try {
|
|
127
190
|
return JSON.parse(str);
|
|
128
191
|
} catch (err) {
|
|
129
192
|
return {};
|
|
130
193
|
}
|
|
131
194
|
};
|
|
195
|
+
var getDiscountEnvAttributeValue = (attributes = []) => {
|
|
196
|
+
const attr = attributes.find((attr2) => attr2.key === CUSTOMER_ATTRIBUTE_KEY);
|
|
197
|
+
return safeParse(attr?.value ?? "") ?? {};
|
|
198
|
+
};
|
|
199
|
+
var checkAttributesUpdateNeeded = (oldAttributes, newAttributes, customAttributesNeedRemove) => {
|
|
200
|
+
return oldAttributes.some((attr) => {
|
|
201
|
+
const newAttr = newAttributes.find((newAttr2) => newAttr2.key === attr.key);
|
|
202
|
+
return newAttr ? newAttr.value !== attr.value : true;
|
|
203
|
+
}) || newAttributes.some((attr) => !oldAttributes.some((oldAttr) => oldAttr.key === attr.key)) || customAttributesNeedRemove.some(
|
|
204
|
+
(removeAttr) => oldAttributes.some((oldAttr) => oldAttr.key === removeAttr.key)
|
|
205
|
+
);
|
|
206
|
+
};
|
|
132
207
|
var containsAll = (source, requiredItems = []) => {
|
|
133
208
|
if (!requiredItems?.length) return true;
|
|
134
209
|
const sourceSet = new Set(source);
|
|
@@ -295,12 +370,18 @@ var formatFunctionAutoFreeGift = ({
|
|
|
295
370
|
};
|
|
296
371
|
return result;
|
|
297
372
|
};
|
|
298
|
-
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
373
|
+
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
299
374
|
const tags = react.useMemo(() => customer?.tags || [], [customer?.tags]);
|
|
300
375
|
const isCustomerLoading = react.useMemo(() => !customer ? true : false, [customer]);
|
|
301
376
|
const dealsType = "";
|
|
302
377
|
const { client, locale } = useShopify();
|
|
303
378
|
const giftProductsCache = react.useRef(null);
|
|
379
|
+
const effectiveCart = react.useMemo(() => {
|
|
380
|
+
if (lines && lines.length > 0) {
|
|
381
|
+
return createMockCartFromLines(lines, cart);
|
|
382
|
+
}
|
|
383
|
+
return cart;
|
|
384
|
+
}, [lines, cart]);
|
|
304
385
|
const { activeCampaign, subtotal } = react.useMemo(() => {
|
|
305
386
|
for (const campaign of autoFreeGiftConfig) {
|
|
306
387
|
const { rule_conditions = [], rule_result } = campaign;
|
|
@@ -308,7 +389,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
308
389
|
const isPreCheckPassed = preCheck(rule_conditions, tags, []);
|
|
309
390
|
if (isPreCheckPassed && spend_get_reward) {
|
|
310
391
|
const matchedSubtotal = getMatchedMainProductSubTotal(
|
|
311
|
-
|
|
392
|
+
effectiveCart,
|
|
312
393
|
spend_get_reward.main_product?.variant_list?.map((v) => v.variant_id) || [],
|
|
313
394
|
{
|
|
314
395
|
spend_money_type: spend_get_reward.main_product?.spend_money_type || 1,
|
|
@@ -322,13 +403,13 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
322
403
|
}
|
|
323
404
|
}
|
|
324
405
|
return { activeCampaign: null, subtotal: 0 };
|
|
325
|
-
}, [autoFreeGiftConfig,
|
|
406
|
+
}, [autoFreeGiftConfig, effectiveCart, tags, dealsType]);
|
|
326
407
|
const { qualifyingGift, nextTierGoal } = react.useMemo(() => {
|
|
327
408
|
if (!activeCampaign || !activeCampaign.rule_result?.spend_get_reward?.gift_product) {
|
|
328
409
|
return { qualifyingGift: null, nextTierGoal: null };
|
|
329
410
|
}
|
|
330
411
|
const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
|
|
331
|
-
const qualifyingTier = [...giftTiers].
|
|
412
|
+
const qualifyingTier = [...giftTiers].sort((a, b) => Number(b.spend_sum_money) - Number(a.spend_sum_money)).find((tier) => subtotal >= Number(tier.spend_sum_money));
|
|
332
413
|
const nextGoal = giftTiers.find((tier) => subtotal < Number(tier.spend_sum_money));
|
|
333
414
|
if (!qualifyingTier) {
|
|
334
415
|
return { qualifyingGift: null, nextTierGoal: nextGoal || null };
|
|
@@ -405,18 +486,33 @@ var useScriptAutoFreeGift = ({
|
|
|
405
486
|
campaign,
|
|
406
487
|
_giveaway,
|
|
407
488
|
cart,
|
|
408
|
-
locale: providedLocale
|
|
489
|
+
locale: providedLocale,
|
|
490
|
+
lines
|
|
409
491
|
}) => {
|
|
410
492
|
const { client, locale: contextLocale } = useShopify();
|
|
411
493
|
const locale = providedLocale || contextLocale;
|
|
412
494
|
const [points_subscribe, set_points_subscribe] = react.useState(false);
|
|
413
495
|
const giftProductsCache = react.useRef(null);
|
|
496
|
+
const effectiveCart = react.useMemo(() => {
|
|
497
|
+
if (lines && lines.length > 0) {
|
|
498
|
+
return createMockCartFromLines(lines, cart);
|
|
499
|
+
}
|
|
500
|
+
return cart;
|
|
501
|
+
}, [lines, cart]);
|
|
414
502
|
react.useEffect(() => {
|
|
415
503
|
if (locale === "au") {
|
|
416
504
|
const isPointsSubscribe = Cookies5__default.default.get("points_subscribe");
|
|
417
505
|
set_points_subscribe(!!isPointsSubscribe);
|
|
418
506
|
}
|
|
419
507
|
}, [locale]);
|
|
508
|
+
const isActivityAvailable = react.useMemo(() => {
|
|
509
|
+
if (!campaign) return false;
|
|
510
|
+
const query = getQuery();
|
|
511
|
+
const utmCampaign = Cookies5__default.default.get("utm_campaign") || query?.utm_campaign;
|
|
512
|
+
if (campaign.activityAvailableQuery && !utmCampaign?.includes(campaign.activityAvailableQuery))
|
|
513
|
+
return false;
|
|
514
|
+
return true;
|
|
515
|
+
}, [campaign]);
|
|
420
516
|
const [upgrade_multiple, upgrade_value] = react.useMemo(() => {
|
|
421
517
|
let upgrade_multiple2 = 1;
|
|
422
518
|
let upgrade_value2 = 0;
|
|
@@ -424,17 +520,17 @@ var useScriptAutoFreeGift = ({
|
|
|
424
520
|
upgrade_multiple2 = 1.2;
|
|
425
521
|
upgrade_value2 = 40;
|
|
426
522
|
}
|
|
427
|
-
|
|
523
|
+
effectiveCart?.lineItems?.forEach(({ customAttributes }) => {
|
|
428
524
|
customAttributes?.forEach(({ key, value }) => {
|
|
429
525
|
if (key === "_amount_upgrade_multiple") upgrade_multiple2 = Number(value) || 1;
|
|
430
526
|
if (key === "_amount_upgrade_value") upgrade_value2 = Number(value) || 0;
|
|
431
527
|
});
|
|
432
528
|
});
|
|
433
529
|
return [upgrade_multiple2, upgrade_value2];
|
|
434
|
-
}, [
|
|
530
|
+
}, [effectiveCart?.lineItems, points_subscribe]);
|
|
435
531
|
const breakpoints = react.useMemo(() => {
|
|
436
|
-
if (!
|
|
437
|
-
return (campaign
|
|
532
|
+
if (!isActivityAvailable) return [];
|
|
533
|
+
return (campaign?.breakpoints || []).map((item) => ({
|
|
438
534
|
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),
|
|
439
535
|
giveawayProducts: item.giveawayProducts || []
|
|
440
536
|
}));
|
|
@@ -458,25 +554,26 @@ var useScriptAutoFreeGift = ({
|
|
|
458
554
|
return true;
|
|
459
555
|
}, [giftHandles]);
|
|
460
556
|
const involvedLines = react.useMemo(() => {
|
|
461
|
-
if (!
|
|
462
|
-
return (
|
|
557
|
+
if (!isActivityAvailable) return [];
|
|
558
|
+
return (effectiveCart?.lineItems || []).filter((line) => {
|
|
463
559
|
const isNotGift = line?.totalAmount && Number(line.totalAmount) > 0 && line.customAttributes?.every(
|
|
464
560
|
(item) => item.key !== _giveaway
|
|
465
561
|
);
|
|
466
562
|
const hasCampaignTag = line.product?.tags?.some(
|
|
467
|
-
(tag) => campaign
|
|
563
|
+
(tag) => campaign?.includeTags?.includes(tag.trim()) && line.variant?.availableForSale
|
|
468
564
|
);
|
|
469
565
|
return isNotGift && hasCampaignTag;
|
|
470
566
|
});
|
|
471
|
-
}, [
|
|
567
|
+
}, [effectiveCart?.lineItems, isActivityAvailable, _giveaway]);
|
|
472
568
|
const involvedSubTotal = react.useMemo(() => {
|
|
473
|
-
if (!
|
|
569
|
+
if (!isActivityAvailable) return new Decimal2__default.default(0);
|
|
474
570
|
return involvedLines.reduce((prev, item) => {
|
|
475
|
-
const amount = campaign
|
|
571
|
+
const amount = campaign?.useTotalAmount ? item.totalAmount : item.subtotalAmount;
|
|
476
572
|
return new Decimal2__default.default(prev).plus(new Decimal2__default.default(amount || 0));
|
|
477
573
|
}, new Decimal2__default.default(0));
|
|
478
|
-
}, [involvedLines,
|
|
574
|
+
}, [involvedLines, isActivityAvailable]);
|
|
479
575
|
const [freeGiftLevel, nextFreeGiftLevel] = react.useMemo(() => {
|
|
576
|
+
if (!isActivityAvailable) return [null, null];
|
|
480
577
|
const sortedLevels = [...breakpoints].sort(
|
|
481
578
|
(a, b) => Number(b.breakpoint) - Number(a.breakpoint)
|
|
482
579
|
);
|
|
@@ -630,8 +727,12 @@ var trackAddToCartGA = ({
|
|
|
630
727
|
}
|
|
631
728
|
const { variant } = lineItems[0];
|
|
632
729
|
const currencyCode = variant.product?.price?.currencyCode;
|
|
633
|
-
const
|
|
634
|
-
|
|
730
|
+
const totalPrice = lineItems?.reduce(
|
|
731
|
+
(prev, { variant: variant2 }) => prev.plus(
|
|
732
|
+
variant2?.finalPrice?.amount ?? variant2?.compareAtPrice?.amount ?? variant2?.price?.amount ?? 0
|
|
733
|
+
),
|
|
734
|
+
new Decimal2__default.default(0)
|
|
735
|
+
).toNumber();
|
|
635
736
|
gaTrack({
|
|
636
737
|
event: "ga4Event",
|
|
637
738
|
event_name: "add_to_cart",
|
|
@@ -646,7 +747,7 @@ var trackAddToCartGA = ({
|
|
|
646
747
|
item_brand: gtmParams?.brand || "",
|
|
647
748
|
item_category: variant2?.product?.productType || "",
|
|
648
749
|
item_variant: variant2?.title || variant2?.title,
|
|
649
|
-
price,
|
|
750
|
+
price: variant2?.compareAtPrice?.amount ?? variant2?.price?.amount,
|
|
650
751
|
quantity: quantity || 1
|
|
651
752
|
})),
|
|
652
753
|
...gtmParams?.ga4Params
|
|
@@ -662,8 +763,12 @@ var trackBuyNowGA = ({
|
|
|
662
763
|
}
|
|
663
764
|
const { variant } = lineItems[0];
|
|
664
765
|
const currencyCode = variant.price?.currencyCode;
|
|
665
|
-
const
|
|
666
|
-
|
|
766
|
+
const totalPrice = lineItems?.reduce(
|
|
767
|
+
(prev, { variant: variant2 }) => prev.plus(
|
|
768
|
+
variant2?.finalPrice?.amount ?? variant2?.compareAtPrice?.amount ?? (variant2?.price?.amount || 0)
|
|
769
|
+
),
|
|
770
|
+
new Decimal2__default.default(0)
|
|
771
|
+
).toNumber();
|
|
667
772
|
gaTrack({
|
|
668
773
|
event: "ga4Event",
|
|
669
774
|
event_name: "begin_checkout",
|
|
@@ -678,7 +783,7 @@ var trackBuyNowGA = ({
|
|
|
678
783
|
item_brand: gtmParams?.brand || "",
|
|
679
784
|
item_category: item.variant?.product?.productType || "",
|
|
680
785
|
item_variant: item.variant?.title,
|
|
681
|
-
price,
|
|
786
|
+
price: item.variant?.compareAtPrice?.amount ?? item.variant?.price?.amount,
|
|
682
787
|
quantity: item.quantity || 1
|
|
683
788
|
})),
|
|
684
789
|
...gtmParams?.ga4Params
|
|
@@ -821,6 +926,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
821
926
|
if (!resultCart) {
|
|
822
927
|
return void 0;
|
|
823
928
|
}
|
|
929
|
+
console.log("npm addCartLines resultCart", resultCart);
|
|
824
930
|
if (resultCart.discountCodes && resultCart.discountCodes.length > 0) {
|
|
825
931
|
const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
826
932
|
if (unapplicableCodes.length > 0) {
|
|
@@ -994,6 +1100,60 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
994
1100
|
);
|
|
995
1101
|
return useSWRMutation__default.default("buy-now", buyNow, swrOptions);
|
|
996
1102
|
}
|
|
1103
|
+
function useCalcGiftsFromLines({
|
|
1104
|
+
lines,
|
|
1105
|
+
customer,
|
|
1106
|
+
scriptGiveawayKey = CUSTOMER_SCRIPT_GIFT_KEY
|
|
1107
|
+
}) {
|
|
1108
|
+
const { locale } = useShopify();
|
|
1109
|
+
const { cart, autoFreeGiftConfig, gradientGiftsConfig } = useCartContext();
|
|
1110
|
+
const functionGift = useCalcAutoFreeGift(cart, autoFreeGiftConfig || [], customer, lines);
|
|
1111
|
+
const scriptGift = useScriptAutoFreeGift({
|
|
1112
|
+
campaign: gradientGiftsConfig || null,
|
|
1113
|
+
_giveaway: scriptGiveawayKey,
|
|
1114
|
+
cart,
|
|
1115
|
+
locale,
|
|
1116
|
+
lines
|
|
1117
|
+
});
|
|
1118
|
+
const allGiftLines = react.useMemo(() => {
|
|
1119
|
+
const functionGiftLines = functionGift.qualifyingGift?.itemsToAdd || [];
|
|
1120
|
+
const scriptGiftLines = scriptGift.freeGiftLevel ? scriptGift.freeGiftLevel.giveawayProducts.map((product) => {
|
|
1121
|
+
const giftProduct = scriptGift.giftProductsResult?.find(
|
|
1122
|
+
(p) => p.handle === product.handle
|
|
1123
|
+
);
|
|
1124
|
+
const variant = giftProduct?.variants?.[0];
|
|
1125
|
+
return {
|
|
1126
|
+
variant: {
|
|
1127
|
+
id: variant?.id || "",
|
|
1128
|
+
handle: product.handle,
|
|
1129
|
+
sku: product.sku
|
|
1130
|
+
},
|
|
1131
|
+
quantity: 1,
|
|
1132
|
+
attributes: [
|
|
1133
|
+
{
|
|
1134
|
+
key: scriptGiveawayKey,
|
|
1135
|
+
value: "true"
|
|
1136
|
+
}
|
|
1137
|
+
]
|
|
1138
|
+
};
|
|
1139
|
+
}).filter((item) => item.variant.id) : [];
|
|
1140
|
+
return [...functionGiftLines, ...scriptGiftLines];
|
|
1141
|
+
}, [
|
|
1142
|
+
functionGift.qualifyingGift,
|
|
1143
|
+
scriptGift.freeGiftLevel,
|
|
1144
|
+
scriptGift.giftProductsResult,
|
|
1145
|
+
scriptGiveawayKey
|
|
1146
|
+
]);
|
|
1147
|
+
const hasGifts = react.useMemo(() => {
|
|
1148
|
+
return allGiftLines.length > 0;
|
|
1149
|
+
}, [allGiftLines]);
|
|
1150
|
+
return {
|
|
1151
|
+
functionGift,
|
|
1152
|
+
scriptGift,
|
|
1153
|
+
allGiftLines,
|
|
1154
|
+
hasGifts
|
|
1155
|
+
};
|
|
1156
|
+
}
|
|
997
1157
|
|
|
998
1158
|
// src/hooks/cart/types/order-discount.ts
|
|
999
1159
|
var OrderDiscountType = /* @__PURE__ */ ((OrderDiscountType2) => {
|
|
@@ -1143,8 +1303,6 @@ var useCartAttributes = ({
|
|
|
1143
1303
|
memberSetting,
|
|
1144
1304
|
cart
|
|
1145
1305
|
});
|
|
1146
|
-
console.log("memberSetting", memberSetting);
|
|
1147
|
-
console.log("hasPlusMember", hasPlusMember);
|
|
1148
1306
|
react.useEffect(() => {
|
|
1149
1307
|
setCurrentUrl(window.location.href);
|
|
1150
1308
|
}, []);
|
|
@@ -1170,7 +1328,7 @@ var useCartAttributes = ({
|
|
|
1170
1328
|
return "new_user_login";
|
|
1171
1329
|
}, [customer]);
|
|
1172
1330
|
const memberAttributes = react.useMemo(() => {
|
|
1173
|
-
|
|
1331
|
+
const attributes = [
|
|
1174
1332
|
{
|
|
1175
1333
|
key: "_token",
|
|
1176
1334
|
value: profile?.token
|
|
@@ -1189,19 +1347,34 @@ var useCartAttributes = ({
|
|
|
1189
1347
|
{
|
|
1190
1348
|
key: "_is_login",
|
|
1191
1349
|
value: profile?.token ? "true" : "false"
|
|
1350
|
+
},
|
|
1351
|
+
{
|
|
1352
|
+
key: "_last_url",
|
|
1353
|
+
value: typeof window !== "undefined" ? window.location.origin + window.location.pathname : ""
|
|
1192
1354
|
}
|
|
1193
1355
|
];
|
|
1356
|
+
if (profile?.token) {
|
|
1357
|
+
attributes.push({
|
|
1358
|
+
key: "_login_user",
|
|
1359
|
+
value: "1"
|
|
1360
|
+
});
|
|
1361
|
+
}
|
|
1362
|
+
return attributes;
|
|
1194
1363
|
}, [profile?.memberType, profile?.token, userType, hasPlusMember]);
|
|
1195
1364
|
const functionAttributes = react.useMemo(() => {
|
|
1196
|
-
|
|
1197
|
-
|
|
1365
|
+
const hasFunctionEnvAttribute = cart?.lineItems.some(
|
|
1366
|
+
(item) => item.customAttributes?.some((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY)
|
|
1367
|
+
);
|
|
1368
|
+
const discountCodes = cart?.discountCodes.map((item) => item.code).filter((code) => code) || [];
|
|
1369
|
+
return hasFunctionEnvAttribute ? [
|
|
1370
|
+
{
|
|
1198
1371
|
key: "_discounts_function_env",
|
|
1199
1372
|
value: JSON.stringify({
|
|
1200
|
-
discount_code:
|
|
1373
|
+
discount_code: discountCodes,
|
|
1201
1374
|
user_tags: customer?.tags || []
|
|
1202
1375
|
})
|
|
1203
1376
|
}
|
|
1204
|
-
];
|
|
1377
|
+
] : [];
|
|
1205
1378
|
}, [cart]);
|
|
1206
1379
|
const presellAttributes = react.useMemo(() => {
|
|
1207
1380
|
return [
|
|
@@ -2378,6 +2551,69 @@ var usePlusMemberDeliveryCodes = ({
|
|
|
2378
2551
|
[deliveryData]
|
|
2379
2552
|
);
|
|
2380
2553
|
};
|
|
2554
|
+
function useUpdateCartDeliveryOptions(mutate, metafieldIdentifiers, options) {
|
|
2555
|
+
const { client, locale, cartCookieAdapter } = useShopify();
|
|
2556
|
+
const updateDeliveryOptions = react.useCallback(
|
|
2557
|
+
async (_key, { arg }) => {
|
|
2558
|
+
const updatedCart = await shopifySdk.updateCartDeliveryOptions(client, {
|
|
2559
|
+
...arg,
|
|
2560
|
+
metafieldIdentifiers,
|
|
2561
|
+
cookieAdapter: cartCookieAdapter
|
|
2562
|
+
});
|
|
2563
|
+
console.log("useUpdateCartDeliveryOptions updatedCart", updatedCart);
|
|
2564
|
+
if (updatedCart) {
|
|
2565
|
+
mutate(updatedCart);
|
|
2566
|
+
}
|
|
2567
|
+
return updatedCart;
|
|
2568
|
+
},
|
|
2569
|
+
[client, locale, cartCookieAdapter, mutate]
|
|
2570
|
+
);
|
|
2571
|
+
return useSWRMutation__default.default("update-cart-delivery-options", updateDeliveryOptions, options);
|
|
2572
|
+
}
|
|
2573
|
+
|
|
2574
|
+
// src/hooks/member/plus/use-update-plus-member-delivery-options.ts
|
|
2575
|
+
var useUpdatePlusMemberDeliveryOptions = ({
|
|
2576
|
+
options
|
|
2577
|
+
}) => {
|
|
2578
|
+
const { cart, mutateCart: mutate, metafieldIdentifiers } = useCartContext();
|
|
2579
|
+
const { trigger: updateCartDeliveryOptions2, isMutating } = useUpdateCartDeliveryOptions(
|
|
2580
|
+
mutate,
|
|
2581
|
+
metafieldIdentifiers
|
|
2582
|
+
);
|
|
2583
|
+
const handler = react.useCallback(
|
|
2584
|
+
async (_, { arg }) => {
|
|
2585
|
+
const { deliveryData } = arg;
|
|
2586
|
+
const firstDeliveryGroup = cart?.deliveryGroups?.[0];
|
|
2587
|
+
const deliveryGroupId = firstDeliveryGroup?.id;
|
|
2588
|
+
const selectedOptionCode = deliveryData?.deliveryCustomData?.selected_delivery_option?.code;
|
|
2589
|
+
if (!deliveryGroupId || !selectedOptionCode || selectedOptionCode === firstDeliveryGroup?.selectedDeliveryOption?.code) {
|
|
2590
|
+
return null;
|
|
2591
|
+
}
|
|
2592
|
+
const deliveryGroup = cart?.deliveryGroups?.find((group) => group?.id === deliveryGroupId);
|
|
2593
|
+
const matchedOption = deliveryGroup?.deliveryOptions?.find(
|
|
2594
|
+
(option) => option?.code === selectedOptionCode
|
|
2595
|
+
);
|
|
2596
|
+
if (!matchedOption?.handle) {
|
|
2597
|
+
return null;
|
|
2598
|
+
}
|
|
2599
|
+
const deliveryOptions = [
|
|
2600
|
+
{
|
|
2601
|
+
deliveryGroupId,
|
|
2602
|
+
deliveryOptionHandle: matchedOption.handle
|
|
2603
|
+
}
|
|
2604
|
+
];
|
|
2605
|
+
const updatedCart = await updateCartDeliveryOptions2({
|
|
2606
|
+
selectedDeliveryOptions: deliveryOptions
|
|
2607
|
+
});
|
|
2608
|
+
if (updatedCart && mutate) {
|
|
2609
|
+
mutate(updatedCart);
|
|
2610
|
+
}
|
|
2611
|
+
return updatedCart;
|
|
2612
|
+
},
|
|
2613
|
+
[cart, updateCartDeliveryOptions2, mutate]
|
|
2614
|
+
);
|
|
2615
|
+
return useSWRMutation__default.default("update-cart-delivery-options", handler, options);
|
|
2616
|
+
};
|
|
2381
2617
|
var usePlusMemberItemCustomAttributes = ({
|
|
2382
2618
|
deliveryData
|
|
2383
2619
|
}) => {
|
|
@@ -2397,48 +2633,12 @@ var usePlusMemberCheckoutCustomAttributes = ({
|
|
|
2397
2633
|
deliveryData,
|
|
2398
2634
|
product,
|
|
2399
2635
|
variant,
|
|
2400
|
-
customer,
|
|
2401
2636
|
isShowShippingBenefits
|
|
2402
2637
|
}) => {
|
|
2403
2638
|
const { deliveryCustomData } = deliveryData || {};
|
|
2404
2639
|
const { profile } = usePlusMemberContext();
|
|
2405
|
-
const userType = react.useMemo(() => {
|
|
2406
|
-
const customerInfo = customer;
|
|
2407
|
-
if (!customerInfo) {
|
|
2408
|
-
return "new_user_unlogin";
|
|
2409
|
-
}
|
|
2410
|
-
if (customer) {
|
|
2411
|
-
const { orders = {} } = customer;
|
|
2412
|
-
const edgesLength = orders?.edges?.length;
|
|
2413
|
-
if (edgesLength === 1) {
|
|
2414
|
-
return "old_user_orders_once";
|
|
2415
|
-
} else if (edgesLength && edgesLength > 1) {
|
|
2416
|
-
return "old_user_orders_twice";
|
|
2417
|
-
}
|
|
2418
|
-
}
|
|
2419
|
-
return "new_user_login";
|
|
2420
|
-
}, [customer]);
|
|
2421
2640
|
return react.useMemo(() => {
|
|
2422
|
-
const checkoutCustomAttributes = [
|
|
2423
|
-
{
|
|
2424
|
-
key: "_token",
|
|
2425
|
-
value: profile?.token || ""
|
|
2426
|
-
},
|
|
2427
|
-
{
|
|
2428
|
-
key: "_last_url",
|
|
2429
|
-
value: typeof window !== "undefined" ? window.location.origin + window.location.pathname : ""
|
|
2430
|
-
},
|
|
2431
|
-
{
|
|
2432
|
-
key: "_user_type",
|
|
2433
|
-
value: userType
|
|
2434
|
-
}
|
|
2435
|
-
];
|
|
2436
|
-
if (profile) {
|
|
2437
|
-
checkoutCustomAttributes.push({
|
|
2438
|
-
key: "_login_user",
|
|
2439
|
-
value: "1"
|
|
2440
|
-
});
|
|
2441
|
-
}
|
|
2641
|
+
const checkoutCustomAttributes = [];
|
|
2442
2642
|
if (deliveryCustomData) {
|
|
2443
2643
|
checkoutCustomAttributes.push({
|
|
2444
2644
|
key: "_checkout_delivery_custom",
|
|
@@ -2448,12 +2648,6 @@ var usePlusMemberCheckoutCustomAttributes = ({
|
|
|
2448
2648
|
})
|
|
2449
2649
|
});
|
|
2450
2650
|
}
|
|
2451
|
-
if (variant?.metafields?.presell) {
|
|
2452
|
-
checkoutCustomAttributes.push({
|
|
2453
|
-
key: "_presale",
|
|
2454
|
-
value: "true"
|
|
2455
|
-
});
|
|
2456
|
-
}
|
|
2457
2651
|
if (isShowShippingBenefits && !isShowShippingBenefits({ variant, product, setting: {} })) {
|
|
2458
2652
|
checkoutCustomAttributes.push({
|
|
2459
2653
|
key: "_hide_shipping",
|
|
@@ -2461,18 +2655,17 @@ var usePlusMemberCheckoutCustomAttributes = ({
|
|
|
2461
2655
|
});
|
|
2462
2656
|
}
|
|
2463
2657
|
return checkoutCustomAttributes;
|
|
2464
|
-
}, [deliveryCustomData, product, profile,
|
|
2658
|
+
}, [deliveryCustomData, product, profile, variant, isShowShippingBenefits]);
|
|
2465
2659
|
};
|
|
2466
2660
|
function useAutoRemovePlusMemberInCart({
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2661
|
+
cart,
|
|
2662
|
+
profile,
|
|
2663
|
+
memberSetting
|
|
2470
2664
|
}) {
|
|
2471
|
-
const { plus_monthly_product, plus_annual_product } =
|
|
2472
|
-
const { cart } = useCartContext();
|
|
2665
|
+
const { plus_monthly_product, plus_annual_product } = memberSetting || {};
|
|
2473
2666
|
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
2474
2667
|
react.useEffect(() => {
|
|
2475
|
-
if (!cart) return;
|
|
2668
|
+
if (!cart || !plus_monthly_product || !plus_annual_product) return;
|
|
2476
2669
|
const removePlusProduct = async (productType) => {
|
|
2477
2670
|
if (!productType) return;
|
|
2478
2671
|
const product = cart.lineItems?.find(
|
|
@@ -2484,26 +2677,53 @@ function useAutoRemovePlusMemberInCart({
|
|
|
2484
2677
|
});
|
|
2485
2678
|
}
|
|
2486
2679
|
};
|
|
2487
|
-
if (isMonthlyPlus) {
|
|
2680
|
+
if (profile?.isMonthlyPlus) {
|
|
2488
2681
|
removePlusProduct(plus_monthly_product);
|
|
2489
2682
|
}
|
|
2490
|
-
if (isAnnualPlus) {
|
|
2683
|
+
if (profile?.isAnnualPlus) {
|
|
2491
2684
|
removePlusProduct(plus_annual_product);
|
|
2492
2685
|
}
|
|
2686
|
+
}, [cart, plus_annual_product, plus_monthly_product, profile, removeCartLines2]);
|
|
2687
|
+
}
|
|
2688
|
+
function useAddPlusMemberProductsToCart({
|
|
2689
|
+
cart,
|
|
2690
|
+
profile
|
|
2691
|
+
}) {
|
|
2692
|
+
const { selectedPlusMemberMode, selectedPlusMemberProduct, plusMemberMetafields } = usePlusMemberContext();
|
|
2693
|
+
const { hasMonthlyPlus, hasAnnualPlus } = useHasPlusMemberInCart({
|
|
2694
|
+
memberSetting: plusMemberMetafields,
|
|
2695
|
+
cart
|
|
2696
|
+
});
|
|
2697
|
+
const plusMemberProduct = react.useMemo(() => {
|
|
2698
|
+
if (!selectedPlusMemberProduct || selectedPlusMemberMode === "free" /* FREE */) {
|
|
2699
|
+
return void 0;
|
|
2700
|
+
}
|
|
2701
|
+
if (selectedPlusMemberMode === "monthly" /* MONTHLY */ && hasMonthlyPlus) {
|
|
2702
|
+
return void 0;
|
|
2703
|
+
}
|
|
2704
|
+
if (selectedPlusMemberMode === "annual" /* ANNUAL */ && hasAnnualPlus) {
|
|
2705
|
+
return void 0;
|
|
2706
|
+
}
|
|
2707
|
+
if (profile?.isMonthlyPlus && selectedPlusMemberMode === "monthly" /* MONTHLY */) {
|
|
2708
|
+
return void 0;
|
|
2709
|
+
}
|
|
2710
|
+
if (!profile?.isAnnualPlus && selectedPlusMemberMode === "annual" /* ANNUAL */) {
|
|
2711
|
+
return void 0;
|
|
2712
|
+
}
|
|
2713
|
+
return selectedPlusMemberProduct;
|
|
2493
2714
|
}, [
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
removeCartLines2
|
|
2715
|
+
selectedPlusMemberMode,
|
|
2716
|
+
selectedPlusMemberProduct?.variant,
|
|
2717
|
+
selectedPlusMemberProduct?.product,
|
|
2718
|
+
hasMonthlyPlus,
|
|
2719
|
+
hasAnnualPlus
|
|
2500
2720
|
]);
|
|
2721
|
+
return plusMemberProduct;
|
|
2501
2722
|
}
|
|
2502
2723
|
var PlusMemberProvider = ({
|
|
2503
2724
|
variant,
|
|
2504
2725
|
product,
|
|
2505
|
-
|
|
2506
|
-
metafields,
|
|
2726
|
+
memberSetting,
|
|
2507
2727
|
initialSelectedPlusMemberMode = "free",
|
|
2508
2728
|
profile,
|
|
2509
2729
|
locale,
|
|
@@ -2523,14 +2743,14 @@ var PlusMemberProvider = ({
|
|
|
2523
2743
|
const [deleteMarginBottom, setDeleteMarginBottom] = react.useState(false);
|
|
2524
2744
|
const shippingMethodsContext = useShippingMethods({
|
|
2525
2745
|
variant,
|
|
2526
|
-
plusMemberMetafields:
|
|
2746
|
+
plusMemberMetafields: memberSetting,
|
|
2527
2747
|
selectedPlusMemberMode});
|
|
2528
2748
|
const plusMemberHandles = react.useMemo(() => {
|
|
2529
2749
|
return [
|
|
2530
|
-
|
|
2531
|
-
|
|
2750
|
+
memberSetting?.plus_monthly_product?.handle,
|
|
2751
|
+
memberSetting?.plus_annual_product?.handle
|
|
2532
2752
|
].filter(Boolean);
|
|
2533
|
-
}, [
|
|
2753
|
+
}, [memberSetting]);
|
|
2534
2754
|
const { data: plusMemberProducts = [] } = useProductsByHandles({
|
|
2535
2755
|
handles: plusMemberHandles
|
|
2536
2756
|
});
|
|
@@ -2538,25 +2758,24 @@ var PlusMemberProvider = ({
|
|
|
2538
2758
|
if (selectedPlusMemberMode === "free" /* FREE */) {
|
|
2539
2759
|
return null;
|
|
2540
2760
|
}
|
|
2541
|
-
const handle = selectedPlusMemberMode === "monthly" /* MONTHLY */ ?
|
|
2542
|
-
const sku = selectedPlusMemberMode === "monthly" /* MONTHLY */ ?
|
|
2761
|
+
const handle = selectedPlusMemberMode === "monthly" /* MONTHLY */ ? memberSetting?.plus_monthly_product?.handle : memberSetting?.plus_annual_product?.handle;
|
|
2762
|
+
const sku = selectedPlusMemberMode === "monthly" /* MONTHLY */ ? memberSetting?.plus_monthly_product?.sku : memberSetting?.plus_annual_product?.sku;
|
|
2543
2763
|
const product2 = plusMemberProducts?.find((p) => p.handle === handle);
|
|
2544
2764
|
const variant2 = product2?.variants?.find((v) => v.sku === sku);
|
|
2545
2765
|
return product2 && variant2 ? { product: product2, variant: variant2 } : null;
|
|
2546
|
-
}, [plusMemberProducts,
|
|
2766
|
+
}, [plusMemberProducts, memberSetting, selectedPlusMemberMode]);
|
|
2547
2767
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2548
2768
|
PlusMemberContext.Provider,
|
|
2549
2769
|
{
|
|
2550
2770
|
value: {
|
|
2551
2771
|
variant,
|
|
2552
|
-
shopCommon,
|
|
2553
2772
|
zipCode,
|
|
2554
2773
|
setZipCode,
|
|
2555
2774
|
allowNextDayDelivery,
|
|
2556
2775
|
setAllowNextDayDelivery,
|
|
2557
2776
|
allowThirdDayDelivery,
|
|
2558
2777
|
setAllowThirdDayDelivery,
|
|
2559
|
-
plusMemberMetafields:
|
|
2778
|
+
plusMemberMetafields: memberSetting,
|
|
2560
2779
|
selectedPlusMemberMode,
|
|
2561
2780
|
setSelectedPlusMemberMode,
|
|
2562
2781
|
showAreaCheckModal,
|
|
@@ -2782,7 +3001,9 @@ exports.ShippingMethodMode = ShippingMethodMode;
|
|
|
2782
3001
|
exports.SpendMoneyType = SpendMoneyType;
|
|
2783
3002
|
exports.atobID = atobID;
|
|
2784
3003
|
exports.btoaID = btoaID;
|
|
3004
|
+
exports.checkAttributesUpdateNeeded = checkAttributesUpdateNeeded;
|
|
2785
3005
|
exports.clearGeoLocationCache = clearGeoLocationCache;
|
|
3006
|
+
exports.createMockCartFromLines = createMockCartFromLines;
|
|
2786
3007
|
exports.currencyCodeMapping = currencyCodeMapping;
|
|
2787
3008
|
exports.defaultSWRMutationConfiguration = defaultSWRMutationConfiguration;
|
|
2788
3009
|
exports.formatFunctionAutoFreeGift = formatFunctionAutoFreeGift;
|
|
@@ -2792,10 +3013,11 @@ exports.getDiscountEnvAttributeValue = getDiscountEnvAttributeValue;
|
|
|
2792
3013
|
exports.getMatchedMainProductSubTotal = getMatchedMainProductSubTotal;
|
|
2793
3014
|
exports.getQuery = getQuery;
|
|
2794
3015
|
exports.getReferralAttributes = getReferralAttributes;
|
|
2795
|
-
exports.
|
|
3016
|
+
exports.normalizeAddToCartLines = normalizeAddToCartLines;
|
|
2796
3017
|
exports.preCheck = preCheck;
|
|
2797
|
-
exports.
|
|
3018
|
+
exports.safeParse = safeParse;
|
|
2798
3019
|
exports.useAddCartLines = useAddCartLines;
|
|
3020
|
+
exports.useAddPlusMemberProductsToCart = useAddPlusMemberProductsToCart;
|
|
2799
3021
|
exports.useAddToCart = useAddToCart;
|
|
2800
3022
|
exports.useAllBlogs = useAllBlogs;
|
|
2801
3023
|
exports.useAllCollections = useAllCollections;
|
|
@@ -2808,6 +3030,7 @@ exports.useAutoRemovePlusMemberInCart = useAutoRemovePlusMemberInCart;
|
|
|
2808
3030
|
exports.useBlog = useBlog;
|
|
2809
3031
|
exports.useBuyNow = useBuyNow;
|
|
2810
3032
|
exports.useCalcAutoFreeGift = useCalcAutoFreeGift;
|
|
3033
|
+
exports.useCalcGiftsFromLines = useCalcGiftsFromLines;
|
|
2811
3034
|
exports.useCalcOrderDiscount = useCalcOrderDiscount;
|
|
2812
3035
|
exports.useCartAttributes = useCartAttributes;
|
|
2813
3036
|
exports.useCartItemQuantityLimit = useCartItemQuantityLimit;
|
|
@@ -2840,6 +3063,7 @@ exports.useSite = useSite;
|
|
|
2840
3063
|
exports.useUpdateCartAttributes = useUpdateCartAttributes;
|
|
2841
3064
|
exports.useUpdateCartLines = useUpdateCartLines;
|
|
2842
3065
|
exports.useUpdateLineCodeAmountAttributes = useUpdateLineCodeAmountAttributes;
|
|
3066
|
+
exports.useUpdatePlusMemberDeliveryOptions = useUpdatePlusMemberDeliveryOptions;
|
|
2843
3067
|
exports.useUpdateVariantQuery = useUpdateVariantQuery;
|
|
2844
3068
|
exports.useVariant = useVariant;
|
|
2845
3069
|
exports.useVariantMedia = useVariantMedia;
|