@anker-in/shopify-react 0.1.1-beta.5 → 0.1.1-beta.51
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/adapters/index.d.mts +18 -3
- package/dist/adapters/index.d.ts +18 -3
- package/dist/adapters/index.js +15 -0
- package/dist/adapters/index.js.map +1 -1
- package/dist/adapters/index.mjs +15 -1
- package/dist/adapters/index.mjs.map +1 -1
- package/dist/hooks/index.d.mts +1984 -9
- package/dist/hooks/index.d.ts +1984 -9
- package/dist/hooks/index.js +1392 -1245
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +1384 -1237
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/index.d.mts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +1830 -1647
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1817 -1617
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +29 -15
- package/dist/provider/index.d.ts +29 -15
- package/dist/provider/index.js +404 -322
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +404 -322
- package/dist/provider/index.mjs.map +1 -1
- package/dist/{types-CICUnw0v.d.mts → types-CMMWxyUF.d.mts} +12 -58
- package/dist/{types-CICUnw0v.d.ts → types-CMMWxyUF.d.ts} +12 -58
- package/dist/{types-BLMoxbOk.d.mts → types-SKDHauqk.d.mts} +14 -9
- package/dist/{types-BLMoxbOk.d.ts → types-SKDHauqk.d.ts} +14 -9
- package/package.json +3 -3
- package/dist/index-BTb1ZIF-.d.ts +0 -1949
- package/dist/index-eFsqDs13.d.mts +0 -1949
package/dist/hooks/index.mjs
CHANGED
|
@@ -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 { getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, updateCartAttributes, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog,
|
|
3
|
+
import { getProductsByHandles, createCart, updateCartCodes, addCartLines, getLocalStorage, updateCartLines, removeCartLines, updateCartAttributes, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage } 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
|
|
|
@@ -60,6 +61,7 @@ var CUSTOMER_ATTRIBUTE_KEY = "_discounts_function_env";
|
|
|
60
61
|
var CUSTOMER_SCRIPT_GIFT_KEY = "_giveaway_gradient_gifts";
|
|
61
62
|
var CODE_AMOUNT_KEY = "_sku_code_money";
|
|
62
63
|
var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
|
|
64
|
+
var MEMBER_PRICE_ATTRIBUTE_KEY = "_member_price";
|
|
63
65
|
var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
|
|
64
66
|
|
|
65
67
|
// src/hooks/cart/utils/normalize-add-to-cart-lines.ts
|
|
@@ -68,9 +70,10 @@ function normalizeAddToCartLines(lines) {
|
|
|
68
70
|
const variant = line.variant;
|
|
69
71
|
const product = variant.product;
|
|
70
72
|
const quantity = line.quantity || 1;
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
const
|
|
73
|
+
const originalPrice = variant.price?.amount ? Number(variant.price.amount) : 0;
|
|
74
|
+
const finalPrice = variant.finalPrice?.amount === void 0 ? originalPrice : Number(variant.finalPrice?.amount);
|
|
75
|
+
const subtotalAmount = originalPrice * quantity;
|
|
76
|
+
const totalAmount = finalPrice * quantity;
|
|
74
77
|
return {
|
|
75
78
|
id: `temp-line-${index}-${variant.id}`,
|
|
76
79
|
// Temporary ID for pre-cart lines
|
|
@@ -84,7 +87,7 @@ function normalizeAddToCartLines(lines) {
|
|
|
84
87
|
customAttributes: line.attributes || [],
|
|
85
88
|
variant: {
|
|
86
89
|
id: variant.id,
|
|
87
|
-
price,
|
|
90
|
+
price: finalPrice,
|
|
88
91
|
listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
|
|
89
92
|
sku: variant.sku || "",
|
|
90
93
|
name: variant.title || "",
|
|
@@ -115,15 +118,16 @@ function createMockCartFromLines(lines, existingCart) {
|
|
|
115
118
|
const normalizedLines = normalizeAddToCartLines(lines);
|
|
116
119
|
const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
|
|
117
120
|
const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
|
|
121
|
+
const currency = lines[0]?.variant?.price?.currencyCode;
|
|
118
122
|
return {
|
|
119
123
|
id: existingCart?.id || "temp-cart-id",
|
|
120
124
|
customerId: existingCart?.customerId,
|
|
121
125
|
email: existingCart?.email,
|
|
122
126
|
createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
123
|
-
currency: existingCart?.currency || { code:
|
|
127
|
+
currency: existingCart?.currency || { code: currency },
|
|
124
128
|
taxesIncluded: existingCart?.taxesIncluded,
|
|
125
129
|
lineItems: normalizedLines,
|
|
126
|
-
|
|
130
|
+
totalLineItemsDiscount: 0,
|
|
127
131
|
orderDiscounts: 0,
|
|
128
132
|
lineItemsSubtotalPrice: subtotalPrice,
|
|
129
133
|
subtotalPrice,
|
|
@@ -154,16 +158,6 @@ var getQuery = () => {
|
|
|
154
158
|
}
|
|
155
159
|
return theRequest;
|
|
156
160
|
};
|
|
157
|
-
function atobID(id) {
|
|
158
|
-
if (id && typeof id === "string" && id.includes("/")) {
|
|
159
|
-
return id.split("/").pop()?.split("?")?.shift();
|
|
160
|
-
} else {
|
|
161
|
-
return id;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
function btoaID(id, type = "ProductVariant") {
|
|
165
|
-
return `gid://shopify/${type}/${id}`;
|
|
166
|
-
}
|
|
167
161
|
var getMatchedMainProductSubTotal = (cartData, variant_list, main_product) => {
|
|
168
162
|
const isAllStoreVariant = main_product?.all_store_variant ?? false;
|
|
169
163
|
const matchedList = cartData?.lineItems?.filter((line) => {
|
|
@@ -187,14 +181,6 @@ var getDiscountEnvAttributeValue = (attributes = []) => {
|
|
|
187
181
|
const attr = attributes.find((attr2) => attr2.key === CUSTOMER_ATTRIBUTE_KEY);
|
|
188
182
|
return safeParse(attr?.value ?? "") ?? {};
|
|
189
183
|
};
|
|
190
|
-
var checkAttributesUpdateNeeded = (oldAttributes, newAttributes, customAttributesNeedRemove) => {
|
|
191
|
-
return oldAttributes.some((attr) => {
|
|
192
|
-
const newAttr = newAttributes.find((newAttr2) => newAttr2.key === attr.key);
|
|
193
|
-
return newAttr ? newAttr.value !== attr.value : true;
|
|
194
|
-
}) || newAttributes.some((attr) => !oldAttributes.some((oldAttr) => oldAttr.key === attr.key)) || customAttributesNeedRemove.some(
|
|
195
|
-
(removeAttr) => oldAttributes.some((oldAttr) => oldAttr.key === removeAttr.key)
|
|
196
|
-
);
|
|
197
|
-
};
|
|
198
184
|
var containsAll = (source, requiredItems = []) => {
|
|
199
185
|
if (!requiredItems?.length) return true;
|
|
200
186
|
const sourceSet = new Set(source);
|
|
@@ -395,43 +381,29 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
395
381
|
}
|
|
396
382
|
return { activeCampaign: null, subtotal: 0 };
|
|
397
383
|
}, [autoFreeGiftConfig, effectiveCart, tags, dealsType]);
|
|
398
|
-
const {
|
|
384
|
+
const { qualifyingTier, nextTierGoal, actualThreshold, currentCurrency } = useMemo(() => {
|
|
399
385
|
if (!activeCampaign || !activeCampaign.rule_result?.spend_get_reward?.gift_product) {
|
|
400
|
-
return {
|
|
386
|
+
return { qualifyingTier: null, nextTierGoal: null, actualThreshold: 0, currentCurrency: "" };
|
|
401
387
|
}
|
|
402
388
|
const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
|
|
403
|
-
const
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
tier
|
|
410
|
-
itemsToAdd: qualifyingTier.reward_list?.map((reward) => {
|
|
411
|
-
const giftProduct = reward?.variant_list?.[0];
|
|
412
|
-
if (!giftProduct) return null;
|
|
413
|
-
return {
|
|
414
|
-
variant: {
|
|
415
|
-
id: btoaID(giftProduct.variant_id),
|
|
416
|
-
handle: giftProduct.handle,
|
|
417
|
-
sku: giftProduct.sku
|
|
418
|
-
},
|
|
419
|
-
quantity: reward?.get_unit || 1,
|
|
420
|
-
attributes: [
|
|
421
|
-
{
|
|
422
|
-
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
423
|
-
value: JSON.stringify({
|
|
424
|
-
is_gift: true,
|
|
425
|
-
rule_id: activeCampaign.rule_id,
|
|
426
|
-
spend_sum_money: qualifyingTier.spend_sum_money
|
|
427
|
-
})
|
|
428
|
-
}
|
|
429
|
-
]
|
|
430
|
-
};
|
|
431
|
-
}).filter((item) => item !== null)
|
|
389
|
+
const currentCurrency2 = effectiveCart?.currency?.code || "";
|
|
390
|
+
console.log("currentCurrency useCalcAutoFreeGift", effectiveCart, currentCurrency2);
|
|
391
|
+
const getThresholdAmount = (tier) => {
|
|
392
|
+
if (tier.spend_sum_money_multi_markets?.[currentCurrency2]?.value) {
|
|
393
|
+
return Number(tier.spend_sum_money_multi_markets[currentCurrency2].value);
|
|
394
|
+
}
|
|
395
|
+
return Number(tier.spend_sum_money || 0);
|
|
432
396
|
};
|
|
433
|
-
|
|
434
|
-
|
|
397
|
+
const qualifyingTier2 = [...giftTiers].sort((a, b) => getThresholdAmount(b) - getThresholdAmount(a)).find((tier) => subtotal >= getThresholdAmount(tier));
|
|
398
|
+
const nextGoal = giftTiers.find((tier) => subtotal < getThresholdAmount(tier));
|
|
399
|
+
const actualThreshold2 = qualifyingTier2 ? getThresholdAmount(qualifyingTier2) : 0;
|
|
400
|
+
return {
|
|
401
|
+
qualifyingTier: qualifyingTier2,
|
|
402
|
+
nextTierGoal: nextGoal || null,
|
|
403
|
+
actualThreshold: actualThreshold2,
|
|
404
|
+
currentCurrency: currentCurrency2
|
|
405
|
+
};
|
|
406
|
+
}, [activeCampaign, subtotal, effectiveCart]);
|
|
435
407
|
const giftHandles = useMemo(() => {
|
|
436
408
|
const giftVariant = autoFreeGiftConfig.map(
|
|
437
409
|
(item) => item.rule_result?.spend_get_reward?.gift_product?.map(
|
|
@@ -447,24 +419,82 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
447
419
|
}
|
|
448
420
|
return true;
|
|
449
421
|
}, [giftHandles]);
|
|
450
|
-
const { data: giftProductsResult } = useSWR(
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
422
|
+
const { data: giftProductsResult } = useSWR(
|
|
423
|
+
shouldFetch ? giftHandles : null,
|
|
424
|
+
async () => {
|
|
425
|
+
const res = await getProductsByHandles(client, {
|
|
426
|
+
handles: giftHandles,
|
|
427
|
+
locale
|
|
428
|
+
});
|
|
429
|
+
const result = Array.isArray(res) ? res : [];
|
|
430
|
+
giftProductsCache.current = {
|
|
431
|
+
data: result,
|
|
432
|
+
giftHandles: [...giftHandles]
|
|
433
|
+
};
|
|
434
|
+
return result;
|
|
435
|
+
},
|
|
436
|
+
{
|
|
437
|
+
revalidateOnFocus: false
|
|
438
|
+
}
|
|
439
|
+
);
|
|
462
440
|
const finalGiftProductsResult = useMemo(() => {
|
|
463
441
|
if (giftProductsCache.current && !shouldFetch) {
|
|
464
442
|
return giftProductsCache.current.data || void 0;
|
|
465
443
|
}
|
|
466
444
|
return giftProductsResult;
|
|
467
445
|
}, [giftProductsResult, shouldFetch]);
|
|
446
|
+
const qualifyingGift = useMemo(() => {
|
|
447
|
+
if (!qualifyingTier || !activeCampaign) {
|
|
448
|
+
return null;
|
|
449
|
+
}
|
|
450
|
+
const itemsToAdd = qualifyingTier.reward_list?.map((reward) => {
|
|
451
|
+
if (!reward.variant_list || reward.variant_list.length === 0) {
|
|
452
|
+
return null;
|
|
453
|
+
}
|
|
454
|
+
let selectedGiftProduct = null;
|
|
455
|
+
for (const giftVariant of reward.variant_list) {
|
|
456
|
+
const productInfo = finalGiftProductsResult?.find(
|
|
457
|
+
(p) => p.handle === giftVariant.handle
|
|
458
|
+
);
|
|
459
|
+
if (productInfo) {
|
|
460
|
+
const variantInfo = productInfo.variants?.find((v) => v.sku === giftVariant.sku);
|
|
461
|
+
if (variantInfo?.availableForSale) {
|
|
462
|
+
selectedGiftProduct = giftVariant;
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (!selectedGiftProduct) {
|
|
468
|
+
selectedGiftProduct = reward.variant_list[0];
|
|
469
|
+
}
|
|
470
|
+
return {
|
|
471
|
+
variant: {
|
|
472
|
+
id: btoaID(selectedGiftProduct.variant_id),
|
|
473
|
+
handle: selectedGiftProduct.handle,
|
|
474
|
+
sku: selectedGiftProduct.sku
|
|
475
|
+
},
|
|
476
|
+
quantity: reward?.get_unit || 1,
|
|
477
|
+
attributes: [
|
|
478
|
+
{
|
|
479
|
+
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
480
|
+
value: JSON.stringify({
|
|
481
|
+
is_gift: true,
|
|
482
|
+
rule_id: activeCampaign.rule_id,
|
|
483
|
+
spend_sum_money: actualThreshold,
|
|
484
|
+
// 使用实际的门槛金额(多币种支持)
|
|
485
|
+
currency_code: currentCurrency
|
|
486
|
+
// 记录当前币种
|
|
487
|
+
})
|
|
488
|
+
}
|
|
489
|
+
]
|
|
490
|
+
};
|
|
491
|
+
}).filter((item) => item !== null);
|
|
492
|
+
const formattedGift = {
|
|
493
|
+
tier: qualifyingTier,
|
|
494
|
+
itemsToAdd
|
|
495
|
+
};
|
|
496
|
+
return formattedGift;
|
|
497
|
+
}, [qualifyingTier, activeCampaign, finalGiftProductsResult, actualThreshold, currentCurrency]);
|
|
468
498
|
return {
|
|
469
499
|
qualifyingGift,
|
|
470
500
|
nextTierGoal,
|
|
@@ -478,7 +508,8 @@ var useScriptAutoFreeGift = ({
|
|
|
478
508
|
_giveaway,
|
|
479
509
|
cart,
|
|
480
510
|
locale: providedLocale,
|
|
481
|
-
lines
|
|
511
|
+
lines,
|
|
512
|
+
profile
|
|
482
513
|
}) => {
|
|
483
514
|
const { client, locale: contextLocale } = useShopify();
|
|
484
515
|
const locale = providedLocale || contextLocale;
|
|
@@ -502,8 +533,9 @@ var useScriptAutoFreeGift = ({
|
|
|
502
533
|
const utmCampaign = Cookies5.get("utm_campaign") || query?.utm_campaign;
|
|
503
534
|
if (campaign.activityAvailableQuery && !utmCampaign?.includes(campaign.activityAvailableQuery))
|
|
504
535
|
return false;
|
|
536
|
+
if (campaign.requireLogin && !profile?.email) return false;
|
|
505
537
|
return true;
|
|
506
|
-
}, [campaign]);
|
|
538
|
+
}, [campaign, profile]);
|
|
507
539
|
const [upgrade_multiple, upgrade_value] = useMemo(() => {
|
|
508
540
|
let upgrade_multiple2 = 1;
|
|
509
541
|
let upgrade_value2 = 0;
|
|
@@ -511,12 +543,14 @@ var useScriptAutoFreeGift = ({
|
|
|
511
543
|
upgrade_multiple2 = 1.2;
|
|
512
544
|
upgrade_value2 = 40;
|
|
513
545
|
}
|
|
514
|
-
effectiveCart?.lineItems?.forEach(
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
546
|
+
effectiveCart?.lineItems?.forEach(
|
|
547
|
+
({ customAttributes }) => {
|
|
548
|
+
customAttributes?.forEach(({ key, value }) => {
|
|
549
|
+
if (key === "_amount_upgrade_multiple") upgrade_multiple2 = Number(value) || 1;
|
|
550
|
+
if (key === "_amount_upgrade_value") upgrade_value2 = Number(value) || 0;
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
);
|
|
520
554
|
return [upgrade_multiple2, upgrade_value2];
|
|
521
555
|
}, [effectiveCart?.lineItems, points_subscribe]);
|
|
522
556
|
const breakpoints = useMemo(() => {
|
|
@@ -581,18 +615,24 @@ var useScriptAutoFreeGift = ({
|
|
|
581
615
|
const nextLevel = levelIndex > 0 ? sortedLevels[levelIndex - 1] ?? null : null;
|
|
582
616
|
return [currentLevel, nextLevel];
|
|
583
617
|
}, [breakpoints, involvedSubTotal, involvedLines.length]);
|
|
584
|
-
const { data: giftProductsResult } = useSWR(
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
618
|
+
const { data: giftProductsResult } = useSWR(
|
|
619
|
+
shouldFetch ? giftHandles : null,
|
|
620
|
+
async () => {
|
|
621
|
+
const res = await getProductsByHandles(client, {
|
|
622
|
+
handles: giftHandles,
|
|
623
|
+
locale
|
|
624
|
+
});
|
|
625
|
+
const result = Array.isArray(res) ? res : [];
|
|
626
|
+
giftProductsCache.current = {
|
|
627
|
+
data: result,
|
|
628
|
+
giftHandles: [...giftHandles]
|
|
629
|
+
};
|
|
630
|
+
return result;
|
|
631
|
+
},
|
|
632
|
+
{
|
|
633
|
+
revalidateOnFocus: false
|
|
634
|
+
}
|
|
635
|
+
);
|
|
596
636
|
const finalGiftProductsResult = useMemo(() => {
|
|
597
637
|
if (giftProductsCache.current && !shouldFetch) {
|
|
598
638
|
return giftProductsCache.current.data || void 0;
|
|
@@ -625,16 +665,19 @@ var useScriptAutoFreeGift = ({
|
|
|
625
665
|
};
|
|
626
666
|
};
|
|
627
667
|
var CartContext = createContext(null);
|
|
628
|
-
function useCartContext() {
|
|
668
|
+
function useCartContext(options) {
|
|
629
669
|
const context = useContext(CartContext);
|
|
630
|
-
if (!context) {
|
|
670
|
+
if (!context && true) {
|
|
631
671
|
throw new Error("useCartContext must be used within a CartProvider");
|
|
632
672
|
}
|
|
633
673
|
return context;
|
|
634
674
|
}
|
|
635
675
|
|
|
636
676
|
// src/hooks/cart/use-create-cart.ts
|
|
637
|
-
function useCreateCart(
|
|
677
|
+
function useCreateCart({
|
|
678
|
+
updateCookie = false,
|
|
679
|
+
options
|
|
680
|
+
}) {
|
|
638
681
|
const { client, locale, cartCookieAdapter } = useShopify();
|
|
639
682
|
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
640
683
|
const createNewCart = useCallback(
|
|
@@ -642,7 +685,8 @@ function useCreateCart(options) {
|
|
|
642
685
|
let newCart = await createCart(client, {
|
|
643
686
|
...arg,
|
|
644
687
|
metafieldIdentifiers,
|
|
645
|
-
cookieAdapter: cartCookieAdapter
|
|
688
|
+
cookieAdapter: cartCookieAdapter,
|
|
689
|
+
updateCookie
|
|
646
690
|
});
|
|
647
691
|
if (newCart) {
|
|
648
692
|
const unApplicableCodes = newCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
@@ -669,11 +713,23 @@ function useAddCartLines(options) {
|
|
|
669
713
|
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
670
714
|
const addLines = useCallback(
|
|
671
715
|
async (_key, { arg }) => {
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
716
|
+
const { cartId, lines } = arg;
|
|
717
|
+
const id = cartId || cartCookieAdapter?.getCartId(locale);
|
|
718
|
+
let updatedCart;
|
|
719
|
+
if (!id) {
|
|
720
|
+
updatedCart = await createCart(client, {
|
|
721
|
+
lines,
|
|
722
|
+
metafieldIdentifiers,
|
|
723
|
+
cookieAdapter: cartCookieAdapter
|
|
724
|
+
});
|
|
725
|
+
} else {
|
|
726
|
+
updatedCart = await addCartLines(client, {
|
|
727
|
+
cartId: id,
|
|
728
|
+
lines,
|
|
729
|
+
metafieldIdentifiers,
|
|
730
|
+
cookieAdapter: cartCookieAdapter
|
|
731
|
+
});
|
|
732
|
+
}
|
|
677
733
|
if (updatedCart) {
|
|
678
734
|
const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
679
735
|
if (unApplicableCodes.length > 0) {
|
|
@@ -720,7 +776,7 @@ var trackAddToCartGA = ({
|
|
|
720
776
|
const currencyCode = variant.product?.price?.currencyCode;
|
|
721
777
|
const totalPrice = lineItems?.reduce(
|
|
722
778
|
(prev, { variant: variant2 }) => prev.plus(
|
|
723
|
-
variant2?.finalPrice?.amount
|
|
779
|
+
variant2?.finalPrice?.amount === void 0 ? Number(variant2?.price?.amount) || 0 : Number(variant2?.finalPrice?.amount) || 0
|
|
724
780
|
),
|
|
725
781
|
new Decimal2(0)
|
|
726
782
|
).toNumber();
|
|
@@ -753,10 +809,10 @@ var trackBuyNowGA = ({
|
|
|
753
809
|
return;
|
|
754
810
|
}
|
|
755
811
|
const { variant } = lineItems[0];
|
|
756
|
-
const currencyCode = variant.price?.currencyCode;
|
|
812
|
+
const currencyCode = variant.product?.price?.currencyCode || variant.price?.currencyCode;
|
|
757
813
|
const totalPrice = lineItems?.reduce(
|
|
758
814
|
(prev, { variant: variant2 }) => prev.plus(
|
|
759
|
-
variant2?.finalPrice?.amount
|
|
815
|
+
variant2?.finalPrice?.amount === void 0 ? Number(variant2?.price?.amount) || 0 : Number(variant2?.finalPrice?.amount) || 0
|
|
760
816
|
),
|
|
761
817
|
new Decimal2(0)
|
|
762
818
|
).toNumber();
|
|
@@ -830,7 +886,7 @@ function useApplyCartCodes(options) {
|
|
|
830
886
|
if (!discountCodes?.length) {
|
|
831
887
|
throw new Error("Invalid input used for this operation: Miss discountCode");
|
|
832
888
|
}
|
|
833
|
-
const cartId = providedCartId
|
|
889
|
+
const cartId = providedCartId || cart?.id;
|
|
834
890
|
if (!cartId) {
|
|
835
891
|
return void 0;
|
|
836
892
|
}
|
|
@@ -843,12 +899,18 @@ function useApplyCartCodes(options) {
|
|
|
843
899
|
cookieAdapter: cartCookieAdapter,
|
|
844
900
|
metafieldIdentifiers
|
|
845
901
|
});
|
|
902
|
+
const unApplicableCodes = discountCodes.filter(
|
|
903
|
+
(code) => updatedCart?.discountCodes?.find((item) => item.code === code && !item.applicable)
|
|
904
|
+
);
|
|
905
|
+
if (unApplicableCodes.length) {
|
|
906
|
+
throw new Error(`${unApplicableCodes.join(", ")} is not applicable to the cart`);
|
|
907
|
+
}
|
|
846
908
|
if (updatedCart) {
|
|
847
909
|
mutateCart(updatedCart);
|
|
848
910
|
}
|
|
849
911
|
return updatedCart;
|
|
850
912
|
},
|
|
851
|
-
[client, locale, cartCookieAdapter, mutateCart, cart]
|
|
913
|
+
[client, locale, cartCookieAdapter, mutateCart, cart, metafieldIdentifiers]
|
|
852
914
|
);
|
|
853
915
|
return useSWRMutation("apply-codes", applyCodes, options);
|
|
854
916
|
}
|
|
@@ -858,7 +920,7 @@ function useRemoveCartCodes(options) {
|
|
|
858
920
|
const removeCodes = useCallback(
|
|
859
921
|
async (_key, { arg }) => {
|
|
860
922
|
const { cartId: providedCartId, discountCodes } = arg;
|
|
861
|
-
const cartId = providedCartId
|
|
923
|
+
const cartId = providedCartId || cart?.id;
|
|
862
924
|
const codes = cart?.discountCodes?.filter((code) => !!code.applicable) || [];
|
|
863
925
|
const leftCodes = codes.filter((code) => discountCodes?.length ? !discountCodes.includes(code.code) : code.code).map((code) => code.code);
|
|
864
926
|
const updatedCart = await updateCartCodes(client, {
|
|
@@ -872,225 +934,96 @@ function useRemoveCartCodes(options) {
|
|
|
872
934
|
}
|
|
873
935
|
return updatedCart;
|
|
874
936
|
},
|
|
875
|
-
[client, locale, cartCookieAdapter, mutateCart, cart]
|
|
937
|
+
[client, locale, cartCookieAdapter, mutateCart, cart, metafieldIdentifiers]
|
|
876
938
|
);
|
|
877
939
|
return useSWRMutation("remove-codes", removeCodes, options);
|
|
878
940
|
}
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
const
|
|
884
|
-
|
|
885
|
-
const { trigger: removeInvalidCodes } = useRemoveCartCodes();
|
|
886
|
-
const { trigger: addCartLines2 } = useAddCartLines();
|
|
887
|
-
const addToCart = useCallback(
|
|
888
|
-
async (_key, { arg }) => {
|
|
889
|
-
const {
|
|
890
|
-
lineItems,
|
|
891
|
-
cartId: providedCartId,
|
|
892
|
-
discountCodes,
|
|
893
|
-
gtmParams = {},
|
|
894
|
-
buyerIdentity,
|
|
895
|
-
needCreateCart = false,
|
|
896
|
-
onCodesInvalid,
|
|
897
|
-
replaceExistingCodes
|
|
898
|
-
} = arg;
|
|
899
|
-
if (!lineItems || lineItems.length === 0) {
|
|
900
|
-
return;
|
|
901
|
-
}
|
|
902
|
-
const lines = lineItems.map((item) => ({
|
|
903
|
-
merchandiseId: item.variant?.id || "",
|
|
904
|
-
quantity: item.quantity || 1,
|
|
905
|
-
attributes: item.attributes,
|
|
906
|
-
sellingPlanId: item.sellingPlanId
|
|
907
|
-
})).filter((item) => item.merchandiseId && item.quantity);
|
|
908
|
-
if (lines.length === 0) {
|
|
909
|
-
return;
|
|
910
|
-
}
|
|
911
|
-
const cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
|
|
912
|
-
let resultCart = await addCartLines2({
|
|
913
|
-
cartId,
|
|
914
|
-
lines,
|
|
915
|
-
buyerIdentity
|
|
916
|
-
});
|
|
917
|
-
if (!resultCart) {
|
|
918
|
-
return void 0;
|
|
919
|
-
}
|
|
920
|
-
console.log("npm addCartLines resultCart", resultCart);
|
|
921
|
-
if (resultCart.discountCodes && resultCart.discountCodes.length > 0) {
|
|
922
|
-
const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
923
|
-
if (unapplicableCodes.length > 0) {
|
|
924
|
-
if (onCodesInvalid) {
|
|
925
|
-
const handledCart = await onCodesInvalid(resultCart, unapplicableCodes);
|
|
926
|
-
if (handledCart) {
|
|
927
|
-
resultCart = handledCart;
|
|
928
|
-
}
|
|
929
|
-
} else {
|
|
930
|
-
await removeInvalidCodes({
|
|
931
|
-
discountCodes: unapplicableCodes
|
|
932
|
-
});
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
if (discountCodes && discountCodes.length > 0) {
|
|
937
|
-
applyCartCodes({
|
|
938
|
-
replaceExistingCodes,
|
|
939
|
-
discountCodes
|
|
940
|
-
});
|
|
941
|
-
}
|
|
942
|
-
if (withTrack) {
|
|
943
|
-
trackAddToCartGA({
|
|
944
|
-
lineItems,
|
|
945
|
-
gtmParams: { ...gtmParams, brand: config.getBrand() }
|
|
946
|
-
});
|
|
947
|
-
trackAddToCartFBQ({ lineItems });
|
|
948
|
-
}
|
|
949
|
-
return resultCart;
|
|
950
|
-
},
|
|
951
|
-
[client, locale, cartCookieAdapter, userAdapter, cart, withTrack]
|
|
941
|
+
var initSameLinesAttributes = ({
|
|
942
|
+
cart,
|
|
943
|
+
line
|
|
944
|
+
}) => {
|
|
945
|
+
const sameLineInCart = cart?.lineItems.find(
|
|
946
|
+
(lineInCart) => lineInCart.variant.sku === line.variant?.sku && lineInCart.product?.handle === line.variant?.product?.handle
|
|
952
947
|
);
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
function useUpdateCartLines(options) {
|
|
956
|
-
const { client, locale, cartCookieAdapter } = useShopify();
|
|
957
|
-
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
958
|
-
const updateLines = useCallback(
|
|
959
|
-
async (_key, { arg }) => {
|
|
960
|
-
const updatedCart = await updateCartLines(client, {
|
|
961
|
-
...arg,
|
|
962
|
-
metafieldIdentifiers,
|
|
963
|
-
cookieAdapter: cartCookieAdapter
|
|
964
|
-
});
|
|
965
|
-
if (updatedCart) {
|
|
966
|
-
mutateCart(updatedCart);
|
|
967
|
-
}
|
|
968
|
-
return updatedCart;
|
|
969
|
-
},
|
|
970
|
-
[client, locale, cartCookieAdapter, mutateCart]
|
|
948
|
+
const codeAmountAttribute = sameLineInCart?.customAttributes?.find(
|
|
949
|
+
(attr) => attr.key === CODE_AMOUNT_KEY
|
|
971
950
|
);
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
function useRemoveCartLines(options) {
|
|
975
|
-
const { client, locale, cartCookieAdapter } = useShopify();
|
|
976
|
-
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
977
|
-
const removeLines = useCallback(
|
|
978
|
-
async (_key, { arg }) => {
|
|
979
|
-
const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
|
|
980
|
-
let updatedCart = await removeCartLines(client, {
|
|
981
|
-
cartId,
|
|
982
|
-
lineIds,
|
|
983
|
-
metafieldIdentifiers,
|
|
984
|
-
cookieAdapter: cartCookieAdapter
|
|
985
|
-
});
|
|
986
|
-
if (updatedCart && autoRemoveInvalidCodes) {
|
|
987
|
-
const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
988
|
-
if (unApplicableCodes.length > 0) {
|
|
989
|
-
if (onCodesRemoved) {
|
|
990
|
-
const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
|
|
991
|
-
if (handledCart) {
|
|
992
|
-
updatedCart = handledCart;
|
|
993
|
-
}
|
|
994
|
-
} else {
|
|
995
|
-
updatedCart = await updateCartCodes(client, {
|
|
996
|
-
cartId: updatedCart.id,
|
|
997
|
-
discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
|
|
998
|
-
metafieldIdentifiers,
|
|
999
|
-
cookieAdapter: cartCookieAdapter
|
|
1000
|
-
}) || updatedCart;
|
|
1001
|
-
}
|
|
1002
|
-
}
|
|
1003
|
-
}
|
|
1004
|
-
if (updatedCart) {
|
|
1005
|
-
mutateCart(updatedCart);
|
|
1006
|
-
}
|
|
1007
|
-
return updatedCart;
|
|
1008
|
-
},
|
|
1009
|
-
[client, locale, cartCookieAdapter, mutateCart]
|
|
951
|
+
const scriptCodeAmountAttribute = sameLineInCart?.customAttributes?.find(
|
|
952
|
+
(attr) => attr.key === SCRIPT_CODE_AMOUNT_KEY
|
|
1010
953
|
);
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
954
|
+
let functionAttribute = null;
|
|
955
|
+
try {
|
|
956
|
+
functionAttribute = sameLineInCart?.customAttributes?.find(
|
|
957
|
+
(attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY && JSON.parse(attr.value)?.discounted_amount
|
|
958
|
+
);
|
|
959
|
+
} catch (error) {
|
|
960
|
+
}
|
|
961
|
+
if (codeAmountAttribute || functionAttribute || scriptCodeAmountAttribute) {
|
|
962
|
+
return {
|
|
963
|
+
...line,
|
|
964
|
+
attributes: [
|
|
965
|
+
...line.attributes || [],
|
|
966
|
+
codeAmountAttribute,
|
|
967
|
+
functionAttribute,
|
|
968
|
+
scriptCodeAmountAttribute
|
|
969
|
+
].filter(Boolean)
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
return line;
|
|
973
|
+
};
|
|
974
|
+
var initDiscountAttributes = ({ line }) => {
|
|
975
|
+
let itemAttributes = line.attributes || [];
|
|
976
|
+
const functionEnvAttribute = itemAttributes.find((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY);
|
|
977
|
+
if (!functionEnvAttribute) {
|
|
978
|
+
itemAttributes = itemAttributes.concat([
|
|
979
|
+
{
|
|
980
|
+
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
981
|
+
value: JSON.stringify({
|
|
982
|
+
is_gift: false,
|
|
983
|
+
discounted_amount: line.variant?.finalPrice?.amount === void 0 ? Number(line.variant?.price?.amount) * (line.quantity || 1) : Number(line.variant?.finalPrice?.amount) * (line.quantity || 1)
|
|
984
|
+
})
|
|
1025
985
|
}
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
986
|
+
]);
|
|
987
|
+
}
|
|
988
|
+
const memberPriceAttribute = itemAttributes.find(
|
|
989
|
+
(attr) => attr.key === MEMBER_PRICE_ATTRIBUTE_KEY
|
|
1029
990
|
);
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
async (_key, { arg }) => {
|
|
1037
|
-
const {
|
|
1038
|
-
lineItems,
|
|
1039
|
-
discountCodes,
|
|
1040
|
-
gtmParams = {},
|
|
1041
|
-
buyerIdentity,
|
|
1042
|
-
fbqTrackConfig,
|
|
1043
|
-
customAttributes,
|
|
1044
|
-
metafieldIdentifiers,
|
|
1045
|
-
redirectToCheckout
|
|
1046
|
-
} = arg;
|
|
1047
|
-
if (!lineItems || lineItems.length === 0) {
|
|
1048
|
-
return;
|
|
991
|
+
const coupon = line.coupon;
|
|
992
|
+
if (!memberPriceAttribute && coupon) {
|
|
993
|
+
itemAttributes = itemAttributes.concat([
|
|
994
|
+
{
|
|
995
|
+
key: MEMBER_PRICE_ATTRIBUTE_KEY,
|
|
996
|
+
value: JSON.stringify({ code: coupon.code })
|
|
1049
997
|
}
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
998
|
+
]);
|
|
999
|
+
}
|
|
1000
|
+
const couponDiscountAttribute = itemAttributes.find(
|
|
1001
|
+
(attr) => attr.key === CODE_AMOUNT_KEY || attr.key === SCRIPT_CODE_AMOUNT_KEY
|
|
1002
|
+
);
|
|
1003
|
+
if (!couponDiscountAttribute && coupon && Number(coupon?.amount) > 0) {
|
|
1004
|
+
itemAttributes = itemAttributes.concat([
|
|
1005
|
+
{
|
|
1006
|
+
key: CODE_AMOUNT_KEY,
|
|
1007
|
+
value: new Decimal2(coupon.amount).times(line.quantity || 1).toString()
|
|
1008
|
+
},
|
|
1009
|
+
{
|
|
1010
|
+
key: SCRIPT_CODE_AMOUNT_KEY,
|
|
1011
|
+
value: new Decimal2(coupon.amount).times(line.quantity || 1).toString()
|
|
1058
1012
|
}
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
gtmParams: { ...gtmParams, brand: config.getBrand() }
|
|
1074
|
-
});
|
|
1075
|
-
if (fbqTrackConfig) {
|
|
1076
|
-
trackBuyNowFBQ({ trackConfig: fbqTrackConfig });
|
|
1077
|
-
}
|
|
1078
|
-
}
|
|
1079
|
-
if (redirectToCheckout) {
|
|
1080
|
-
if (resultCart.url) {
|
|
1081
|
-
if (typeof window !== "undefined") {
|
|
1082
|
-
window.location.href = resultCart.url;
|
|
1083
|
-
}
|
|
1084
|
-
} else {
|
|
1085
|
-
throw new Error("Failed to get checkout URL");
|
|
1086
|
-
}
|
|
1087
|
-
}
|
|
1088
|
-
return resultCart;
|
|
1089
|
-
},
|
|
1090
|
-
[client, locale, isLoggedIn, cartCookieAdapter, withTrack]
|
|
1091
|
-
);
|
|
1092
|
-
return useSWRMutation("buy-now", buyNow, swrOptions);
|
|
1093
|
-
}
|
|
1013
|
+
]);
|
|
1014
|
+
}
|
|
1015
|
+
return { ...line, attributes: itemAttributes };
|
|
1016
|
+
};
|
|
1017
|
+
var getLinesWithAttributes = ({
|
|
1018
|
+
cart,
|
|
1019
|
+
lineItems
|
|
1020
|
+
}) => {
|
|
1021
|
+
return lineItems.map((line) => {
|
|
1022
|
+
const sameLine = initSameLinesAttributes({ cart, line });
|
|
1023
|
+
const functionLine = initDiscountAttributes({ line: sameLine });
|
|
1024
|
+
return functionLine;
|
|
1025
|
+
});
|
|
1026
|
+
};
|
|
1094
1027
|
function useCalcGiftsFromLines({
|
|
1095
1028
|
lines,
|
|
1096
1029
|
customer,
|
|
@@ -1107,17 +1040,41 @@ function useCalcGiftsFromLines({
|
|
|
1107
1040
|
lines
|
|
1108
1041
|
});
|
|
1109
1042
|
const allGiftLines = useMemo(() => {
|
|
1110
|
-
const functionGiftLines = functionGift.qualifyingGift?.itemsToAdd || []
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1043
|
+
const functionGiftLines = (functionGift.qualifyingGift?.itemsToAdd || []).map((item) => {
|
|
1044
|
+
const product = functionGift.giftProductsResult?.find(
|
|
1045
|
+
(product2) => product2.handle === item.variant.handle
|
|
1046
|
+
);
|
|
1047
|
+
const variants = product?.variants;
|
|
1048
|
+
const variant = Array.isArray(variants) ? variants.find((v) => v.sku === item.variant.sku) : void 0;
|
|
1049
|
+
if (!variant) {
|
|
1050
|
+
console.warn(
|
|
1051
|
+
`Function gift: Variant not found for handle=${item.variant.handle}, sku=${item.variant.sku}`
|
|
1052
|
+
);
|
|
1053
|
+
return null;
|
|
1054
|
+
}
|
|
1055
|
+
return {
|
|
1056
|
+
variant: {
|
|
1057
|
+
...variant,
|
|
1058
|
+
product
|
|
1059
|
+
},
|
|
1060
|
+
quantity: item.quantity ?? 1,
|
|
1061
|
+
attributes: item.attributes
|
|
1062
|
+
};
|
|
1063
|
+
}).filter((item) => item !== null);
|
|
1064
|
+
const scriptGiftLines = scriptGift.freeGiftLevel ? scriptGift.freeGiftLevel.giveawayProducts.map((item) => {
|
|
1065
|
+
const product = scriptGift.giftProductsResult?.find(
|
|
1066
|
+
(product2) => product2.handle === item.handle
|
|
1114
1067
|
);
|
|
1115
|
-
const
|
|
1068
|
+
const variants = product?.variants;
|
|
1069
|
+
const variant = Array.isArray(variants) ? variants.find((v) => v.sku === item.sku) : void 0;
|
|
1070
|
+
if (!variant) {
|
|
1071
|
+
console.warn(`Script gift: Variant not found for handle=${item.handle}, sku=${item.sku}`);
|
|
1072
|
+
return null;
|
|
1073
|
+
}
|
|
1116
1074
|
return {
|
|
1117
1075
|
variant: {
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
sku: product.sku
|
|
1076
|
+
...variant,
|
|
1077
|
+
product
|
|
1121
1078
|
},
|
|
1122
1079
|
quantity: 1,
|
|
1123
1080
|
attributes: [
|
|
@@ -1127,10 +1084,11 @@ function useCalcGiftsFromLines({
|
|
|
1127
1084
|
}
|
|
1128
1085
|
]
|
|
1129
1086
|
};
|
|
1130
|
-
}).filter((item) => item
|
|
1087
|
+
}).filter((item) => item !== null) : [];
|
|
1131
1088
|
return [...functionGiftLines, ...scriptGiftLines];
|
|
1132
1089
|
}, [
|
|
1133
1090
|
functionGift.qualifyingGift,
|
|
1091
|
+
functionGift.giftProductsResult,
|
|
1134
1092
|
scriptGift.freeGiftLevel,
|
|
1135
1093
|
scriptGift.giftProductsResult,
|
|
1136
1094
|
scriptGiveawayKey
|
|
@@ -1165,7 +1123,7 @@ var useCalcOrderDiscount = (cart, orderDiscountConfig, customer) => {
|
|
|
1165
1123
|
const isCustomerLoading = useMemo(() => !customer ? true : false, [customer]);
|
|
1166
1124
|
const dealsType = "";
|
|
1167
1125
|
const { activeCampaign, subtotal } = useMemo(() => {
|
|
1168
|
-
for (const campaign of orderDiscountConfig) {
|
|
1126
|
+
for (const campaign of orderDiscountConfig || []) {
|
|
1169
1127
|
const { rule_conditions = [], result_detail } = campaign;
|
|
1170
1128
|
const { main_product, order_discount_conf } = result_detail || {};
|
|
1171
1129
|
const isPreCheckPassed = preCheck(rule_conditions, tags, []);
|
|
@@ -1195,9 +1153,12 @@ var useCalcOrderDiscount = (cart, orderDiscountConfig, customer) => {
|
|
|
1195
1153
|
discountAmount: 0
|
|
1196
1154
|
};
|
|
1197
1155
|
}
|
|
1198
|
-
const
|
|
1199
|
-
|
|
1200
|
-
const
|
|
1156
|
+
const currentCurrency = cart?.currency?.code || "";
|
|
1157
|
+
console.log("currentCurrency", cart, currentCurrency);
|
|
1158
|
+
const orderDiscountConf = activeCampaign.result_detail.order_discount_conf;
|
|
1159
|
+
const tieredDiscounts = orderDiscountConf.tiered_discounts_markets?.[currentCurrency] || orderDiscountConf.tiered_discounts;
|
|
1160
|
+
const qualifyingTier = [...tieredDiscounts].sort((a, b) => Number(b.amount) - Number(a.amount)).find((tier) => subtotal >= Number(tier.amount));
|
|
1161
|
+
const nextGoal = [...tieredDiscounts].sort((a, b) => Number(a.amount) - Number(b.amount)).find((tier) => subtotal < Number(tier.amount));
|
|
1201
1162
|
if (!qualifyingTier) {
|
|
1202
1163
|
return {
|
|
1203
1164
|
qualifyingDiscount: null,
|
|
@@ -1234,43 +1195,10 @@ var useCalcOrderDiscount = (cart, orderDiscountConfig, customer) => {
|
|
|
1234
1195
|
isLoading: isCustomerLoading
|
|
1235
1196
|
};
|
|
1236
1197
|
};
|
|
1237
|
-
function useHasPlusMemberInCart({
|
|
1238
|
-
memberSetting,
|
|
1239
|
-
cart
|
|
1240
|
-
}) {
|
|
1241
|
-
const { plus_monthly_product, plus_annual_product } = memberSetting || {};
|
|
1242
|
-
return useMemo(() => {
|
|
1243
|
-
if (!cart?.lineItems) {
|
|
1244
|
-
return {
|
|
1245
|
-
hasPlusMember: false,
|
|
1246
|
-
hasMonthlyPlus: false,
|
|
1247
|
-
hasAnnualPlus: false
|
|
1248
|
-
};
|
|
1249
|
-
}
|
|
1250
|
-
const monthlyPlusItem = cart.lineItems.find(
|
|
1251
|
-
(item) => item.product?.handle === plus_monthly_product?.handle && item.variant?.sku === plus_monthly_product?.sku
|
|
1252
|
-
);
|
|
1253
|
-
const annualPlusItem = cart.lineItems.find(
|
|
1254
|
-
(item) => item.product?.handle === plus_annual_product?.handle && item.variant?.sku === plus_annual_product?.sku
|
|
1255
|
-
);
|
|
1256
|
-
const hasMonthlyPlus = !!monthlyPlusItem;
|
|
1257
|
-
const hasAnnualPlus = !!annualPlusItem;
|
|
1258
|
-
const hasPlusMember = hasMonthlyPlus || hasAnnualPlus;
|
|
1259
|
-
return {
|
|
1260
|
-
hasPlusMember,
|
|
1261
|
-
hasMonthlyPlus,
|
|
1262
|
-
hasAnnualPlus,
|
|
1263
|
-
monthlyPlusItem,
|
|
1264
|
-
annualPlusItem
|
|
1265
|
-
};
|
|
1266
|
-
}, [cart?.lineItems, plus_monthly_product, plus_annual_product]);
|
|
1267
|
-
}
|
|
1268
|
-
|
|
1269
|
-
// src/hooks/cart/feature/use-cart-attributes.ts
|
|
1270
1198
|
var getReferralAttributes = () => {
|
|
1271
|
-
const inviteCode = Cookies5.get("
|
|
1272
|
-
const playModeId = Cookies5.get("playModeId");
|
|
1273
|
-
const popup = Cookies5.get("_popup");
|
|
1199
|
+
const inviteCode = getLocalStorage("inviteCode") || Cookies5.get("inviteCode");
|
|
1200
|
+
const playModeId = getLocalStorage("playModeId") || Cookies5.get("playModeId");
|
|
1201
|
+
const popup = getLocalStorage("_popup") || Cookies5.get("_popup");
|
|
1274
1202
|
if (inviteCode && playModeId) {
|
|
1275
1203
|
return popup ? [
|
|
1276
1204
|
{ key: "_invite_code", value: inviteCode ? inviteCode : "" },
|
|
@@ -1283,117 +1211,130 @@ var getReferralAttributes = () => {
|
|
|
1283
1211
|
}
|
|
1284
1212
|
return [];
|
|
1285
1213
|
};
|
|
1214
|
+
var getUserType = (customer) => {
|
|
1215
|
+
let userInfo = Cookies5.get("userInfo");
|
|
1216
|
+
if (userInfo) {
|
|
1217
|
+
userInfo = JSON.parse(userInfo);
|
|
1218
|
+
let arr = typeof userInfo?.id == "string" && userInfo?.id.split("/");
|
|
1219
|
+
userInfo.setId = arr[arr.length - 1];
|
|
1220
|
+
}
|
|
1221
|
+
const customerInfo = userInfo || customer;
|
|
1222
|
+
if (!customerInfo) {
|
|
1223
|
+
return "new_user_unlogin";
|
|
1224
|
+
}
|
|
1225
|
+
if (customer) {
|
|
1226
|
+
const { orders = {} } = customer;
|
|
1227
|
+
if (orders?.edges?.length === 1) {
|
|
1228
|
+
return "old_user_orders_once";
|
|
1229
|
+
} else if (orders?.edges?.length > 1) {
|
|
1230
|
+
return "old_user_orders_twice";
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
return "new_user_login";
|
|
1234
|
+
};
|
|
1235
|
+
function getCartAttributes({
|
|
1236
|
+
profile,
|
|
1237
|
+
customer,
|
|
1238
|
+
cart,
|
|
1239
|
+
memberType,
|
|
1240
|
+
currentUrl = ""
|
|
1241
|
+
}) {
|
|
1242
|
+
const userType = getUserType(customer);
|
|
1243
|
+
const memberAttributes = [
|
|
1244
|
+
{
|
|
1245
|
+
key: "_token",
|
|
1246
|
+
value: profile?.token
|
|
1247
|
+
},
|
|
1248
|
+
{
|
|
1249
|
+
key: "_member_type",
|
|
1250
|
+
value: memberType ?? String(profile?.memberType)
|
|
1251
|
+
},
|
|
1252
|
+
{
|
|
1253
|
+
key: "_user_type",
|
|
1254
|
+
value: userType
|
|
1255
|
+
},
|
|
1256
|
+
{
|
|
1257
|
+
key: "_is_login",
|
|
1258
|
+
value: profile?.token ? "true" : "false"
|
|
1259
|
+
}
|
|
1260
|
+
];
|
|
1261
|
+
if (profile?.token) {
|
|
1262
|
+
memberAttributes.push({
|
|
1263
|
+
key: "_login_user",
|
|
1264
|
+
value: "1"
|
|
1265
|
+
});
|
|
1266
|
+
}
|
|
1267
|
+
const discountCodes = cart?.discountCodes.map((item) => item.code).filter((code) => code) || [];
|
|
1268
|
+
const functionAttributes = [
|
|
1269
|
+
{
|
|
1270
|
+
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
1271
|
+
value: JSON.stringify({
|
|
1272
|
+
discount_code: discountCodes,
|
|
1273
|
+
user_tags: customer?.tags || []
|
|
1274
|
+
})
|
|
1275
|
+
}
|
|
1276
|
+
];
|
|
1277
|
+
const presellAttributes = [
|
|
1278
|
+
{
|
|
1279
|
+
key: "_presale",
|
|
1280
|
+
value: cart?.lineItems.some((item) => item?.variant?.metafields?.presell === "presell")
|
|
1281
|
+
}
|
|
1282
|
+
];
|
|
1283
|
+
const weightAttributes = [
|
|
1284
|
+
{
|
|
1285
|
+
key: "_weight",
|
|
1286
|
+
value: cart?.lineItems.reduce((acc, item) => {
|
|
1287
|
+
return new Decimal2(acc).plus(item.variant.weight ?? 0).toNumber();
|
|
1288
|
+
}, 0).toString()
|
|
1289
|
+
},
|
|
1290
|
+
{
|
|
1291
|
+
key: "_app_source_name",
|
|
1292
|
+
value: "dtc"
|
|
1293
|
+
}
|
|
1294
|
+
];
|
|
1295
|
+
const trackingAttributes = [
|
|
1296
|
+
{
|
|
1297
|
+
key: "utm_params",
|
|
1298
|
+
value: currentUrl
|
|
1299
|
+
}
|
|
1300
|
+
];
|
|
1301
|
+
const commonAttributes = [
|
|
1302
|
+
...memberAttributes,
|
|
1303
|
+
...functionAttributes,
|
|
1304
|
+
...presellAttributes,
|
|
1305
|
+
...weightAttributes,
|
|
1306
|
+
...trackingAttributes,
|
|
1307
|
+
...getReferralAttributes()
|
|
1308
|
+
].filter((item) => item?.value);
|
|
1309
|
+
const extraAttributesInCart = cart?.customAttributes?.filter(
|
|
1310
|
+
(item) => !commonAttributes.some((attr) => attr.key === item.key)
|
|
1311
|
+
) || [];
|
|
1312
|
+
return [...commonAttributes, ...extraAttributesInCart].filter((item) => item?.value);
|
|
1313
|
+
}
|
|
1286
1314
|
var useCartAttributes = ({
|
|
1287
1315
|
profile,
|
|
1288
1316
|
customer,
|
|
1289
1317
|
cart,
|
|
1290
|
-
|
|
1318
|
+
memberType
|
|
1291
1319
|
}) => {
|
|
1292
1320
|
const [currentUrl, setCurrentUrl] = useState("");
|
|
1293
|
-
const { hasPlusMember } = useHasPlusMemberInCart({
|
|
1294
|
-
memberSetting,
|
|
1295
|
-
cart
|
|
1296
|
-
});
|
|
1297
1321
|
useEffect(() => {
|
|
1298
1322
|
setCurrentUrl(window.location.href);
|
|
1299
1323
|
}, []);
|
|
1300
|
-
const
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
return "new_user_unlogin";
|
|
1310
|
-
}
|
|
1311
|
-
if (customer) {
|
|
1312
|
-
const { orders = {} } = customer;
|
|
1313
|
-
if (orders?.edges?.length === 1) {
|
|
1314
|
-
return "old_user_orders_once";
|
|
1315
|
-
} else if (orders?.edges?.length > 1) {
|
|
1316
|
-
return "old_user_orders_twice";
|
|
1317
|
-
}
|
|
1318
|
-
}
|
|
1319
|
-
return "new_user_login";
|
|
1320
|
-
}, [customer]);
|
|
1321
|
-
const memberAttributes = useMemo(() => {
|
|
1322
|
-
return [
|
|
1323
|
-
{
|
|
1324
|
-
key: "_token",
|
|
1325
|
-
value: profile?.token
|
|
1326
|
-
//是否登录
|
|
1327
|
-
},
|
|
1328
|
-
{
|
|
1329
|
-
key: "_member_type",
|
|
1330
|
-
value: hasPlusMember ? "2" : profile?.memberType
|
|
1331
|
-
//:0(游客),1(普通会员),2(付费会员)
|
|
1332
|
-
},
|
|
1333
|
-
{
|
|
1334
|
-
key: "_user_type",
|
|
1335
|
-
value: userType
|
|
1336
|
-
// n
|
|
1337
|
-
},
|
|
1338
|
-
{
|
|
1339
|
-
key: "_is_login",
|
|
1340
|
-
value: profile?.token ? "true" : "false"
|
|
1341
|
-
}
|
|
1342
|
-
];
|
|
1343
|
-
}, [profile?.memberType, profile?.token, userType, hasPlusMember]);
|
|
1344
|
-
const functionAttributes = useMemo(() => {
|
|
1345
|
-
return [
|
|
1346
|
-
cart?.discountCodes && {
|
|
1347
|
-
key: "_discounts_function_env",
|
|
1348
|
-
value: JSON.stringify({
|
|
1349
|
-
discount_code: cart?.discountCodes.map((item) => item.code),
|
|
1350
|
-
user_tags: customer?.tags || []
|
|
1351
|
-
})
|
|
1352
|
-
}
|
|
1353
|
-
];
|
|
1354
|
-
}, [cart]);
|
|
1355
|
-
const presellAttributes = useMemo(() => {
|
|
1356
|
-
return [
|
|
1357
|
-
{
|
|
1358
|
-
key: "_presale",
|
|
1359
|
-
value: cart?.lineItems.some((item) => item?.variant?.metafields?.presell === "presell")
|
|
1360
|
-
}
|
|
1361
|
-
];
|
|
1362
|
-
}, [cart]);
|
|
1363
|
-
const weightAttributes = useMemo(() => {
|
|
1364
|
-
return [
|
|
1365
|
-
{
|
|
1366
|
-
key: "_weight",
|
|
1367
|
-
value: cart?.lineItems.reduce((acc, item) => {
|
|
1368
|
-
return new Decimal2(acc).plus(item.variant.weight ?? 0).toNumber();
|
|
1369
|
-
}, 0).toString()
|
|
1370
|
-
},
|
|
1371
|
-
{
|
|
1372
|
-
key: "_app_source_name",
|
|
1373
|
-
value: "dtc"
|
|
1374
|
-
}
|
|
1375
|
-
];
|
|
1376
|
-
}, [cart]);
|
|
1377
|
-
const trackingAttributes = useMemo(() => {
|
|
1378
|
-
return [
|
|
1379
|
-
{
|
|
1380
|
-
key: "utm_params",
|
|
1381
|
-
value: currentUrl
|
|
1382
|
-
}
|
|
1383
|
-
];
|
|
1384
|
-
}, [currentUrl]);
|
|
1324
|
+
const attributes = useMemo(() => {
|
|
1325
|
+
return getCartAttributes({
|
|
1326
|
+
profile,
|
|
1327
|
+
customer,
|
|
1328
|
+
cart,
|
|
1329
|
+
memberType,
|
|
1330
|
+
currentUrl
|
|
1331
|
+
});
|
|
1332
|
+
}, [profile, customer, cart, memberType, currentUrl]);
|
|
1385
1333
|
return useMemo(
|
|
1386
1334
|
() => ({
|
|
1387
|
-
attributes
|
|
1388
|
-
...memberAttributes,
|
|
1389
|
-
...functionAttributes,
|
|
1390
|
-
...presellAttributes,
|
|
1391
|
-
...weightAttributes,
|
|
1392
|
-
...trackingAttributes,
|
|
1393
|
-
...getReferralAttributes()
|
|
1394
|
-
].filter((item) => item?.value)
|
|
1335
|
+
attributes
|
|
1395
1336
|
}),
|
|
1396
|
-
[
|
|
1337
|
+
[attributes]
|
|
1397
1338
|
);
|
|
1398
1339
|
};
|
|
1399
1340
|
var DEFAULT_MIN = 1;
|
|
@@ -1456,7 +1397,7 @@ var useUpdateLineCodeAmountAttributes = ({
|
|
|
1456
1397
|
);
|
|
1457
1398
|
const functionEnvValue = getDiscountEnvAttributeValue(line.customAttributes);
|
|
1458
1399
|
const hasSameFunctionEnvAttribute = Number(functionEnvValue.discounted_amount) === Number(line.totalAmount);
|
|
1459
|
-
if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute) {
|
|
1400
|
+
if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute && !functionEnvValue.is_gift) {
|
|
1460
1401
|
attrNeedUpdate.push({
|
|
1461
1402
|
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
1462
1403
|
value: JSON.stringify({
|
|
@@ -1495,29 +1436,22 @@ var useUpdateLineCodeAmountAttributes = ({
|
|
|
1495
1436
|
}).filter(
|
|
1496
1437
|
({ attrNeedUpdate, attrNeedDelete }) => attrNeedUpdate.length || attrNeedDelete.length
|
|
1497
1438
|
).map(({ line, attrNeedUpdate, attrNeedDelete }) => {
|
|
1439
|
+
let lineId = line.id;
|
|
1440
|
+
let attributes = line.customAttributes || [];
|
|
1441
|
+
if (attrNeedDelete.length) {
|
|
1442
|
+
attributes = attributes.filter(
|
|
1443
|
+
(attr) => !attrNeedDelete.includes(attr.key)
|
|
1444
|
+
);
|
|
1445
|
+
}
|
|
1498
1446
|
if (attrNeedUpdate.length) {
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
...line.customAttributes?.filter(
|
|
1503
|
-
(attr) => !attrNeedUpdate.some((updateAttr) => updateAttr.key === attr.key)
|
|
1504
|
-
) || [],
|
|
1505
|
-
...attrNeedUpdate
|
|
1506
|
-
]
|
|
1507
|
-
};
|
|
1508
|
-
} else if (attrNeedDelete.length) {
|
|
1509
|
-
return {
|
|
1510
|
-
id: line.id,
|
|
1511
|
-
attributes: line.customAttributes?.filter(
|
|
1512
|
-
(attr) => !attrNeedDelete.includes(attr.key)
|
|
1513
|
-
) || []
|
|
1514
|
-
};
|
|
1515
|
-
} else {
|
|
1516
|
-
return {
|
|
1517
|
-
id: line.id,
|
|
1518
|
-
attributes: line.customAttributes || []
|
|
1519
|
-
};
|
|
1447
|
+
attributes = attributes.filter(
|
|
1448
|
+
(attr) => !attrNeedUpdate.some((updateAttr) => updateAttr.key === attr.key)
|
|
1449
|
+
).concat(attrNeedUpdate);
|
|
1520
1450
|
}
|
|
1451
|
+
return {
|
|
1452
|
+
id: lineId,
|
|
1453
|
+
attributes
|
|
1454
|
+
};
|
|
1521
1455
|
}),
|
|
1522
1456
|
[cart?.lineItems, mainProductDiscountCodes]
|
|
1523
1457
|
);
|
|
@@ -1552,45 +1486,61 @@ var useUpdateLineCodeAmountAttributes = ({
|
|
|
1552
1486
|
}, [loading, setLoadingState]);
|
|
1553
1487
|
};
|
|
1554
1488
|
|
|
1555
|
-
// src/hooks/
|
|
1556
|
-
var
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
}
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1489
|
+
// src/hooks/member/plus/types.ts
|
|
1490
|
+
var PLUS_MEMBER_TYPE = /* @__PURE__ */ ((PLUS_MEMBER_TYPE2) => {
|
|
1491
|
+
PLUS_MEMBER_TYPE2[PLUS_MEMBER_TYPE2["FREE"] = 0] = "FREE";
|
|
1492
|
+
PLUS_MEMBER_TYPE2[PLUS_MEMBER_TYPE2["MONTHLY"] = 1] = "MONTHLY";
|
|
1493
|
+
PLUS_MEMBER_TYPE2[PLUS_MEMBER_TYPE2["ANNUAL"] = 2] = "ANNUAL";
|
|
1494
|
+
return PLUS_MEMBER_TYPE2;
|
|
1495
|
+
})(PLUS_MEMBER_TYPE || {});
|
|
1496
|
+
var PlusMemberMode = /* @__PURE__ */ ((PlusMemberMode2) => {
|
|
1497
|
+
PlusMemberMode2["MONTHLY"] = "monthly";
|
|
1498
|
+
PlusMemberMode2["ANNUAL"] = "annual";
|
|
1499
|
+
return PlusMemberMode2;
|
|
1500
|
+
})(PlusMemberMode || {});
|
|
1501
|
+
var DeliveryPlusType = /* @__PURE__ */ ((DeliveryPlusType2) => {
|
|
1502
|
+
DeliveryPlusType2["FREE"] = "free";
|
|
1503
|
+
DeliveryPlusType2["MONTHLY"] = "monthly";
|
|
1504
|
+
DeliveryPlusType2["ANNUAL"] = "annual";
|
|
1505
|
+
return DeliveryPlusType2;
|
|
1506
|
+
})(DeliveryPlusType || {});
|
|
1507
|
+
var ShippingMethodMode = /* @__PURE__ */ ((ShippingMethodMode2) => {
|
|
1508
|
+
ShippingMethodMode2["FREE"] = "free";
|
|
1509
|
+
ShippingMethodMode2["TDD"] = "tdd";
|
|
1510
|
+
ShippingMethodMode2["NDD"] = "ndd";
|
|
1511
|
+
return ShippingMethodMode2;
|
|
1512
|
+
})(ShippingMethodMode || {});
|
|
1513
|
+
var createInitialValue = () => ({
|
|
1514
|
+
plusMemberMetafields: {},
|
|
1515
|
+
selectedPlusMemberMode: "free",
|
|
1516
|
+
setSelectedPlusMemberMode: () => {
|
|
1517
|
+
},
|
|
1518
|
+
selectedShippingMethod: void 0,
|
|
1519
|
+
setSelectedShippingMethod: () => {
|
|
1520
|
+
},
|
|
1521
|
+
showMoreShippingMethod: false,
|
|
1522
|
+
setShowMoreShippingMethod: () => {
|
|
1523
|
+
},
|
|
1524
|
+
variant: {},
|
|
1525
|
+
product: {},
|
|
1526
|
+
shippingMethodsContext: {
|
|
1527
|
+
freeShippingMethods: [],
|
|
1528
|
+
paymentShippingMethods: [],
|
|
1529
|
+
nddOverweight: false,
|
|
1530
|
+
tddOverweight: false,
|
|
1531
|
+
nddCoupon: void 0,
|
|
1532
|
+
tddCoupon: void 0,
|
|
1533
|
+
isLoadingCoupon: false
|
|
1534
|
+
},
|
|
1535
|
+
selectedPlusMemberVariant: void 0,
|
|
1536
|
+
showPlusMemberBenefit: false,
|
|
1537
|
+
setShowPlusMemberBenefit: () => {
|
|
1538
|
+
},
|
|
1539
|
+
profile: void 0
|
|
1540
|
+
});
|
|
1541
|
+
var PlusMemberContext = createContext(createInitialValue());
|
|
1542
|
+
function usePlusMemberContext() {
|
|
1543
|
+
return useContext(PlusMemberContext);
|
|
1594
1544
|
}
|
|
1595
1545
|
function useProductsByHandles(options = {}) {
|
|
1596
1546
|
const { client, locale } = useShopify();
|
|
@@ -1610,41 +1560,811 @@ function useProductsByHandles(options = {}) {
|
|
|
1610
1560
|
metafieldIdentifiers
|
|
1611
1561
|
});
|
|
1612
1562
|
},
|
|
1613
|
-
|
|
1614
|
-
revalidateOnFocus: false
|
|
1563
|
+
{
|
|
1564
|
+
revalidateOnFocus: false,
|
|
1565
|
+
...swrOptions
|
|
1615
1566
|
}
|
|
1616
1567
|
);
|
|
1617
1568
|
}
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1569
|
+
|
|
1570
|
+
// src/hooks/member/plus/use-plus-member-variants.ts
|
|
1571
|
+
function usePlusMemberVariants({
|
|
1572
|
+
memberSetting
|
|
1573
|
+
}) {
|
|
1574
|
+
const plusMonthly = memberSetting?.plus_monthly_product;
|
|
1575
|
+
const plusAnnual = memberSetting?.plus_annual_product;
|
|
1576
|
+
const plusMemberHandles = useMemo(() => {
|
|
1577
|
+
return [plusMonthly?.handle, plusAnnual?.handle].filter(Boolean);
|
|
1578
|
+
}, [plusMonthly?.handle, plusAnnual?.handle]);
|
|
1579
|
+
const { data: plusMemberProducts = [] } = useProductsByHandles({
|
|
1580
|
+
handles: plusMemberHandles
|
|
1627
1581
|
});
|
|
1582
|
+
const monthlyProduct = useMemo(() => {
|
|
1583
|
+
return plusMemberProducts?.find((item) => item?.handle === plusMonthly?.handle);
|
|
1584
|
+
}, [plusMemberProducts, plusMonthly]);
|
|
1585
|
+
const annualProduct = useMemo(() => {
|
|
1586
|
+
return plusMemberProducts?.find((item) => item?.handle === plusAnnual?.handle);
|
|
1587
|
+
}, [plusMemberProducts, plusAnnual]);
|
|
1588
|
+
const monthlyVariant = useMemo(() => {
|
|
1589
|
+
return monthlyProduct?.variants?.find((item) => item.sku === plusMonthly?.sku);
|
|
1590
|
+
}, [monthlyProduct, plusMonthly]);
|
|
1591
|
+
const annualVariant = useMemo(() => {
|
|
1592
|
+
return annualProduct?.variants?.find((item) => item.sku === plusAnnual?.sku);
|
|
1593
|
+
}, [annualProduct, plusAnnual]);
|
|
1594
|
+
return {
|
|
1595
|
+
monthlyVariant: monthlyVariant ? {
|
|
1596
|
+
...monthlyVariant,
|
|
1597
|
+
product: monthlyProduct
|
|
1598
|
+
} : void 0,
|
|
1599
|
+
annualVariant: annualVariant ? {
|
|
1600
|
+
...annualVariant,
|
|
1601
|
+
product: annualProduct
|
|
1602
|
+
} : void 0
|
|
1603
|
+
};
|
|
1628
1604
|
}
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
}
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
useEffect(() => {
|
|
1637
|
-
if (!product) {
|
|
1638
|
-
setVariant(void 0);
|
|
1639
|
-
return;
|
|
1640
|
-
}
|
|
1641
|
-
const newVariant = getVariantFromSelectedOptions(product, selectedOptions);
|
|
1642
|
-
if (newVariant && newVariant.id !== variant?.id) {
|
|
1643
|
-
setVariant(newVariant);
|
|
1644
|
-
} else if (!newVariant) {
|
|
1645
|
-
setVariant(getFirstAvailableVariant(product));
|
|
1605
|
+
var useAvailableDeliveryCoupon = ({
|
|
1606
|
+
profile
|
|
1607
|
+
}) => {
|
|
1608
|
+
const { data: availableDeliveryCoupon, isLoading } = useSWR(
|
|
1609
|
+
profile?.email ? ["/api/multipass/subsrv/v1/prime/delivery_coupons/current/available", profile?.email] : void 0,
|
|
1610
|
+
async ([apiPath]) => {
|
|
1611
|
+
return fetch(apiPath).then((res) => res.json());
|
|
1646
1612
|
}
|
|
1647
|
-
|
|
1613
|
+
);
|
|
1614
|
+
console.log("availableDeliveryCoupon", availableDeliveryCoupon);
|
|
1615
|
+
const { ndd_coupon: nddCoupon, tdd_coupon: tddCoupon } = availableDeliveryCoupon?.data?.data || {};
|
|
1616
|
+
return {
|
|
1617
|
+
nddCoupon,
|
|
1618
|
+
tddCoupon,
|
|
1619
|
+
isLoading
|
|
1620
|
+
};
|
|
1621
|
+
};
|
|
1622
|
+
|
|
1623
|
+
// src/hooks/member/plus/use-shipping-methods.ts
|
|
1624
|
+
function useShippingMethods(options) {
|
|
1625
|
+
const { variant, plusMemberMetafields, selectedPlusMemberMode, profile } = options;
|
|
1626
|
+
const isPlus = profile?.isPlus || false;
|
|
1627
|
+
const { nddCoupon, tddCoupon, isLoading } = useAvailableDeliveryCoupon({ profile });
|
|
1628
|
+
const { plus_shipping, shippingMethod } = plusMemberMetafields || {};
|
|
1629
|
+
const nddOverweight = useMemo(() => {
|
|
1630
|
+
return (variant?.weight || 0) > (shippingMethod?.overWeight_ndd || Infinity);
|
|
1631
|
+
}, [shippingMethod?.overWeight_ndd, variant?.weight]);
|
|
1632
|
+
const tddOverweight = useMemo(() => {
|
|
1633
|
+
return (variant?.weight || 0) > (shippingMethod?.overWeight_tdd || Infinity);
|
|
1634
|
+
}, [shippingMethod?.overWeight_tdd, variant?.weight]);
|
|
1635
|
+
const paymentShippingMethods = useMemo(() => {
|
|
1636
|
+
const weight = variant?.weight || 0;
|
|
1637
|
+
const methods = plus_shipping?.shipping_methods?.filter(({ weight_low, weight_high, __mode, __plus }) => {
|
|
1638
|
+
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
1639
|
+
return __mode !== "free" /* FREE */ && !__plus && fitWeight;
|
|
1640
|
+
}) || [];
|
|
1641
|
+
return methods.map((method) => {
|
|
1642
|
+
let disabled = false;
|
|
1643
|
+
const selectedFreeMember = selectedPlusMemberMode === "free";
|
|
1644
|
+
if (method.__mode === "ndd" /* NDD */) {
|
|
1645
|
+
disabled = selectedFreeMember || nddOverweight;
|
|
1646
|
+
} else if (method.__mode === "tdd" /* TDD */) {
|
|
1647
|
+
disabled = selectedFreeMember || tddOverweight;
|
|
1648
|
+
}
|
|
1649
|
+
return {
|
|
1650
|
+
...method,
|
|
1651
|
+
id: method.__mode + method.__code,
|
|
1652
|
+
useCoupon: false,
|
|
1653
|
+
subtitle: plus_shipping?.directly || "",
|
|
1654
|
+
disabled
|
|
1655
|
+
};
|
|
1656
|
+
});
|
|
1657
|
+
}, [
|
|
1658
|
+
nddOverweight,
|
|
1659
|
+
plus_shipping?.directly,
|
|
1660
|
+
plus_shipping?.shipping_methods,
|
|
1661
|
+
selectedPlusMemberMode,
|
|
1662
|
+
tddOverweight,
|
|
1663
|
+
variant?.weight
|
|
1664
|
+
]);
|
|
1665
|
+
const nddPrice = useMemo(() => {
|
|
1666
|
+
const weight = variant?.weight || 0;
|
|
1667
|
+
const nddMethod = paymentShippingMethods.find(({ __mode, weight_high, weight_low }) => {
|
|
1668
|
+
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
1669
|
+
return __mode === "ndd" && fitWeight;
|
|
1670
|
+
});
|
|
1671
|
+
return nddMethod?.price || 0;
|
|
1672
|
+
}, [variant?.weight, paymentShippingMethods]);
|
|
1673
|
+
const tddPrice = useMemo(() => {
|
|
1674
|
+
const weight = variant?.weight || 0;
|
|
1675
|
+
const tddMethod = paymentShippingMethods.find(({ __mode, weight_high, weight_low }) => {
|
|
1676
|
+
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
1677
|
+
return __mode === "tdd" && fitWeight;
|
|
1678
|
+
});
|
|
1679
|
+
return tddMethod?.price || 0;
|
|
1680
|
+
}, [variant?.weight, paymentShippingMethods]);
|
|
1681
|
+
const freeShippingMethods = useMemo(() => {
|
|
1682
|
+
const weight = variant?.weight || 0;
|
|
1683
|
+
let methods = plus_shipping?.shipping_methods?.filter(({ __mode, __plus, weight_low, weight_high }) => {
|
|
1684
|
+
if (__mode === "free" /* FREE */) {
|
|
1685
|
+
return true;
|
|
1686
|
+
}
|
|
1687
|
+
if (isPlus) {
|
|
1688
|
+
const hasCoupon = isPlus && __mode === "ndd" /* NDD */ && nddCoupon || isPlus && __mode === "tdd" /* TDD */ && (tddCoupon || nddCoupon);
|
|
1689
|
+
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
1690
|
+
return hasCoupon && fitWeight && !__plus;
|
|
1691
|
+
} else {
|
|
1692
|
+
return __plus;
|
|
1693
|
+
}
|
|
1694
|
+
}) || [];
|
|
1695
|
+
if (isPlus) {
|
|
1696
|
+
methods = methods.sort((a, b) => {
|
|
1697
|
+
if (b.__mode === "free" /* FREE */) return -1;
|
|
1698
|
+
return 0;
|
|
1699
|
+
});
|
|
1700
|
+
}
|
|
1701
|
+
return methods.map((method) => {
|
|
1702
|
+
let price = 0;
|
|
1703
|
+
let coupon;
|
|
1704
|
+
let disabled;
|
|
1705
|
+
if (method.__mode !== "free" /* FREE */) {
|
|
1706
|
+
switch (method.__mode) {
|
|
1707
|
+
case "tdd":
|
|
1708
|
+
price = tddPrice;
|
|
1709
|
+
coupon = tddCoupon || nddCoupon;
|
|
1710
|
+
break;
|
|
1711
|
+
case "ndd":
|
|
1712
|
+
price = nddPrice;
|
|
1713
|
+
coupon = nddCoupon;
|
|
1714
|
+
break;
|
|
1715
|
+
}
|
|
1716
|
+
disabled = selectedPlusMemberMode === "free";
|
|
1717
|
+
if (method.__mode === "ndd" /* NDD */) {
|
|
1718
|
+
disabled = disabled || nddOverweight;
|
|
1719
|
+
} else if (method.__mode === "tdd" /* TDD */) {
|
|
1720
|
+
disabled = disabled || tddOverweight;
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
return {
|
|
1724
|
+
...method,
|
|
1725
|
+
id: method.__mode + method.__code,
|
|
1726
|
+
useCoupon: true,
|
|
1727
|
+
disabled,
|
|
1728
|
+
coupon,
|
|
1729
|
+
price
|
|
1730
|
+
};
|
|
1731
|
+
});
|
|
1732
|
+
}, [
|
|
1733
|
+
variant?.weight,
|
|
1734
|
+
plus_shipping?.shipping_methods,
|
|
1735
|
+
isPlus,
|
|
1736
|
+
nddCoupon,
|
|
1737
|
+
tddCoupon,
|
|
1738
|
+
selectedPlusMemberMode,
|
|
1739
|
+
tddPrice,
|
|
1740
|
+
nddPrice,
|
|
1741
|
+
nddOverweight,
|
|
1742
|
+
tddOverweight
|
|
1743
|
+
]);
|
|
1744
|
+
return {
|
|
1745
|
+
freeShippingMethods,
|
|
1746
|
+
paymentShippingMethods,
|
|
1747
|
+
nddOverweight,
|
|
1748
|
+
tddOverweight,
|
|
1749
|
+
nddCoupon,
|
|
1750
|
+
tddCoupon,
|
|
1751
|
+
isLoadingCoupon: isLoading
|
|
1752
|
+
};
|
|
1753
|
+
}
|
|
1754
|
+
var useReplaceCartPlusMember = () => {
|
|
1755
|
+
const { plusMemberMetafields, selectedPlusMemberMode } = usePlusMemberContext();
|
|
1756
|
+
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
1757
|
+
const { cart } = useCartContext();
|
|
1758
|
+
const plusMonthly = plusMemberMetafields?.plus_monthly_product;
|
|
1759
|
+
const plusAnnual = plusMemberMetafields?.plus_annual_product;
|
|
1760
|
+
const handler = useCallback(async () => {
|
|
1761
|
+
const plusMonthlyInCart = cart?.lineItems.find(
|
|
1762
|
+
(item) => item.variant?.sku === plusMonthly?.sku
|
|
1763
|
+
);
|
|
1764
|
+
const plusAnnualInCart = cart?.lineItems.find(
|
|
1765
|
+
(item) => item.variant?.sku === plusAnnual?.sku
|
|
1766
|
+
);
|
|
1767
|
+
if (selectedPlusMemberMode === "annual" /* ANNUAL */ && plusMonthlyInCart) {
|
|
1768
|
+
await removeCartLines2({
|
|
1769
|
+
lineIds: [plusMonthlyInCart.id]
|
|
1770
|
+
});
|
|
1771
|
+
} else if (selectedPlusMemberMode === "monthly" /* MONTHLY */ && plusAnnualInCart) {
|
|
1772
|
+
await removeCartLines2({
|
|
1773
|
+
lineIds: [plusAnnualInCart.id]
|
|
1774
|
+
});
|
|
1775
|
+
}
|
|
1776
|
+
}, [
|
|
1777
|
+
cart?.lineItems,
|
|
1778
|
+
selectedPlusMemberMode,
|
|
1779
|
+
plusMonthly?.sku,
|
|
1780
|
+
plusAnnual?.sku,
|
|
1781
|
+
removeCartLines2
|
|
1782
|
+
]);
|
|
1783
|
+
return handler;
|
|
1784
|
+
};
|
|
1785
|
+
var usePlusMemberCheckoutCustomAttributes = ({
|
|
1786
|
+
disableShipping = false,
|
|
1787
|
+
isPresaleContains = false
|
|
1788
|
+
}) => {
|
|
1789
|
+
const { profile, selectedShippingMethod, selectedPlusMemberMode } = usePlusMemberContext();
|
|
1790
|
+
return useMemo(() => {
|
|
1791
|
+
const checkoutCustomAttributes = [
|
|
1792
|
+
{
|
|
1793
|
+
key: "_last_url",
|
|
1794
|
+
value: typeof window !== "undefined" ? window.location.origin + window.location.pathname : ""
|
|
1795
|
+
}
|
|
1796
|
+
];
|
|
1797
|
+
checkoutCustomAttributes.push({
|
|
1798
|
+
key: "_checkout_delivery_custom",
|
|
1799
|
+
value: JSON.stringify({
|
|
1800
|
+
allow_nextday_delivery: true,
|
|
1801
|
+
allow_thirdday_delivery: true,
|
|
1802
|
+
selected_delivery_option: {
|
|
1803
|
+
code: selectedShippingMethod?.__code,
|
|
1804
|
+
mode: selectedShippingMethod?.__mode
|
|
1805
|
+
},
|
|
1806
|
+
is_presale: isPresaleContains,
|
|
1807
|
+
discount_code: selectedShippingMethod?.coupon ? [selectedShippingMethod.coupon] : [],
|
|
1808
|
+
plus_type: profile?.isPlus ? "free" /* FREE */ : selectedPlusMemberMode,
|
|
1809
|
+
is_prime: profile?.isPlus
|
|
1810
|
+
})
|
|
1811
|
+
});
|
|
1812
|
+
if (disableShipping) {
|
|
1813
|
+
checkoutCustomAttributes.push({
|
|
1814
|
+
key: "_hide_shipping",
|
|
1815
|
+
value: "true"
|
|
1816
|
+
});
|
|
1817
|
+
}
|
|
1818
|
+
return checkoutCustomAttributes;
|
|
1819
|
+
}, [profile, selectedShippingMethod, selectedPlusMemberMode, isPresaleContains]);
|
|
1820
|
+
};
|
|
1821
|
+
function useRemoveCartLines(options) {
|
|
1822
|
+
const { client, locale, cartCookieAdapter } = useShopify();
|
|
1823
|
+
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
1824
|
+
const removeLines = useCallback(
|
|
1825
|
+
async (_key, { arg }) => {
|
|
1826
|
+
const { autoRemoveInvalidCodes = true, onCodesRemoved, cartId, lineIds } = arg;
|
|
1827
|
+
let updatedCart = await removeCartLines(client, {
|
|
1828
|
+
cartId,
|
|
1829
|
+
lineIds,
|
|
1830
|
+
metafieldIdentifiers,
|
|
1831
|
+
cookieAdapter: cartCookieAdapter
|
|
1832
|
+
});
|
|
1833
|
+
if (updatedCart && autoRemoveInvalidCodes) {
|
|
1834
|
+
const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
1835
|
+
if (unApplicableCodes.length > 0) {
|
|
1836
|
+
if (onCodesRemoved) {
|
|
1837
|
+
const handledCart = await onCodesRemoved(updatedCart, unApplicableCodes);
|
|
1838
|
+
if (handledCart) {
|
|
1839
|
+
updatedCart = handledCart;
|
|
1840
|
+
}
|
|
1841
|
+
} else {
|
|
1842
|
+
updatedCart = await updateCartCodes(client, {
|
|
1843
|
+
cartId: updatedCart.id,
|
|
1844
|
+
discountCodes: updatedCart.discountCodes.filter((item) => item.applicable).map((item) => item.code),
|
|
1845
|
+
metafieldIdentifiers,
|
|
1846
|
+
cookieAdapter: cartCookieAdapter
|
|
1847
|
+
}) || updatedCart;
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
if (updatedCart) {
|
|
1852
|
+
mutateCart(updatedCart);
|
|
1853
|
+
}
|
|
1854
|
+
return updatedCart;
|
|
1855
|
+
},
|
|
1856
|
+
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
1857
|
+
);
|
|
1858
|
+
return useSWRMutation("remove-cart-lines", removeLines, options);
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
// src/hooks/member/plus/use-auto-remove-plus-member-in-cart.ts
|
|
1862
|
+
function useAutoRemovePlusMemberInCart({
|
|
1863
|
+
cart,
|
|
1864
|
+
profile,
|
|
1865
|
+
memberSetting
|
|
1866
|
+
}) {
|
|
1867
|
+
const { plus_monthly_product, plus_annual_product } = memberSetting || {};
|
|
1868
|
+
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
1869
|
+
useEffect(() => {
|
|
1870
|
+
if (!cart || !plus_monthly_product || !plus_annual_product) return;
|
|
1871
|
+
const removePlusProduct = async (productType) => {
|
|
1872
|
+
if (!productType) return;
|
|
1873
|
+
const product = cart.lineItems?.find(
|
|
1874
|
+
(item) => item.product?.handle === productType?.handle && item.variant?.sku === productType?.sku
|
|
1875
|
+
);
|
|
1876
|
+
if (product) {
|
|
1877
|
+
await removeCartLines2({
|
|
1878
|
+
lineIds: [product.id]
|
|
1879
|
+
});
|
|
1880
|
+
}
|
|
1881
|
+
};
|
|
1882
|
+
if (profile?.isMonthlyPlus) {
|
|
1883
|
+
removePlusProduct(plus_monthly_product);
|
|
1884
|
+
}
|
|
1885
|
+
if (profile?.isAnnualPlus) {
|
|
1886
|
+
removePlusProduct(plus_annual_product);
|
|
1887
|
+
}
|
|
1888
|
+
}, [cart, plus_annual_product, plus_monthly_product, profile, removeCartLines2]);
|
|
1889
|
+
}
|
|
1890
|
+
function hasPlusMemberInCart({
|
|
1891
|
+
memberSetting,
|
|
1892
|
+
cart
|
|
1893
|
+
}) {
|
|
1894
|
+
const { plus_monthly_product, plus_annual_product } = memberSetting || {};
|
|
1895
|
+
if (!cart?.lineItems) {
|
|
1896
|
+
return {
|
|
1897
|
+
hasPlusMember: false,
|
|
1898
|
+
hasMonthlyPlus: false,
|
|
1899
|
+
hasAnnualPlus: false
|
|
1900
|
+
};
|
|
1901
|
+
}
|
|
1902
|
+
const monthlyPlusItem = cart.lineItems.find(
|
|
1903
|
+
(item) => item.product?.handle === plus_monthly_product?.handle && item.variant?.sku === plus_monthly_product?.sku
|
|
1904
|
+
);
|
|
1905
|
+
const annualPlusItem = cart.lineItems.find(
|
|
1906
|
+
(item) => item.product?.handle === plus_annual_product?.handle && item.variant?.sku === plus_annual_product?.sku
|
|
1907
|
+
);
|
|
1908
|
+
const hasMonthlyPlus = !!monthlyPlusItem;
|
|
1909
|
+
const hasAnnualPlus = !!annualPlusItem;
|
|
1910
|
+
const hasPlusMember = hasMonthlyPlus || hasAnnualPlus;
|
|
1911
|
+
return {
|
|
1912
|
+
hasPlusMember,
|
|
1913
|
+
hasMonthlyPlus,
|
|
1914
|
+
hasAnnualPlus,
|
|
1915
|
+
monthlyPlusItem,
|
|
1916
|
+
annualPlusItem
|
|
1917
|
+
};
|
|
1918
|
+
}
|
|
1919
|
+
function useHasPlusMemberInCart({
|
|
1920
|
+
memberSetting,
|
|
1921
|
+
cart
|
|
1922
|
+
}) {
|
|
1923
|
+
return useMemo(
|
|
1924
|
+
() => hasPlusMemberInCart({
|
|
1925
|
+
memberSetting,
|
|
1926
|
+
cart
|
|
1927
|
+
}),
|
|
1928
|
+
[memberSetting, cart]
|
|
1929
|
+
);
|
|
1930
|
+
}
|
|
1931
|
+
function hasPlusMemberInLines({
|
|
1932
|
+
memberSetting,
|
|
1933
|
+
lines
|
|
1934
|
+
}) {
|
|
1935
|
+
const { plus_monthly_product, plus_annual_product } = memberSetting || {};
|
|
1936
|
+
if (!lines || lines.length === 0) {
|
|
1937
|
+
return {
|
|
1938
|
+
hasPlusMember: false,
|
|
1939
|
+
hasMonthlyPlus: false,
|
|
1940
|
+
hasAnnualPlus: false
|
|
1941
|
+
};
|
|
1942
|
+
}
|
|
1943
|
+
const monthlyPlusLine = lines.find((line) => {
|
|
1944
|
+
const variantHandle = line.variant?.product?.handle;
|
|
1945
|
+
const variantSku = line.variant?.sku;
|
|
1946
|
+
return variantHandle === plus_monthly_product?.handle && variantSku === plus_monthly_product?.sku;
|
|
1947
|
+
});
|
|
1948
|
+
const annualPlusLine = lines.find((line) => {
|
|
1949
|
+
const variantHandle = line.variant?.product?.handle;
|
|
1950
|
+
const variantSku = line.variant?.sku;
|
|
1951
|
+
return variantHandle === plus_annual_product?.handle && variantSku === plus_annual_product?.sku;
|
|
1952
|
+
});
|
|
1953
|
+
const hasMonthlyPlus = !!monthlyPlusLine;
|
|
1954
|
+
const hasAnnualPlus = !!annualPlusLine;
|
|
1955
|
+
const hasPlusMember = hasMonthlyPlus || hasAnnualPlus;
|
|
1956
|
+
return {
|
|
1957
|
+
hasPlusMember,
|
|
1958
|
+
hasMonthlyPlus,
|
|
1959
|
+
hasAnnualPlus,
|
|
1960
|
+
monthlyPlusLine,
|
|
1961
|
+
annualPlusLine
|
|
1962
|
+
};
|
|
1963
|
+
}
|
|
1964
|
+
function useHasPlusMemberInLines({
|
|
1965
|
+
memberSetting,
|
|
1966
|
+
lines
|
|
1967
|
+
}) {
|
|
1968
|
+
return useMemo(
|
|
1969
|
+
() => hasPlusMemberInLines({
|
|
1970
|
+
memberSetting,
|
|
1971
|
+
lines
|
|
1972
|
+
}),
|
|
1973
|
+
[memberSetting, lines]
|
|
1974
|
+
);
|
|
1975
|
+
}
|
|
1976
|
+
function usePlusMemberNeedAddToCart({
|
|
1977
|
+
cart,
|
|
1978
|
+
profile
|
|
1979
|
+
}) {
|
|
1980
|
+
const { selectedPlusMemberMode, selectedPlusMemberVariant, plusMemberMetafields } = usePlusMemberContext();
|
|
1981
|
+
const { hasMonthlyPlus, hasAnnualPlus } = useHasPlusMemberInCart({
|
|
1982
|
+
memberSetting: plusMemberMetafields,
|
|
1983
|
+
cart
|
|
1984
|
+
});
|
|
1985
|
+
const plusMemberVariant = useMemo(() => {
|
|
1986
|
+
if (!selectedPlusMemberVariant || selectedPlusMemberMode === "free" /* FREE */) {
|
|
1987
|
+
return void 0;
|
|
1988
|
+
}
|
|
1989
|
+
if (selectedPlusMemberMode === "monthly" /* MONTHLY */ && hasMonthlyPlus) {
|
|
1990
|
+
return void 0;
|
|
1991
|
+
}
|
|
1992
|
+
if (selectedPlusMemberMode === "annual" /* ANNUAL */ && hasAnnualPlus) {
|
|
1993
|
+
return void 0;
|
|
1994
|
+
}
|
|
1995
|
+
if (profile?.isMonthlyPlus && selectedPlusMemberMode === "monthly" /* MONTHLY */) {
|
|
1996
|
+
return void 0;
|
|
1997
|
+
}
|
|
1998
|
+
if (profile?.isAnnualPlus && selectedPlusMemberMode === "annual" /* ANNUAL */) {
|
|
1999
|
+
return void 0;
|
|
2000
|
+
}
|
|
2001
|
+
return selectedPlusMemberVariant;
|
|
2002
|
+
}, [selectedPlusMemberMode, selectedPlusMemberVariant, hasMonthlyPlus, hasAnnualPlus]);
|
|
2003
|
+
return plusMemberVariant;
|
|
2004
|
+
}
|
|
2005
|
+
var PlusMemberProvider = ({
|
|
2006
|
+
variant,
|
|
2007
|
+
product,
|
|
2008
|
+
memberSetting,
|
|
2009
|
+
initialSelectedPlusMemberMode = "free",
|
|
2010
|
+
profile,
|
|
2011
|
+
children
|
|
2012
|
+
}) => {
|
|
2013
|
+
const [selectedPlusMemberMode, setSelectedPlusMemberMode] = useState(
|
|
2014
|
+
initialSelectedPlusMemberMode
|
|
2015
|
+
);
|
|
2016
|
+
const [selectedShippingMethod, setSelectedShippingMethod] = useState();
|
|
2017
|
+
const [showMoreShippingMethod, setShowMoreShippingMethod] = useState(false);
|
|
2018
|
+
const [showPlusMemberBenefit, setShowPlusMemberBenefit] = useState(false);
|
|
2019
|
+
const shippingMethodsContext = useShippingMethods({
|
|
2020
|
+
variant,
|
|
2021
|
+
plusMemberMetafields: memberSetting,
|
|
2022
|
+
selectedPlusMemberMode,
|
|
2023
|
+
profile
|
|
2024
|
+
});
|
|
2025
|
+
const { monthlyVariant, annualVariant } = usePlusMemberVariants({
|
|
2026
|
+
memberSetting
|
|
2027
|
+
});
|
|
2028
|
+
const selectedPlusMemberVariant = useMemo(() => {
|
|
2029
|
+
if (selectedPlusMemberMode === "free" /* FREE */) {
|
|
2030
|
+
return void 0;
|
|
2031
|
+
}
|
|
2032
|
+
return selectedPlusMemberMode === "monthly" /* MONTHLY */ ? monthlyVariant : annualVariant;
|
|
2033
|
+
}, [monthlyVariant, annualVariant, selectedPlusMemberMode]);
|
|
2034
|
+
return /* @__PURE__ */ jsx(
|
|
2035
|
+
PlusMemberContext.Provider,
|
|
2036
|
+
{
|
|
2037
|
+
value: {
|
|
2038
|
+
variant,
|
|
2039
|
+
plusMemberMetafields: memberSetting,
|
|
2040
|
+
selectedPlusMemberMode,
|
|
2041
|
+
setSelectedPlusMemberMode,
|
|
2042
|
+
selectedShippingMethod,
|
|
2043
|
+
setSelectedShippingMethod,
|
|
2044
|
+
shippingMethodsContext,
|
|
2045
|
+
showMoreShippingMethod,
|
|
2046
|
+
setShowMoreShippingMethod,
|
|
2047
|
+
selectedPlusMemberVariant,
|
|
2048
|
+
product,
|
|
2049
|
+
showPlusMemberBenefit,
|
|
2050
|
+
setShowPlusMemberBenefit,
|
|
2051
|
+
profile
|
|
2052
|
+
},
|
|
2053
|
+
children
|
|
2054
|
+
}
|
|
2055
|
+
);
|
|
2056
|
+
};
|
|
2057
|
+
|
|
2058
|
+
// src/hooks/cart/use-add-to-cart.ts
|
|
2059
|
+
function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
2060
|
+
const { client, config, locale, cartCookieAdapter, userAdapter, performanceAdapter } = useShopify();
|
|
2061
|
+
const { cart, addCustomAttributes, memberSetting, profile, customer } = useCartContext();
|
|
2062
|
+
const { trigger: applyCartCodes } = useApplyCartCodes();
|
|
2063
|
+
const { trigger: removeInvalidCodes } = useRemoveCartCodes();
|
|
2064
|
+
const { trigger: addCartLines2 } = useAddCartLines();
|
|
2065
|
+
const { trigger: createCart4 } = useCreateCart({
|
|
2066
|
+
updateCookie: true
|
|
2067
|
+
});
|
|
2068
|
+
const { hasPlusMember } = useHasPlusMemberInCart({
|
|
2069
|
+
memberSetting,
|
|
2070
|
+
cart
|
|
2071
|
+
});
|
|
2072
|
+
const { attributes: cartAttributes } = useCartAttributes({
|
|
2073
|
+
profile,
|
|
2074
|
+
customer,
|
|
2075
|
+
cart,
|
|
2076
|
+
memberType: hasPlusMember ? "2" : String(profile?.memberType ?? 0)
|
|
2077
|
+
});
|
|
2078
|
+
const addToCart = useCallback(
|
|
2079
|
+
async (_key, { arg }) => {
|
|
2080
|
+
const {
|
|
2081
|
+
lineItems,
|
|
2082
|
+
cartId: providedCartId,
|
|
2083
|
+
discountCodes,
|
|
2084
|
+
gtmParams = {},
|
|
2085
|
+
buyerIdentity,
|
|
2086
|
+
needCreateCart = false,
|
|
2087
|
+
onCodesInvalid,
|
|
2088
|
+
replaceExistingCodes,
|
|
2089
|
+
customAttributes
|
|
2090
|
+
} = arg;
|
|
2091
|
+
if (!lineItems || lineItems.length === 0) {
|
|
2092
|
+
return;
|
|
2093
|
+
}
|
|
2094
|
+
performanceAdapter?.addToCartStart();
|
|
2095
|
+
const linesWithFunctionAttributes = getLinesWithAttributes({ cart, lineItems });
|
|
2096
|
+
const lines = linesWithFunctionAttributes.map((item) => ({
|
|
2097
|
+
merchandiseId: item.variant?.id || "",
|
|
2098
|
+
quantity: item.quantity || 1,
|
|
2099
|
+
attributes: item.attributes,
|
|
2100
|
+
sellingPlanId: item.sellingPlanId
|
|
2101
|
+
})).filter((item) => item.merchandiseId && item.quantity);
|
|
2102
|
+
if (lines.length === 0) {
|
|
2103
|
+
return;
|
|
2104
|
+
}
|
|
2105
|
+
let cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
|
|
2106
|
+
let resultCart = null;
|
|
2107
|
+
if (!cartId) {
|
|
2108
|
+
resultCart = await createCart4({
|
|
2109
|
+
lines,
|
|
2110
|
+
buyerIdentity,
|
|
2111
|
+
discountCodes,
|
|
2112
|
+
customAttributes: [...cartAttributes, ...customAttributes || []]
|
|
2113
|
+
// 初次加购时,就把所有 cart attributes 带上
|
|
2114
|
+
});
|
|
2115
|
+
} else {
|
|
2116
|
+
resultCart = await addCartLines2({
|
|
2117
|
+
cartId,
|
|
2118
|
+
lines
|
|
2119
|
+
});
|
|
2120
|
+
console.log("npm addCartLines resultCart", resultCart);
|
|
2121
|
+
if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
|
|
2122
|
+
const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
2123
|
+
if (unapplicableCodes.length > 0) {
|
|
2124
|
+
if (onCodesInvalid) {
|
|
2125
|
+
const handledCart = await onCodesInvalid(resultCart, unapplicableCodes);
|
|
2126
|
+
if (handledCart) {
|
|
2127
|
+
resultCart = handledCart;
|
|
2128
|
+
}
|
|
2129
|
+
} else {
|
|
2130
|
+
await removeInvalidCodes({
|
|
2131
|
+
discountCodes: unapplicableCodes
|
|
2132
|
+
});
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
if (resultCart && discountCodes && discountCodes.length > 0) {
|
|
2137
|
+
applyCartCodes({
|
|
2138
|
+
replaceExistingCodes,
|
|
2139
|
+
discountCodes
|
|
2140
|
+
});
|
|
2141
|
+
}
|
|
2142
|
+
if (customAttributes && customAttributes.length > 0) {
|
|
2143
|
+
addCustomAttributes(customAttributes);
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
if (withTrack) {
|
|
2147
|
+
trackAddToCartGA({
|
|
2148
|
+
lineItems,
|
|
2149
|
+
gtmParams: { ...gtmParams, brand: config.getBrand() }
|
|
2150
|
+
});
|
|
2151
|
+
trackAddToCartFBQ({ lineItems });
|
|
2152
|
+
}
|
|
2153
|
+
performanceAdapter?.addToCartEnd();
|
|
2154
|
+
return resultCart;
|
|
2155
|
+
},
|
|
2156
|
+
[
|
|
2157
|
+
client,
|
|
2158
|
+
locale,
|
|
2159
|
+
cartCookieAdapter,
|
|
2160
|
+
userAdapter,
|
|
2161
|
+
cart,
|
|
2162
|
+
withTrack,
|
|
2163
|
+
performanceAdapter,
|
|
2164
|
+
createCart4,
|
|
2165
|
+
addCartLines2,
|
|
2166
|
+
applyCartCodes,
|
|
2167
|
+
removeInvalidCodes,
|
|
2168
|
+
addCustomAttributes,
|
|
2169
|
+
config
|
|
2170
|
+
]
|
|
2171
|
+
);
|
|
2172
|
+
return useSWRMutation("add-to-cart", addToCart, swrOptions);
|
|
2173
|
+
}
|
|
2174
|
+
function useUpdateCartLines(options) {
|
|
2175
|
+
const { client, locale, cartCookieAdapter } = useShopify();
|
|
2176
|
+
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
2177
|
+
const updateLines = useCallback(
|
|
2178
|
+
async (_key, { arg }) => {
|
|
2179
|
+
const updatedCart = await updateCartLines(client, {
|
|
2180
|
+
...arg,
|
|
2181
|
+
metafieldIdentifiers,
|
|
2182
|
+
cookieAdapter: cartCookieAdapter
|
|
2183
|
+
});
|
|
2184
|
+
if (updatedCart) {
|
|
2185
|
+
mutateCart(updatedCart);
|
|
2186
|
+
}
|
|
2187
|
+
console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
|
|
2188
|
+
return updatedCart;
|
|
2189
|
+
},
|
|
2190
|
+
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
2191
|
+
);
|
|
2192
|
+
return useSWRMutation("update-cart-lines", updateLines, options);
|
|
2193
|
+
}
|
|
2194
|
+
function useUpdateCartAttributes({
|
|
2195
|
+
mutate,
|
|
2196
|
+
metafieldIdentifiers,
|
|
2197
|
+
disabled = false,
|
|
2198
|
+
swrOptions
|
|
2199
|
+
}) {
|
|
2200
|
+
const { client, locale, cartCookieAdapter } = useShopify();
|
|
2201
|
+
const updateAttributes = useCallback(
|
|
2202
|
+
async (_key, { arg }) => {
|
|
2203
|
+
if (disabled) {
|
|
2204
|
+
return void 0;
|
|
2205
|
+
}
|
|
2206
|
+
const updatedCart = await updateCartAttributes(client, {
|
|
2207
|
+
...arg,
|
|
2208
|
+
metafieldIdentifiers,
|
|
2209
|
+
cookieAdapter: cartCookieAdapter
|
|
2210
|
+
});
|
|
2211
|
+
if (updatedCart) {
|
|
2212
|
+
mutate(updatedCart);
|
|
2213
|
+
}
|
|
2214
|
+
return updatedCart;
|
|
2215
|
+
},
|
|
2216
|
+
[client, locale, cartCookieAdapter, mutate, metafieldIdentifiers, disabled]
|
|
2217
|
+
);
|
|
2218
|
+
return useSWRMutation("update-cart-attributes", updateAttributes, swrOptions);
|
|
2219
|
+
}
|
|
2220
|
+
function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
2221
|
+
const { client, config, locale, cartCookieAdapter, userAdapter } = useShopify();
|
|
2222
|
+
const { profile, customer, memberSetting } = useCartContext();
|
|
2223
|
+
const isLoggedIn = userAdapter?.isLoggedIn || false;
|
|
2224
|
+
const buyNow = useCallback(
|
|
2225
|
+
async (_key, { arg }) => {
|
|
2226
|
+
const {
|
|
2227
|
+
lineItems,
|
|
2228
|
+
discountCodes,
|
|
2229
|
+
gtmParams = {},
|
|
2230
|
+
buyerIdentity,
|
|
2231
|
+
fbqTrackConfig,
|
|
2232
|
+
customAttributes,
|
|
2233
|
+
metafieldIdentifiers,
|
|
2234
|
+
redirectToCheckout
|
|
2235
|
+
} = arg;
|
|
2236
|
+
if (!lineItems || lineItems.length === 0) {
|
|
2237
|
+
return;
|
|
2238
|
+
}
|
|
2239
|
+
const { hasPlusMember } = hasPlusMemberInLines({
|
|
2240
|
+
memberSetting,
|
|
2241
|
+
lines: lineItems
|
|
2242
|
+
});
|
|
2243
|
+
const memberType = hasPlusMember ? "2" : String(profile?.memberType ?? 0);
|
|
2244
|
+
const cartAttributes = getCartAttributes({
|
|
2245
|
+
profile,
|
|
2246
|
+
customer,
|
|
2247
|
+
memberType,
|
|
2248
|
+
currentUrl: window.location.href
|
|
2249
|
+
});
|
|
2250
|
+
const linesWithFunctionAttributes = getLinesWithAttributes({
|
|
2251
|
+
lineItems
|
|
2252
|
+
});
|
|
2253
|
+
const lines = linesWithFunctionAttributes.map((item) => ({
|
|
2254
|
+
merchandiseId: item.variant?.id || "",
|
|
2255
|
+
quantity: item.quantity || 1,
|
|
2256
|
+
attributes: item.attributes,
|
|
2257
|
+
sellingPlanId: item.sellingPlanId
|
|
2258
|
+
})).filter((item) => item.merchandiseId);
|
|
2259
|
+
if (lines.length === 0) {
|
|
2260
|
+
return;
|
|
2261
|
+
}
|
|
2262
|
+
const resultCart = await createCart(client, {
|
|
2263
|
+
lines,
|
|
2264
|
+
metafieldIdentifiers,
|
|
2265
|
+
cookieAdapter: cartCookieAdapter,
|
|
2266
|
+
buyerIdentity,
|
|
2267
|
+
discountCodes,
|
|
2268
|
+
customAttributes: [...cartAttributes, ...customAttributes || []]
|
|
2269
|
+
});
|
|
2270
|
+
if (!resultCart) {
|
|
2271
|
+
throw new Error("Failed to create cart for buy now");
|
|
2272
|
+
}
|
|
2273
|
+
if (withTrack && resultCart.lineItems) {
|
|
2274
|
+
trackBuyNowGA({
|
|
2275
|
+
lineItems,
|
|
2276
|
+
gtmParams: { ...gtmParams, brand: config.getBrand() }
|
|
2277
|
+
});
|
|
2278
|
+
if (fbqTrackConfig) {
|
|
2279
|
+
trackBuyNowFBQ({ trackConfig: fbqTrackConfig });
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
if (redirectToCheckout) {
|
|
2283
|
+
if (resultCart.url) {
|
|
2284
|
+
if (typeof window !== "undefined") {
|
|
2285
|
+
window.location.href = resultCart.url;
|
|
2286
|
+
}
|
|
2287
|
+
} else {
|
|
2288
|
+
throw new Error("Failed to get checkout URL");
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
return resultCart;
|
|
2292
|
+
},
|
|
2293
|
+
[client, locale, isLoggedIn, cartCookieAdapter, withTrack, customer, profile, memberSetting]
|
|
2294
|
+
);
|
|
2295
|
+
return useSWRMutation("buy-now", buyNow, swrOptions);
|
|
2296
|
+
}
|
|
2297
|
+
|
|
2298
|
+
// src/hooks/cart/types/price-discount.ts
|
|
2299
|
+
var PriceDiscountType = /* @__PURE__ */ ((PriceDiscountType2) => {
|
|
2300
|
+
PriceDiscountType2[PriceDiscountType2["PERCENTAGE"] = 1] = "PERCENTAGE";
|
|
2301
|
+
PriceDiscountType2[PriceDiscountType2["FIXED_AMOUNT"] = 2] = "FIXED_AMOUNT";
|
|
2302
|
+
return PriceDiscountType2;
|
|
2303
|
+
})(PriceDiscountType || {});
|
|
2304
|
+
var PriceBasePriceType = /* @__PURE__ */ ((PriceBasePriceType2) => {
|
|
2305
|
+
PriceBasePriceType2[PriceBasePriceType2["MIN_DISCOUNTED_PRICE"] = 1] = "MIN_DISCOUNTED_PRICE";
|
|
2306
|
+
PriceBasePriceType2[PriceBasePriceType2["MIN_TOTAL_PRICE"] = 2] = "MIN_TOTAL_PRICE";
|
|
2307
|
+
return PriceBasePriceType2;
|
|
2308
|
+
})(PriceBasePriceType || {});
|
|
2309
|
+
function useProduct(options = {}) {
|
|
2310
|
+
const { client, locale } = useShopify();
|
|
2311
|
+
const { handle, metafieldIdentifiers, ...swrOptions } = options;
|
|
2312
|
+
return useSWR(
|
|
2313
|
+
handle ? ["product", locale, handle, metafieldIdentifiers] : null,
|
|
2314
|
+
() => getProduct(client, {
|
|
2315
|
+
handle,
|
|
2316
|
+
locale,
|
|
2317
|
+
metafieldIdentifiers
|
|
2318
|
+
}),
|
|
2319
|
+
swrOptions
|
|
2320
|
+
);
|
|
2321
|
+
}
|
|
2322
|
+
function useAllProducts(options = {}) {
|
|
2323
|
+
const { client, locale } = useShopify();
|
|
2324
|
+
const { first, query, sortKey, reverse, metafieldIdentifiers, ...swrOptions } = options;
|
|
2325
|
+
return useSWR(
|
|
2326
|
+
["all-products", locale, first, query, sortKey, reverse, metafieldIdentifiers],
|
|
2327
|
+
() => getAllProducts(client, {
|
|
2328
|
+
locale,
|
|
2329
|
+
first,
|
|
2330
|
+
query,
|
|
2331
|
+
sortKey,
|
|
2332
|
+
reverse,
|
|
2333
|
+
metafieldIdentifiers
|
|
2334
|
+
}),
|
|
2335
|
+
swrOptions
|
|
2336
|
+
);
|
|
2337
|
+
}
|
|
2338
|
+
function getFirstAvailableVariant(product) {
|
|
2339
|
+
const availableVariant = product.variants.find((v) => v.availableForSale);
|
|
2340
|
+
return availableVariant || product.variants[0];
|
|
2341
|
+
}
|
|
2342
|
+
function getVariantFromSelectedOptions(product, selectedOptions) {
|
|
2343
|
+
return product.variants.find((variant) => {
|
|
2344
|
+
return variant.selectedOptions.every((option) => {
|
|
2345
|
+
return selectedOptions[option.name] === option.value;
|
|
2346
|
+
});
|
|
2347
|
+
});
|
|
2348
|
+
}
|
|
2349
|
+
function useVariant({
|
|
2350
|
+
product,
|
|
2351
|
+
selectedOptions
|
|
2352
|
+
}) {
|
|
2353
|
+
const [variant, setVariant] = useState(
|
|
2354
|
+
product ? getFirstAvailableVariant(product) : void 0
|
|
2355
|
+
);
|
|
2356
|
+
useEffect(() => {
|
|
2357
|
+
if (!product) {
|
|
2358
|
+
setVariant(void 0);
|
|
2359
|
+
return;
|
|
2360
|
+
}
|
|
2361
|
+
const newVariant = getVariantFromSelectedOptions(product, selectedOptions);
|
|
2362
|
+
if (newVariant && newVariant.id !== variant?.id) {
|
|
2363
|
+
setVariant(newVariant);
|
|
2364
|
+
} else if (!newVariant) {
|
|
2365
|
+
setVariant(getFirstAvailableVariant(product));
|
|
2366
|
+
}
|
|
2367
|
+
}, [selectedOptions, product, variant?.id]);
|
|
1648
2368
|
return variant;
|
|
1649
2369
|
}
|
|
1650
2370
|
var FAKE_PRICE = 999999999e-2;
|
|
@@ -2048,714 +2768,141 @@ async function performSearch(client, locale, searchQuery, first = 20, types = ["
|
|
|
2048
2768
|
id
|
|
2049
2769
|
handle
|
|
2050
2770
|
title
|
|
2051
|
-
description
|
|
2052
|
-
featuredImage {
|
|
2053
|
-
url
|
|
2054
|
-
altText
|
|
2055
|
-
}
|
|
2056
|
-
}
|
|
2057
|
-
}
|
|
2058
|
-
}
|
|
2059
|
-
pageInfo {
|
|
2060
|
-
hasNextPage
|
|
2061
|
-
endCursor
|
|
2062
|
-
}
|
|
2063
|
-
}
|
|
2064
|
-
}
|
|
2065
|
-
`
|
|
2066
|
-
);
|
|
2067
|
-
const data = await client.query(query, {
|
|
2068
|
-
query: searchQuery,
|
|
2069
|
-
first,
|
|
2070
|
-
types
|
|
2071
|
-
});
|
|
2072
|
-
if (!data || !data.search) {
|
|
2073
|
-
return void 0;
|
|
2074
|
-
}
|
|
2075
|
-
const items = data.search.edges?.map((edge) => {
|
|
2076
|
-
const node = edge.node;
|
|
2077
|
-
const item = {
|
|
2078
|
-
type: node.__typename.toUpperCase(),
|
|
2079
|
-
id: node.id,
|
|
2080
|
-
handle: node.handle,
|
|
2081
|
-
title: node.title
|
|
2082
|
-
};
|
|
2083
|
-
if (node.__typename === "Product") {
|
|
2084
|
-
item.description = node.description;
|
|
2085
|
-
item.image = node.featuredImage ? {
|
|
2086
|
-
url: node.featuredImage.url,
|
|
2087
|
-
altText: node.featuredImage.altText
|
|
2088
|
-
} : void 0;
|
|
2089
|
-
} else if (node.__typename === "Article") {
|
|
2090
|
-
item.description = node.excerpt;
|
|
2091
|
-
item.image = node.image ? {
|
|
2092
|
-
url: node.image.url,
|
|
2093
|
-
altText: node.image.altText
|
|
2094
|
-
} : void 0;
|
|
2095
|
-
}
|
|
2096
|
-
return item;
|
|
2097
|
-
}) || [];
|
|
2098
|
-
return {
|
|
2099
|
-
items,
|
|
2100
|
-
totalCount: data.search.totalCount || 0,
|
|
2101
|
-
pageInfo: data.search.pageInfo
|
|
2102
|
-
};
|
|
2103
|
-
}
|
|
2104
|
-
function useSearch(options = {}) {
|
|
2105
|
-
const { client, locale } = useShopify();
|
|
2106
|
-
const { query, first = 20, types = ["PRODUCT", "ARTICLE", "PAGE"], ...swrOptions } = options;
|
|
2107
|
-
return useSWR(
|
|
2108
|
-
query ? ["search", locale, query, first, types] : null,
|
|
2109
|
-
() => performSearch(client, locale, query, first, types),
|
|
2110
|
-
swrOptions
|
|
2111
|
-
);
|
|
2112
|
-
}
|
|
2113
|
-
async function getSiteInfo(client, locale, metafieldIdentifiers) {
|
|
2114
|
-
const hasMetafields = metafieldIdentifiers && metafieldIdentifiers.length > 0;
|
|
2115
|
-
const query = (
|
|
2116
|
-
/* GraphQL */
|
|
2117
|
-
`
|
|
2118
|
-
query getSiteInfo(
|
|
2119
|
-
${hasMetafields ? "$shopMetafieldIdentifiers: [HasMetafieldsIdentifier!]!" : ""}
|
|
2120
|
-
) @inContext(language: $language) {
|
|
2121
|
-
shop {
|
|
2122
|
-
name
|
|
2123
|
-
description
|
|
2124
|
-
primaryDomain {
|
|
2125
|
-
url
|
|
2126
|
-
host
|
|
2127
|
-
}
|
|
2128
|
-
brand {
|
|
2129
|
-
logo {
|
|
2130
|
-
image {
|
|
2131
|
-
url
|
|
2132
|
-
}
|
|
2133
|
-
}
|
|
2134
|
-
colors {
|
|
2135
|
-
primary {
|
|
2136
|
-
background
|
|
2137
|
-
}
|
|
2138
|
-
secondary {
|
|
2139
|
-
background
|
|
2771
|
+
description
|
|
2772
|
+
featuredImage {
|
|
2773
|
+
url
|
|
2774
|
+
altText
|
|
2775
|
+
}
|
|
2140
2776
|
}
|
|
2141
2777
|
}
|
|
2142
2778
|
}
|
|
2143
|
-
|
|
2779
|
+
pageInfo {
|
|
2780
|
+
hasNextPage
|
|
2781
|
+
endCursor
|
|
2782
|
+
}
|
|
2144
2783
|
}
|
|
2145
2784
|
}
|
|
2146
2785
|
`
|
|
2147
2786
|
);
|
|
2148
|
-
const
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
if (!data || !data.
|
|
2787
|
+
const data = await client.query(query, {
|
|
2788
|
+
query: searchQuery,
|
|
2789
|
+
first,
|
|
2790
|
+
types
|
|
2791
|
+
});
|
|
2792
|
+
if (!data || !data.search) {
|
|
2154
2793
|
return void 0;
|
|
2155
2794
|
}
|
|
2156
|
-
const
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2795
|
+
const items = data.search.edges?.map((edge) => {
|
|
2796
|
+
const node = edge.node;
|
|
2797
|
+
const item = {
|
|
2798
|
+
type: node.__typename.toUpperCase(),
|
|
2799
|
+
id: node.id,
|
|
2800
|
+
handle: node.handle,
|
|
2801
|
+
title: node.title
|
|
2802
|
+
};
|
|
2803
|
+
if (node.__typename === "Product") {
|
|
2804
|
+
item.description = node.description;
|
|
2805
|
+
item.image = node.featuredImage ? {
|
|
2806
|
+
url: node.featuredImage.url,
|
|
2807
|
+
altText: node.featuredImage.altText
|
|
2808
|
+
} : void 0;
|
|
2809
|
+
} else if (node.__typename === "Article") {
|
|
2810
|
+
item.description = node.excerpt;
|
|
2811
|
+
item.image = node.image ? {
|
|
2812
|
+
url: node.image.url,
|
|
2813
|
+
altText: node.image.altText
|
|
2814
|
+
} : void 0;
|
|
2160
2815
|
}
|
|
2161
|
-
return
|
|
2162
|
-
}
|
|
2816
|
+
return item;
|
|
2817
|
+
}) || [];
|
|
2163
2818
|
return {
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
brand: shop.brand ? {
|
|
2168
|
-
logo: shop.brand.logo,
|
|
2169
|
-
colors: shop.brand.colors ? {
|
|
2170
|
-
primary: shop.brand.colors.primary?.background,
|
|
2171
|
-
secondary: shop.brand.colors.secondary?.background
|
|
2172
|
-
} : void 0
|
|
2173
|
-
} : void 0,
|
|
2174
|
-
metafields
|
|
2819
|
+
items,
|
|
2820
|
+
totalCount: data.search.totalCount || 0,
|
|
2821
|
+
pageInfo: data.search.pageInfo
|
|
2175
2822
|
};
|
|
2176
2823
|
}
|
|
2177
|
-
function
|
|
2824
|
+
function useSearch(options = {}) {
|
|
2178
2825
|
const { client, locale } = useShopify();
|
|
2179
|
-
const {
|
|
2180
|
-
return useSWR(
|
|
2181
|
-
["
|
|
2182
|
-
() =>
|
|
2183
|
-
swrOptions
|
|
2184
|
-
);
|
|
2185
|
-
}
|
|
2186
|
-
|
|
2187
|
-
// src/hooks/member/plus/types.ts
|
|
2188
|
-
var PLUS_MEMBER_TYPE = /* @__PURE__ */ ((PLUS_MEMBER_TYPE2) => {
|
|
2189
|
-
PLUS_MEMBER_TYPE2[PLUS_MEMBER_TYPE2["FREE"] = 0] = "FREE";
|
|
2190
|
-
PLUS_MEMBER_TYPE2[PLUS_MEMBER_TYPE2["MONTHLY"] = 1] = "MONTHLY";
|
|
2191
|
-
PLUS_MEMBER_TYPE2[PLUS_MEMBER_TYPE2["ANNUAL"] = 2] = "ANNUAL";
|
|
2192
|
-
return PLUS_MEMBER_TYPE2;
|
|
2193
|
-
})(PLUS_MEMBER_TYPE || {});
|
|
2194
|
-
var PlusMemberMode = /* @__PURE__ */ ((PlusMemberMode2) => {
|
|
2195
|
-
PlusMemberMode2["MONTHLY"] = "monthly";
|
|
2196
|
-
PlusMemberMode2["ANNUAL"] = "annual";
|
|
2197
|
-
return PlusMemberMode2;
|
|
2198
|
-
})(PlusMemberMode || {});
|
|
2199
|
-
var DeliveryPlusType = /* @__PURE__ */ ((DeliveryPlusType2) => {
|
|
2200
|
-
DeliveryPlusType2["FREE"] = "free";
|
|
2201
|
-
DeliveryPlusType2["MONTHLY"] = "monthly";
|
|
2202
|
-
DeliveryPlusType2["ANNUAL"] = "annual";
|
|
2203
|
-
return DeliveryPlusType2;
|
|
2204
|
-
})(DeliveryPlusType || {});
|
|
2205
|
-
var ShippingMethodMode = /* @__PURE__ */ ((ShippingMethodMode2) => {
|
|
2206
|
-
ShippingMethodMode2["FREE"] = "free";
|
|
2207
|
-
ShippingMethodMode2["TDD"] = "tdd";
|
|
2208
|
-
ShippingMethodMode2["NDD"] = "ndd";
|
|
2209
|
-
return ShippingMethodMode2;
|
|
2210
|
-
})(ShippingMethodMode || {});
|
|
2211
|
-
var createInitialValue = () => ({
|
|
2212
|
-
zipCode: "",
|
|
2213
|
-
plusMemberMetafields: {},
|
|
2214
|
-
setZipCode: () => {
|
|
2215
|
-
},
|
|
2216
|
-
allowNextDayDelivery: false,
|
|
2217
|
-
setAllowNextDayDelivery: () => {
|
|
2218
|
-
},
|
|
2219
|
-
allowThirdDayDelivery: false,
|
|
2220
|
-
setAllowThirdDayDelivery: () => {
|
|
2221
|
-
},
|
|
2222
|
-
selectedPlusMemberMode: "free",
|
|
2223
|
-
setSelectedPlusMemberMode: () => {
|
|
2224
|
-
},
|
|
2225
|
-
showAreaCheckModal: false,
|
|
2226
|
-
setShowAreaCheckModal: () => {
|
|
2227
|
-
},
|
|
2228
|
-
selectedShippingMethod: void 0,
|
|
2229
|
-
setSelectedShippingMethod: () => {
|
|
2230
|
-
},
|
|
2231
|
-
showTip: false,
|
|
2232
|
-
setShowTip: () => {
|
|
2233
|
-
},
|
|
2234
|
-
showMoreShippingMethod: false,
|
|
2235
|
-
setShowMoreShippingMethod: () => {
|
|
2236
|
-
},
|
|
2237
|
-
variant: {},
|
|
2238
|
-
product: {},
|
|
2239
|
-
shippingMethodsContext: {
|
|
2240
|
-
freeShippingMethods: [],
|
|
2241
|
-
paymentShippingMethods: [],
|
|
2242
|
-
nddOverweight: false,
|
|
2243
|
-
tddOverweight: false
|
|
2244
|
-
},
|
|
2245
|
-
selectedPlusMemberProduct: null,
|
|
2246
|
-
plusMemberProducts: [],
|
|
2247
|
-
showPlusMemberBenefit: false,
|
|
2248
|
-
setShowPlusMemberBenefit: () => {
|
|
2249
|
-
},
|
|
2250
|
-
deleteMarginBottom: false,
|
|
2251
|
-
setDeleteMarginBottom: () => {
|
|
2252
|
-
},
|
|
2253
|
-
profile: void 0,
|
|
2254
|
-
locale: void 0
|
|
2255
|
-
});
|
|
2256
|
-
var PlusMemberContext = createContext(createInitialValue());
|
|
2257
|
-
function usePlusMemberContext() {
|
|
2258
|
-
return useContext(PlusMemberContext);
|
|
2259
|
-
}
|
|
2260
|
-
function usePlusMonthlyProductVariant() {
|
|
2261
|
-
const { plusMemberProducts, plusMemberMetafields } = usePlusMemberContext();
|
|
2262
|
-
const plusMonthly = plusMemberMetafields?.plus_monthly_product;
|
|
2263
|
-
const plusMonthlyProductVariant = useMemo(() => {
|
|
2264
|
-
const product = plusMemberProducts?.find(
|
|
2265
|
-
(item) => item?.handle === plusMonthly?.handle
|
|
2266
|
-
);
|
|
2267
|
-
const productVariant = product?.variants?.find(
|
|
2268
|
-
(item) => item.sku === plusMonthly?.sku
|
|
2269
|
-
);
|
|
2270
|
-
return productVariant;
|
|
2271
|
-
}, [plusMemberProducts, plusMonthly]);
|
|
2272
|
-
return plusMonthlyProductVariant;
|
|
2273
|
-
}
|
|
2274
|
-
function usePlusAnnualProductVariant() {
|
|
2275
|
-
const { plusMemberProducts, plusMemberMetafields } = usePlusMemberContext();
|
|
2276
|
-
const plusAnnual = plusMemberMetafields?.plus_annual_product;
|
|
2277
|
-
const plusAnnualProductVariant = useMemo(() => {
|
|
2278
|
-
const product = plusMemberProducts?.find(
|
|
2279
|
-
(item) => item?.handle === plusAnnual?.handle
|
|
2280
|
-
);
|
|
2281
|
-
const productVariant = product?.variants?.find(
|
|
2282
|
-
(item) => item.sku === plusAnnual?.sku
|
|
2283
|
-
);
|
|
2284
|
-
return productVariant;
|
|
2285
|
-
}, [plusMemberProducts, plusAnnual]);
|
|
2286
|
-
return plusAnnualProductVariant;
|
|
2287
|
-
}
|
|
2288
|
-
function useShippingMethods(options) {
|
|
2289
|
-
const {
|
|
2290
|
-
variant,
|
|
2291
|
-
plusMemberMetafields,
|
|
2292
|
-
selectedPlusMemberMode,
|
|
2293
|
-
isPlus = false,
|
|
2294
|
-
nddCoupon,
|
|
2295
|
-
tddCoupon
|
|
2296
|
-
} = options;
|
|
2297
|
-
const { plus_shipping, shippingMethod } = plusMemberMetafields || {};
|
|
2298
|
-
const nddOverweight = useMemo(() => {
|
|
2299
|
-
return (variant?.weight || 0) > (shippingMethod?.overWeight_ndd || Infinity);
|
|
2300
|
-
}, [shippingMethod?.overWeight_ndd, variant?.weight]);
|
|
2301
|
-
const tddOverweight = useMemo(() => {
|
|
2302
|
-
return (variant?.weight || 0) > (shippingMethod?.overWeight_tdd || Infinity);
|
|
2303
|
-
}, [shippingMethod?.overWeight_tdd, variant?.weight]);
|
|
2304
|
-
const paymentShippingMethods = useMemo(() => {
|
|
2305
|
-
const weight = variant?.weight || 0;
|
|
2306
|
-
const methods = plus_shipping?.shipping_methods?.filter(
|
|
2307
|
-
({ weight_low, weight_high, __mode, __plus }) => {
|
|
2308
|
-
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
2309
|
-
return __mode !== "free" /* FREE */ && !__plus && fitWeight;
|
|
2310
|
-
}
|
|
2311
|
-
) || [];
|
|
2312
|
-
return methods.map((method) => {
|
|
2313
|
-
let disabled = false;
|
|
2314
|
-
const selectedFreeMember = selectedPlusMemberMode === "free";
|
|
2315
|
-
if (method.__mode === "ndd" /* NDD */) {
|
|
2316
|
-
disabled = selectedFreeMember || nddOverweight;
|
|
2317
|
-
} else if (method.__mode === "tdd" /* TDD */) {
|
|
2318
|
-
disabled = selectedFreeMember || tddOverweight;
|
|
2319
|
-
}
|
|
2320
|
-
return {
|
|
2321
|
-
...method,
|
|
2322
|
-
id: method.__mode + method.__code,
|
|
2323
|
-
useCoupon: false,
|
|
2324
|
-
subtitle: plus_shipping?.directly || "",
|
|
2325
|
-
disabled
|
|
2326
|
-
};
|
|
2327
|
-
});
|
|
2328
|
-
}, [
|
|
2329
|
-
nddOverweight,
|
|
2330
|
-
plus_shipping?.directly,
|
|
2331
|
-
plus_shipping?.shipping_methods,
|
|
2332
|
-
selectedPlusMemberMode,
|
|
2333
|
-
tddOverweight,
|
|
2334
|
-
variant?.weight
|
|
2335
|
-
]);
|
|
2336
|
-
const nddPrice = useMemo(() => {
|
|
2337
|
-
const weight = variant?.weight || 0;
|
|
2338
|
-
const nddMethod = paymentShippingMethods.find(
|
|
2339
|
-
({ __mode, weight_high, weight_low }) => {
|
|
2340
|
-
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
2341
|
-
return __mode === "ndd" && fitWeight;
|
|
2342
|
-
}
|
|
2343
|
-
);
|
|
2344
|
-
return nddMethod?.price || 0;
|
|
2345
|
-
}, [variant?.weight, paymentShippingMethods]);
|
|
2346
|
-
const tddPrice = useMemo(() => {
|
|
2347
|
-
const weight = variant?.weight || 0;
|
|
2348
|
-
const tddMethod = paymentShippingMethods.find(
|
|
2349
|
-
({ __mode, weight_high, weight_low }) => {
|
|
2350
|
-
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
2351
|
-
return __mode === "tdd" && fitWeight;
|
|
2352
|
-
}
|
|
2353
|
-
);
|
|
2354
|
-
return tddMethod?.price || 0;
|
|
2355
|
-
}, [variant?.weight, paymentShippingMethods]);
|
|
2356
|
-
const freeShippingMethods = useMemo(() => {
|
|
2357
|
-
const weight = variant?.weight || 0;
|
|
2358
|
-
let methods = plus_shipping?.shipping_methods?.filter(
|
|
2359
|
-
({ __mode, __plus, weight_low, weight_high }) => {
|
|
2360
|
-
if (__mode === "free" /* FREE */) {
|
|
2361
|
-
return true;
|
|
2362
|
-
}
|
|
2363
|
-
if (isPlus) {
|
|
2364
|
-
const hasCoupon = isPlus && __mode === "ndd" /* NDD */ && nddCoupon || isPlus && __mode === "tdd" /* TDD */ && (tddCoupon || nddCoupon);
|
|
2365
|
-
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
2366
|
-
return hasCoupon && fitWeight && !__plus;
|
|
2367
|
-
} else {
|
|
2368
|
-
return __plus;
|
|
2369
|
-
}
|
|
2370
|
-
}
|
|
2371
|
-
) || [];
|
|
2372
|
-
if (isPlus) {
|
|
2373
|
-
methods = methods.sort((a, b) => {
|
|
2374
|
-
if (b.__mode === "free" /* FREE */) return -1;
|
|
2375
|
-
return 0;
|
|
2376
|
-
});
|
|
2377
|
-
}
|
|
2378
|
-
return methods.map((method) => {
|
|
2379
|
-
let price = 0;
|
|
2380
|
-
let coupon;
|
|
2381
|
-
let disabled;
|
|
2382
|
-
if (method.__mode !== "free" /* FREE */) {
|
|
2383
|
-
switch (method.__mode) {
|
|
2384
|
-
case "tdd":
|
|
2385
|
-
price = tddPrice;
|
|
2386
|
-
coupon = tddCoupon || nddCoupon;
|
|
2387
|
-
break;
|
|
2388
|
-
case "ndd":
|
|
2389
|
-
price = nddPrice;
|
|
2390
|
-
coupon = nddCoupon;
|
|
2391
|
-
break;
|
|
2392
|
-
}
|
|
2393
|
-
disabled = selectedPlusMemberMode === "free";
|
|
2394
|
-
if (method.__mode === "ndd" /* NDD */) {
|
|
2395
|
-
disabled = disabled || nddOverweight;
|
|
2396
|
-
} else if (method.__mode === "tdd" /* TDD */) {
|
|
2397
|
-
disabled = disabled || tddOverweight;
|
|
2398
|
-
}
|
|
2399
|
-
}
|
|
2400
|
-
return {
|
|
2401
|
-
...method,
|
|
2402
|
-
id: method.__mode + method.__code,
|
|
2403
|
-
useCoupon: true,
|
|
2404
|
-
disabled,
|
|
2405
|
-
coupon,
|
|
2406
|
-
price
|
|
2407
|
-
};
|
|
2408
|
-
});
|
|
2409
|
-
}, [
|
|
2410
|
-
variant?.weight,
|
|
2411
|
-
plus_shipping?.shipping_methods,
|
|
2412
|
-
isPlus,
|
|
2413
|
-
nddCoupon,
|
|
2414
|
-
tddCoupon,
|
|
2415
|
-
selectedPlusMemberMode,
|
|
2416
|
-
tddPrice,
|
|
2417
|
-
nddPrice,
|
|
2418
|
-
nddOverweight,
|
|
2419
|
-
tddOverweight
|
|
2420
|
-
]);
|
|
2421
|
-
return {
|
|
2422
|
-
freeShippingMethods,
|
|
2423
|
-
paymentShippingMethods,
|
|
2424
|
-
nddOverweight,
|
|
2425
|
-
tddOverweight
|
|
2426
|
-
};
|
|
2826
|
+
const { query, first = 20, types = ["PRODUCT", "ARTICLE", "PAGE"], ...swrOptions } = options;
|
|
2827
|
+
return useSWR(
|
|
2828
|
+
query ? ["search", locale, query, first, types] : null,
|
|
2829
|
+
() => performSearch(client, locale, query, first, types),
|
|
2830
|
+
swrOptions
|
|
2831
|
+
);
|
|
2427
2832
|
}
|
|
2428
|
-
function
|
|
2429
|
-
const
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
if (!allowNextDayDelivery && !allowThirdDayDelivery) {
|
|
2456
|
-
setShowTip(true);
|
|
2457
|
-
setSelectedShippingMethod(standardShippingMethod);
|
|
2458
|
-
} else {
|
|
2459
|
-
if (selectedShippingMethod?.__mode === "ndd" /* NDD */ && !allowNextDayDelivery) {
|
|
2460
|
-
setShowTip(true);
|
|
2461
|
-
if (allowThirdDayDelivery) {
|
|
2462
|
-
if (selectedShippingMethod.useCoupon) {
|
|
2463
|
-
const method = freeTDD || freeShippingMethod;
|
|
2464
|
-
if (method) setSelectedShippingMethod(method);
|
|
2465
|
-
} else {
|
|
2466
|
-
const method = paymentTDD || freeShippingMethod;
|
|
2467
|
-
if (method) setSelectedShippingMethod(method);
|
|
2833
|
+
async function getSiteInfo(client, locale, metafieldIdentifiers) {
|
|
2834
|
+
const hasMetafields = metafieldIdentifiers && metafieldIdentifiers.length > 0;
|
|
2835
|
+
const query = (
|
|
2836
|
+
/* GraphQL */
|
|
2837
|
+
`
|
|
2838
|
+
query getSiteInfo(
|
|
2839
|
+
${hasMetafields ? "$shopMetafieldIdentifiers: [HasMetafieldsIdentifier!]!" : ""}
|
|
2840
|
+
) @inContext(language: $language) {
|
|
2841
|
+
shop {
|
|
2842
|
+
name
|
|
2843
|
+
description
|
|
2844
|
+
primaryDomain {
|
|
2845
|
+
url
|
|
2846
|
+
host
|
|
2847
|
+
}
|
|
2848
|
+
brand {
|
|
2849
|
+
logo {
|
|
2850
|
+
image {
|
|
2851
|
+
url
|
|
2852
|
+
}
|
|
2853
|
+
}
|
|
2854
|
+
colors {
|
|
2855
|
+
primary {
|
|
2856
|
+
background
|
|
2857
|
+
}
|
|
2858
|
+
secondary {
|
|
2859
|
+
background
|
|
2468
2860
|
}
|
|
2469
|
-
} else {
|
|
2470
|
-
if (freeShippingMethod) setSelectedShippingMethod(freeShippingMethod);
|
|
2471
2861
|
}
|
|
2472
|
-
} else if (
|
|
2473
|
-
// TDD 无法使用
|
|
2474
|
-
selectedShippingMethod?.__mode === "tdd" /* TDD */ && !allowThirdDayDelivery
|
|
2475
|
-
) {
|
|
2476
|
-
setShowTip(true);
|
|
2477
|
-
if (freeShippingMethod) setSelectedShippingMethod(freeShippingMethod);
|
|
2478
2862
|
}
|
|
2863
|
+
${hasMetafields ? "metafields(identifiers: $shopMetafieldIdentifiers) { key value }" : ""}
|
|
2479
2864
|
}
|
|
2480
2865
|
}
|
|
2481
|
-
|
|
2482
|
-
allowNextDayDelivery,
|
|
2483
|
-
allowThirdDayDelivery,
|
|
2484
|
-
zipCode,
|
|
2485
|
-
shippingMethodsContext,
|
|
2486
|
-
selectedShippingMethod,
|
|
2487
|
-
setSelectedShippingMethod,
|
|
2488
|
-
setShowTip
|
|
2489
|
-
]);
|
|
2490
|
-
}
|
|
2491
|
-
var useReplaceCartPlusMember = () => {
|
|
2492
|
-
const { plusMemberMetafields, selectedPlusMemberMode } = usePlusMemberContext();
|
|
2493
|
-
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
2494
|
-
const { cart } = useCartContext();
|
|
2495
|
-
const plusMonthly = plusMemberMetafields?.plus_monthly_product;
|
|
2496
|
-
const plusAnnual = plusMemberMetafields?.plus_annual_product;
|
|
2497
|
-
const handler = useCallback(async () => {
|
|
2498
|
-
const plusMonthlyInCart = cart?.lineItems.find(
|
|
2499
|
-
(item) => item.variant?.sku === plusMonthly?.sku
|
|
2500
|
-
);
|
|
2501
|
-
const plusAnnualInCart = cart?.lineItems.find(
|
|
2502
|
-
(item) => item.variant?.sku === plusAnnual?.sku
|
|
2503
|
-
);
|
|
2504
|
-
if (selectedPlusMemberMode === "annual" /* ANNUAL */ && plusMonthlyInCart) {
|
|
2505
|
-
await removeCartLines2({
|
|
2506
|
-
lineIds: [plusMonthlyInCart.id]
|
|
2507
|
-
});
|
|
2508
|
-
} else if (selectedPlusMemberMode === "monthly" /* MONTHLY */ && plusAnnualInCart) {
|
|
2509
|
-
await removeCartLines2({
|
|
2510
|
-
lineIds: [plusAnnualInCart.id]
|
|
2511
|
-
});
|
|
2512
|
-
}
|
|
2513
|
-
}, [
|
|
2514
|
-
cart?.lineItems,
|
|
2515
|
-
selectedPlusMemberMode,
|
|
2516
|
-
plusMonthly?.sku,
|
|
2517
|
-
plusAnnual?.sku,
|
|
2518
|
-
removeCartLines2
|
|
2519
|
-
]);
|
|
2520
|
-
return handler;
|
|
2521
|
-
};
|
|
2522
|
-
var usePlusMemberDeliveryCodes = ({
|
|
2523
|
-
deliveryData
|
|
2524
|
-
}) => {
|
|
2525
|
-
return useMemo(
|
|
2526
|
-
() => deliveryData?.deliveryCustomData?.discount_code,
|
|
2527
|
-
[deliveryData]
|
|
2866
|
+
`
|
|
2528
2867
|
);
|
|
2529
|
-
};
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
}
|
|
2533
|
-
const
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
}
|
|
2542
|
-
return itemCustomAttributes;
|
|
2543
|
-
}, [deliveryCustomData]);
|
|
2544
|
-
};
|
|
2545
|
-
var usePlusMemberCheckoutCustomAttributes = ({
|
|
2546
|
-
deliveryData,
|
|
2547
|
-
product,
|
|
2548
|
-
variant,
|
|
2549
|
-
customer,
|
|
2550
|
-
isShowShippingBenefits
|
|
2551
|
-
}) => {
|
|
2552
|
-
const { deliveryCustomData } = deliveryData || {};
|
|
2553
|
-
const { profile } = usePlusMemberContext();
|
|
2554
|
-
const userType = useMemo(() => {
|
|
2555
|
-
const customerInfo = customer;
|
|
2556
|
-
if (!customerInfo) {
|
|
2557
|
-
return "new_user_unlogin";
|
|
2558
|
-
}
|
|
2559
|
-
if (customer) {
|
|
2560
|
-
const { orders = {} } = customer;
|
|
2561
|
-
const edgesLength = orders?.edges?.length;
|
|
2562
|
-
if (edgesLength === 1) {
|
|
2563
|
-
return "old_user_orders_once";
|
|
2564
|
-
} else if (edgesLength && edgesLength > 1) {
|
|
2565
|
-
return "old_user_orders_twice";
|
|
2566
|
-
}
|
|
2567
|
-
}
|
|
2568
|
-
return "new_user_login";
|
|
2569
|
-
}, [customer]);
|
|
2570
|
-
return useMemo(() => {
|
|
2571
|
-
const checkoutCustomAttributes = [
|
|
2572
|
-
{
|
|
2573
|
-
key: "_token",
|
|
2574
|
-
value: profile?.token || ""
|
|
2575
|
-
},
|
|
2576
|
-
{
|
|
2577
|
-
key: "_last_url",
|
|
2578
|
-
value: typeof window !== "undefined" ? window.location.origin + window.location.pathname : ""
|
|
2579
|
-
},
|
|
2580
|
-
{
|
|
2581
|
-
key: "_user_type",
|
|
2582
|
-
value: userType
|
|
2583
|
-
}
|
|
2584
|
-
];
|
|
2585
|
-
if (profile) {
|
|
2586
|
-
checkoutCustomAttributes.push({
|
|
2587
|
-
key: "_login_user",
|
|
2588
|
-
value: "1"
|
|
2589
|
-
});
|
|
2590
|
-
}
|
|
2591
|
-
if (deliveryCustomData) {
|
|
2592
|
-
checkoutCustomAttributes.push({
|
|
2593
|
-
key: "_checkout_delivery_custom",
|
|
2594
|
-
value: JSON.stringify({
|
|
2595
|
-
...deliveryCustomData,
|
|
2596
|
-
is_prime: profile?.isPlus
|
|
2597
|
-
})
|
|
2598
|
-
});
|
|
2599
|
-
}
|
|
2600
|
-
if (variant?.metafields?.presell) {
|
|
2601
|
-
checkoutCustomAttributes.push({
|
|
2602
|
-
key: "_presale",
|
|
2603
|
-
value: "true"
|
|
2604
|
-
});
|
|
2605
|
-
}
|
|
2606
|
-
if (isShowShippingBenefits && !isShowShippingBenefits({ variant, product, setting: {} })) {
|
|
2607
|
-
checkoutCustomAttributes.push({
|
|
2608
|
-
key: "_hide_shipping",
|
|
2609
|
-
value: "true"
|
|
2610
|
-
});
|
|
2611
|
-
}
|
|
2612
|
-
return checkoutCustomAttributes;
|
|
2613
|
-
}, [deliveryCustomData, product, profile, userType, variant, isShowShippingBenefits]);
|
|
2614
|
-
};
|
|
2615
|
-
function useAutoRemovePlusMemberInCart({
|
|
2616
|
-
cart,
|
|
2617
|
-
profile,
|
|
2618
|
-
memberSetting
|
|
2619
|
-
}) {
|
|
2620
|
-
const { plus_monthly_product, plus_annual_product } = memberSetting || {};
|
|
2621
|
-
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
2622
|
-
useEffect(() => {
|
|
2623
|
-
if (!cart || !plus_monthly_product || !plus_annual_product) return;
|
|
2624
|
-
const removePlusProduct = async (productType) => {
|
|
2625
|
-
if (!productType) return;
|
|
2626
|
-
const product = cart.lineItems?.find(
|
|
2627
|
-
(item) => item.product?.handle === productType?.handle && item.variant?.sku === productType?.sku
|
|
2628
|
-
);
|
|
2629
|
-
if (product) {
|
|
2630
|
-
await removeCartLines2({
|
|
2631
|
-
lineIds: [product.id]
|
|
2632
|
-
});
|
|
2633
|
-
}
|
|
2634
|
-
};
|
|
2635
|
-
if (profile?.isMonthlyPlus) {
|
|
2636
|
-
removePlusProduct(plus_monthly_product);
|
|
2637
|
-
}
|
|
2638
|
-
if (profile?.isAnnualPlus) {
|
|
2639
|
-
removePlusProduct(plus_annual_product);
|
|
2640
|
-
}
|
|
2641
|
-
}, [cart, plus_annual_product, plus_monthly_product, profile, removeCartLines2]);
|
|
2642
|
-
}
|
|
2643
|
-
function useAddPlusMemberProductsToCart({
|
|
2644
|
-
cart,
|
|
2645
|
-
profile
|
|
2646
|
-
}) {
|
|
2647
|
-
const { selectedPlusMemberMode, selectedPlusMemberProduct, plusMemberMetafields } = usePlusMemberContext();
|
|
2648
|
-
const { hasMonthlyPlus, hasAnnualPlus } = useHasPlusMemberInCart({
|
|
2649
|
-
memberSetting: plusMemberMetafields,
|
|
2650
|
-
cart
|
|
2651
|
-
});
|
|
2652
|
-
const plusMemberProduct = useMemo(() => {
|
|
2653
|
-
if (!selectedPlusMemberProduct || selectedPlusMemberMode === "free" /* FREE */) {
|
|
2654
|
-
return void 0;
|
|
2655
|
-
}
|
|
2656
|
-
if (selectedPlusMemberMode === "monthly" /* MONTHLY */ && hasMonthlyPlus) {
|
|
2657
|
-
return void 0;
|
|
2658
|
-
}
|
|
2659
|
-
if (selectedPlusMemberMode === "annual" /* ANNUAL */ && hasAnnualPlus) {
|
|
2660
|
-
return void 0;
|
|
2661
|
-
}
|
|
2662
|
-
if (profile.isMonthlyPlus && selectedPlusMemberMode === "monthly" /* MONTHLY */) {
|
|
2663
|
-
return void 0;
|
|
2664
|
-
}
|
|
2665
|
-
if (!profile.isAnnualPlus && selectedPlusMemberMode === "annual" /* ANNUAL */) {
|
|
2666
|
-
return void 0;
|
|
2868
|
+
const variables = {};
|
|
2869
|
+
if (hasMetafields) {
|
|
2870
|
+
variables.shopMetafieldIdentifiers = metafieldIdentifiers;
|
|
2871
|
+
}
|
|
2872
|
+
const data = await client.query(query, variables);
|
|
2873
|
+
if (!data || !data.shop) {
|
|
2874
|
+
return void 0;
|
|
2875
|
+
}
|
|
2876
|
+
const shop = data.shop;
|
|
2877
|
+
const metafields = shop.metafields?.reduce((acc, mf) => {
|
|
2878
|
+
if (mf && mf.key) {
|
|
2879
|
+
acc[mf.key] = mf.value;
|
|
2667
2880
|
}
|
|
2668
|
-
return
|
|
2669
|
-
},
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2881
|
+
return acc;
|
|
2882
|
+
}, {});
|
|
2883
|
+
return {
|
|
2884
|
+
name: shop.name,
|
|
2885
|
+
description: shop.description,
|
|
2886
|
+
primaryDomain: shop.primaryDomain,
|
|
2887
|
+
brand: shop.brand ? {
|
|
2888
|
+
logo: shop.brand.logo,
|
|
2889
|
+
colors: shop.brand.colors ? {
|
|
2890
|
+
primary: shop.brand.colors.primary?.background,
|
|
2891
|
+
secondary: shop.brand.colors.secondary?.background
|
|
2892
|
+
} : void 0
|
|
2893
|
+
} : void 0,
|
|
2894
|
+
metafields
|
|
2895
|
+
};
|
|
2677
2896
|
}
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
children
|
|
2686
|
-
}) => {
|
|
2687
|
-
const [zipCode, setZipCode] = useState("");
|
|
2688
|
-
const [showTip, setShowTip] = useState(false);
|
|
2689
|
-
const [selectedPlusMemberMode, setSelectedPlusMemberMode] = useState(
|
|
2690
|
-
initialSelectedPlusMemberMode
|
|
2691
|
-
);
|
|
2692
|
-
const [selectedShippingMethod, setSelectedShippingMethod] = useState();
|
|
2693
|
-
const [allowNextDayDelivery, setAllowNextDayDelivery] = useState(false);
|
|
2694
|
-
const [allowThirdDayDelivery, setAllowThirdDayDelivery] = useState(false);
|
|
2695
|
-
const [showAreaCheckModal, setShowAreaCheckModal] = useState(false);
|
|
2696
|
-
const [showMoreShippingMethod, setShowMoreShippingMethod] = useState(false);
|
|
2697
|
-
const [showPlusMemberBenefit, setShowPlusMemberBenefit] = useState(false);
|
|
2698
|
-
const [deleteMarginBottom, setDeleteMarginBottom] = useState(false);
|
|
2699
|
-
const shippingMethodsContext = useShippingMethods({
|
|
2700
|
-
variant,
|
|
2701
|
-
plusMemberMetafields: memberSetting,
|
|
2702
|
-
selectedPlusMemberMode});
|
|
2703
|
-
const plusMemberHandles = useMemo(() => {
|
|
2704
|
-
return [
|
|
2705
|
-
memberSetting?.plus_monthly_product?.handle,
|
|
2706
|
-
memberSetting?.plus_annual_product?.handle
|
|
2707
|
-
].filter(Boolean);
|
|
2708
|
-
}, [memberSetting]);
|
|
2709
|
-
const { data: plusMemberProducts = [] } = useProductsByHandles({
|
|
2710
|
-
handles: plusMemberHandles
|
|
2711
|
-
});
|
|
2712
|
-
const selectedPlusMemberProduct = useMemo(() => {
|
|
2713
|
-
if (selectedPlusMemberMode === "free" /* FREE */) {
|
|
2714
|
-
return null;
|
|
2715
|
-
}
|
|
2716
|
-
const handle = selectedPlusMemberMode === "monthly" /* MONTHLY */ ? memberSetting?.plus_monthly_product?.handle : memberSetting?.plus_annual_product?.handle;
|
|
2717
|
-
const sku = selectedPlusMemberMode === "monthly" /* MONTHLY */ ? memberSetting?.plus_monthly_product?.sku : memberSetting?.plus_annual_product?.sku;
|
|
2718
|
-
const product2 = plusMemberProducts?.find((p) => p.handle === handle);
|
|
2719
|
-
const variant2 = product2?.variants?.find((v) => v.sku === sku);
|
|
2720
|
-
return product2 && variant2 ? { product: product2, variant: variant2 } : null;
|
|
2721
|
-
}, [plusMemberProducts, memberSetting, selectedPlusMemberMode]);
|
|
2722
|
-
return /* @__PURE__ */ jsx(
|
|
2723
|
-
PlusMemberContext.Provider,
|
|
2724
|
-
{
|
|
2725
|
-
value: {
|
|
2726
|
-
variant,
|
|
2727
|
-
zipCode,
|
|
2728
|
-
setZipCode,
|
|
2729
|
-
allowNextDayDelivery,
|
|
2730
|
-
setAllowNextDayDelivery,
|
|
2731
|
-
allowThirdDayDelivery,
|
|
2732
|
-
setAllowThirdDayDelivery,
|
|
2733
|
-
plusMemberMetafields: memberSetting,
|
|
2734
|
-
selectedPlusMemberMode,
|
|
2735
|
-
setSelectedPlusMemberMode,
|
|
2736
|
-
showAreaCheckModal,
|
|
2737
|
-
setShowAreaCheckModal,
|
|
2738
|
-
selectedShippingMethod,
|
|
2739
|
-
setSelectedShippingMethod,
|
|
2740
|
-
shippingMethodsContext,
|
|
2741
|
-
showTip,
|
|
2742
|
-
setShowTip,
|
|
2743
|
-
showMoreShippingMethod,
|
|
2744
|
-
setShowMoreShippingMethod,
|
|
2745
|
-
selectedPlusMemberProduct,
|
|
2746
|
-
plusMemberProducts,
|
|
2747
|
-
product,
|
|
2748
|
-
showPlusMemberBenefit,
|
|
2749
|
-
setShowPlusMemberBenefit,
|
|
2750
|
-
deleteMarginBottom,
|
|
2751
|
-
setDeleteMarginBottom,
|
|
2752
|
-
profile,
|
|
2753
|
-
locale
|
|
2754
|
-
},
|
|
2755
|
-
children
|
|
2756
|
-
}
|
|
2897
|
+
function useSite(options = {}) {
|
|
2898
|
+
const { client, locale } = useShopify();
|
|
2899
|
+
const { metafieldIdentifiers, ...swrOptions } = options;
|
|
2900
|
+
return useSWR(
|
|
2901
|
+
["site", locale, metafieldIdentifiers],
|
|
2902
|
+
() => getSiteInfo(client, locale, metafieldIdentifiers),
|
|
2903
|
+
swrOptions
|
|
2757
2904
|
);
|
|
2758
|
-
}
|
|
2905
|
+
}
|
|
2759
2906
|
function useIntersection(targetRef, options) {
|
|
2760
2907
|
const {
|
|
2761
2908
|
callback,
|
|
@@ -2936,6 +3083,6 @@ function clearGeoLocationCache(cacheKey = "geoLocation") {
|
|
|
2936
3083
|
}
|
|
2937
3084
|
}
|
|
2938
3085
|
|
|
2939
|
-
export { BuyRuleType, CODE_AMOUNT_KEY, CUSTOMER_ATTRIBUTE_KEY, CUSTOMER_SCRIPT_GIFT_KEY, DeliveryPlusType, MAIN_PRODUCT_CODE, OrderBasePriceType, OrderDiscountType, PLUS_MEMBER_TYPE, PlusMemberContext, PlusMemberMode, PlusMemberProvider, PriceBasePriceType, PriceDiscountType, RuleType, SCRIPT_CODE_AMOUNT_KEY, ShippingMethodMode, SpendMoneyType,
|
|
3086
|
+
export { BuyRuleType, CODE_AMOUNT_KEY, CUSTOMER_ATTRIBUTE_KEY, CUSTOMER_SCRIPT_GIFT_KEY, DeliveryPlusType, MAIN_PRODUCT_CODE, MEMBER_PRICE_ATTRIBUTE_KEY, OrderBasePriceType, OrderDiscountType, PLUS_MEMBER_TYPE, PlusMemberContext, PlusMemberMode, PlusMemberProvider, PriceBasePriceType, PriceDiscountType, RuleType, SCRIPT_CODE_AMOUNT_KEY, ShippingMethodMode, SpendMoneyType, clearGeoLocationCache, createMockCartFromLines, currencyCodeMapping, defaultSWRMutationConfiguration, formatFunctionAutoFreeGift, formatScriptAutoFreeGift, getCachedGeoLocation, getCartAttributes, getDiscountEnvAttributeValue, getMatchedMainProductSubTotal, getQuery, getReferralAttributes, getUserType, hasPlusMemberInCart, hasPlusMemberInLines, normalizeAddToCartLines, preCheck, safeParse, useAddCartLines, useAddToCart, useAllBlogs, useAllCollections, useAllProducts, useApplyCartCodes, useArticle, useArticles, useArticlesInBlog, useAutoRemovePlusMemberInCart, useAvailableDeliveryCoupon, useBlog, useBuyNow, useCalcAutoFreeGift, useCalcGiftsFromLines, useCalcOrderDiscount, useCartAttributes, useCartItemQuantityLimit, useCollection, useCollections, useCreateCart, useExposure, useGeoLocation, useHasPlusMemberInCart, useHasPlusMemberInLines, useIntersection, usePlusMemberCheckoutCustomAttributes, usePlusMemberContext, usePlusMemberNeedAddToCart, usePlusMemberVariants, usePrice, useProduct, useProductUrl, useProductsByHandles, useRemoveCartCodes, useRemoveCartLines, useReplaceCartPlusMember, useScriptAutoFreeGift, useSearch, useSelectedOptions, useShippingMethods, useSite, useUpdateCartAttributes, useUpdateCartLines, useUpdateLineCodeAmountAttributes, useUpdateVariantQuery, useVariant, useVariantMedia };
|
|
2940
3087
|
//# sourceMappingURL=index.mjs.map
|
|
2941
3088
|
//# sourceMappingURL=index.mjs.map
|