@anker-in/shopify-react 0.1.1-beta.4 → 0.1.1-beta.41
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 +1953 -9
- package/dist/hooks/index.d.ts +1953 -9
- package/dist/hooks/index.js +582 -297
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +579 -295
- 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 +625 -327
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +616 -302
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +23 -13
- package/dist/provider/index.d.ts +23 -13
- package/dist/provider/index.js +228 -132
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +228 -132
- package/dist/provider/index.mjs.map +1 -1
- 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/dist/{types-CICUnw0v.d.mts → types-y1Wa95P3.d.mts} +10 -51
- package/dist/{types-CICUnw0v.d.ts → types-y1Wa95P3.d.ts} +10 -51
- package/package.json +3 -3
- package/dist/index-Utuz9i5x.d.mts +0 -1966
- package/dist/index-aSsTcW2O.d.ts +0 -1966
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { createContext, useMemo, useContext, useRef, useState, useEffect, useCallback } from 'react';
|
|
2
|
-
import { createShopifyClient, getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, updateCartAttributes, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog,
|
|
3
|
-
export
|
|
2
|
+
import { createShopifyClient, getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, updateCartAttributes, getLocalStorage, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, getCart, setLocalStorage, updateCartDeliveryOptions } from '@anker-in/shopify-sdk';
|
|
3
|
+
export * 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 useSWRMutation from 'swr/mutation';
|
|
9
10
|
import { useRequest } from 'ahooks';
|
|
@@ -45,6 +46,20 @@ var browserCartCookieAdapter = {
|
|
|
45
46
|
Cookies5.remove(getCartCookieName(locale));
|
|
46
47
|
}
|
|
47
48
|
};
|
|
49
|
+
|
|
50
|
+
// src/adapters/browser-performance.ts
|
|
51
|
+
var BrowserPerformanceAdapter = class {
|
|
52
|
+
/**
|
|
53
|
+
* Start tracking a performance event
|
|
54
|
+
*/
|
|
55
|
+
addToCartStart() {
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* End tracking a performance event
|
|
59
|
+
*/
|
|
60
|
+
addToCartEnd() {
|
|
61
|
+
}
|
|
62
|
+
};
|
|
48
63
|
function ShopifyProvider({
|
|
49
64
|
config,
|
|
50
65
|
locale,
|
|
@@ -53,7 +68,8 @@ function ShopifyProvider({
|
|
|
53
68
|
cartCookieAdapter = browserCartCookieAdapter,
|
|
54
69
|
routerAdapter,
|
|
55
70
|
userAdapter,
|
|
56
|
-
children
|
|
71
|
+
children,
|
|
72
|
+
performanceAdapter
|
|
57
73
|
}) {
|
|
58
74
|
const client = useMemo(() => {
|
|
59
75
|
return createShopifyClient(config, locale);
|
|
@@ -67,7 +83,8 @@ function ShopifyProvider({
|
|
|
67
83
|
cookieAdapter,
|
|
68
84
|
cartCookieAdapter,
|
|
69
85
|
routerAdapter,
|
|
70
|
-
userAdapter
|
|
86
|
+
userAdapter,
|
|
87
|
+
performanceAdapter
|
|
71
88
|
};
|
|
72
89
|
}, [
|
|
73
90
|
client,
|
|
@@ -77,6 +94,7 @@ function ShopifyProvider({
|
|
|
77
94
|
cookieAdapter,
|
|
78
95
|
cartCookieAdapter,
|
|
79
96
|
routerAdapter,
|
|
97
|
+
performanceAdapter,
|
|
80
98
|
userAdapter
|
|
81
99
|
]);
|
|
82
100
|
return /* @__PURE__ */ jsx(ShopifyContext.Provider, { value, children });
|
|
@@ -132,6 +150,7 @@ var CUSTOMER_ATTRIBUTE_KEY = "_discounts_function_env";
|
|
|
132
150
|
var CUSTOMER_SCRIPT_GIFT_KEY = "_giveaway_gradient_gifts";
|
|
133
151
|
var CODE_AMOUNT_KEY = "_sku_code_money";
|
|
134
152
|
var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
|
|
153
|
+
var MEMBER_PRICE_ATTRIBUTE_KEY = "_member_price";
|
|
135
154
|
var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
|
|
136
155
|
|
|
137
156
|
// src/hooks/cart/utils/normalize-add-to-cart-lines.ts
|
|
@@ -140,9 +159,10 @@ function normalizeAddToCartLines(lines) {
|
|
|
140
159
|
const variant = line.variant;
|
|
141
160
|
const product = variant.product;
|
|
142
161
|
const quantity = line.quantity || 1;
|
|
143
|
-
const
|
|
144
|
-
const
|
|
145
|
-
const
|
|
162
|
+
const originalPrice = variant.price?.amount ? Number(variant.price.amount) : 0;
|
|
163
|
+
const finalPrice = variant.finalPrice?.amount === void 0 ? originalPrice : Number(variant.finalPrice?.amount);
|
|
164
|
+
const subtotalAmount = originalPrice * quantity;
|
|
165
|
+
const totalAmount = finalPrice * quantity;
|
|
146
166
|
return {
|
|
147
167
|
id: `temp-line-${index}-${variant.id}`,
|
|
148
168
|
// Temporary ID for pre-cart lines
|
|
@@ -156,7 +176,7 @@ function normalizeAddToCartLines(lines) {
|
|
|
156
176
|
customAttributes: line.attributes || [],
|
|
157
177
|
variant: {
|
|
158
178
|
id: variant.id,
|
|
159
|
-
price,
|
|
179
|
+
price: finalPrice,
|
|
160
180
|
listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
|
|
161
181
|
sku: variant.sku || "",
|
|
162
182
|
name: variant.title || "",
|
|
@@ -187,15 +207,16 @@ function createMockCartFromLines(lines, existingCart) {
|
|
|
187
207
|
const normalizedLines = normalizeAddToCartLines(lines);
|
|
188
208
|
const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
|
|
189
209
|
const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
|
|
210
|
+
const currency = lines[0]?.variant?.price?.currencyCode;
|
|
190
211
|
return {
|
|
191
212
|
id: existingCart?.id || "temp-cart-id",
|
|
192
213
|
customerId: existingCart?.customerId,
|
|
193
214
|
email: existingCart?.email,
|
|
194
215
|
createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
195
|
-
currency: existingCart?.currency || { code:
|
|
216
|
+
currency: existingCart?.currency || { code: currency },
|
|
196
217
|
taxesIncluded: existingCart?.taxesIncluded,
|
|
197
218
|
lineItems: normalizedLines,
|
|
198
|
-
|
|
219
|
+
totalLineItemsDiscount: 0,
|
|
199
220
|
orderDiscounts: 0,
|
|
200
221
|
lineItemsSubtotalPrice: subtotalPrice,
|
|
201
222
|
subtotalPrice,
|
|
@@ -226,16 +247,6 @@ var getQuery = () => {
|
|
|
226
247
|
}
|
|
227
248
|
return theRequest;
|
|
228
249
|
};
|
|
229
|
-
function atobID(id) {
|
|
230
|
-
if (id && typeof id === "string" && id.includes("/")) {
|
|
231
|
-
return id.split("/").pop()?.split("?")?.shift();
|
|
232
|
-
} else {
|
|
233
|
-
return id;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
function btoaID(id, type = "ProductVariant") {
|
|
237
|
-
return `gid://shopify/${type}/${id}`;
|
|
238
|
-
}
|
|
239
250
|
var getMatchedMainProductSubTotal = (cartData, variant_list, main_product) => {
|
|
240
251
|
const isAllStoreVariant = main_product?.all_store_variant ?? false;
|
|
241
252
|
const matchedList = cartData?.lineItems?.filter((line) => {
|
|
@@ -467,43 +478,29 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
467
478
|
}
|
|
468
479
|
return { activeCampaign: null, subtotal: 0 };
|
|
469
480
|
}, [autoFreeGiftConfig, effectiveCart, tags, dealsType]);
|
|
470
|
-
const {
|
|
481
|
+
const { qualifyingTier, nextTierGoal, actualThreshold, currentCurrency } = useMemo(() => {
|
|
471
482
|
if (!activeCampaign || !activeCampaign.rule_result?.spend_get_reward?.gift_product) {
|
|
472
|
-
return {
|
|
483
|
+
return { qualifyingTier: null, nextTierGoal: null, actualThreshold: 0, currentCurrency: "" };
|
|
473
484
|
}
|
|
474
485
|
const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
tier
|
|
482
|
-
itemsToAdd: qualifyingTier.reward_list?.map((reward) => {
|
|
483
|
-
const giftProduct = reward?.variant_list?.[0];
|
|
484
|
-
if (!giftProduct) return null;
|
|
485
|
-
return {
|
|
486
|
-
variant: {
|
|
487
|
-
id: btoaID(giftProduct.variant_id),
|
|
488
|
-
handle: giftProduct.handle,
|
|
489
|
-
sku: giftProduct.sku
|
|
490
|
-
},
|
|
491
|
-
quantity: reward?.get_unit || 1,
|
|
492
|
-
attributes: [
|
|
493
|
-
{
|
|
494
|
-
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
495
|
-
value: JSON.stringify({
|
|
496
|
-
is_gift: true,
|
|
497
|
-
rule_id: activeCampaign.rule_id,
|
|
498
|
-
spend_sum_money: qualifyingTier.spend_sum_money
|
|
499
|
-
})
|
|
500
|
-
}
|
|
501
|
-
]
|
|
502
|
-
};
|
|
503
|
-
}).filter((item) => item !== null)
|
|
486
|
+
const currentCurrency2 = effectiveCart?.currency?.code || "";
|
|
487
|
+
console.log("currentCurrency useCalcAutoFreeGift", effectiveCart, currentCurrency2);
|
|
488
|
+
const getThresholdAmount = (tier) => {
|
|
489
|
+
if (tier.spend_sum_money_multi_markets?.[currentCurrency2]?.value) {
|
|
490
|
+
return Number(tier.spend_sum_money_multi_markets[currentCurrency2].value);
|
|
491
|
+
}
|
|
492
|
+
return Number(tier.spend_sum_money || 0);
|
|
504
493
|
};
|
|
505
|
-
|
|
506
|
-
|
|
494
|
+
const qualifyingTier2 = [...giftTiers].sort((a, b) => getThresholdAmount(b) - getThresholdAmount(a)).find((tier) => subtotal >= getThresholdAmount(tier));
|
|
495
|
+
const nextGoal = giftTiers.find((tier) => subtotal < getThresholdAmount(tier));
|
|
496
|
+
const actualThreshold2 = qualifyingTier2 ? getThresholdAmount(qualifyingTier2) : 0;
|
|
497
|
+
return {
|
|
498
|
+
qualifyingTier: qualifyingTier2,
|
|
499
|
+
nextTierGoal: nextGoal || null,
|
|
500
|
+
actualThreshold: actualThreshold2,
|
|
501
|
+
currentCurrency: currentCurrency2
|
|
502
|
+
};
|
|
503
|
+
}, [activeCampaign, subtotal, effectiveCart]);
|
|
507
504
|
const giftHandles = useMemo(() => {
|
|
508
505
|
const giftVariant = autoFreeGiftConfig.map(
|
|
509
506
|
(item) => item.rule_result?.spend_get_reward?.gift_product?.map(
|
|
@@ -519,24 +516,82 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
519
516
|
}
|
|
520
517
|
return true;
|
|
521
518
|
}, [giftHandles]);
|
|
522
|
-
const { data: giftProductsResult } = useSWR(
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
519
|
+
const { data: giftProductsResult } = useSWR(
|
|
520
|
+
shouldFetch ? giftHandles : null,
|
|
521
|
+
async () => {
|
|
522
|
+
const res = await getProductsByHandles(client, {
|
|
523
|
+
handles: giftHandles,
|
|
524
|
+
locale
|
|
525
|
+
});
|
|
526
|
+
const result = Array.isArray(res) ? res : [];
|
|
527
|
+
giftProductsCache.current = {
|
|
528
|
+
data: result,
|
|
529
|
+
giftHandles: [...giftHandles]
|
|
530
|
+
};
|
|
531
|
+
return result;
|
|
532
|
+
},
|
|
533
|
+
{
|
|
534
|
+
revalidateOnFocus: false
|
|
535
|
+
}
|
|
536
|
+
);
|
|
534
537
|
const finalGiftProductsResult = useMemo(() => {
|
|
535
538
|
if (giftProductsCache.current && !shouldFetch) {
|
|
536
539
|
return giftProductsCache.current.data || void 0;
|
|
537
540
|
}
|
|
538
541
|
return giftProductsResult;
|
|
539
542
|
}, [giftProductsResult, shouldFetch]);
|
|
543
|
+
const qualifyingGift = useMemo(() => {
|
|
544
|
+
if (!qualifyingTier || !activeCampaign) {
|
|
545
|
+
return null;
|
|
546
|
+
}
|
|
547
|
+
const itemsToAdd = qualifyingTier.reward_list?.map((reward) => {
|
|
548
|
+
if (!reward.variant_list || reward.variant_list.length === 0) {
|
|
549
|
+
return null;
|
|
550
|
+
}
|
|
551
|
+
let selectedGiftProduct = null;
|
|
552
|
+
for (const giftVariant of reward.variant_list) {
|
|
553
|
+
const productInfo = finalGiftProductsResult?.find(
|
|
554
|
+
(p) => p.handle === giftVariant.handle
|
|
555
|
+
);
|
|
556
|
+
if (productInfo) {
|
|
557
|
+
const variantInfo = productInfo.variants?.find((v) => v.sku === giftVariant.sku);
|
|
558
|
+
if (variantInfo?.availableForSale) {
|
|
559
|
+
selectedGiftProduct = giftVariant;
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
if (!selectedGiftProduct) {
|
|
565
|
+
selectedGiftProduct = reward.variant_list[0];
|
|
566
|
+
}
|
|
567
|
+
return {
|
|
568
|
+
variant: {
|
|
569
|
+
id: btoaID(selectedGiftProduct.variant_id),
|
|
570
|
+
handle: selectedGiftProduct.handle,
|
|
571
|
+
sku: selectedGiftProduct.sku
|
|
572
|
+
},
|
|
573
|
+
quantity: reward?.get_unit || 1,
|
|
574
|
+
attributes: [
|
|
575
|
+
{
|
|
576
|
+
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
577
|
+
value: JSON.stringify({
|
|
578
|
+
is_gift: true,
|
|
579
|
+
rule_id: activeCampaign.rule_id,
|
|
580
|
+
spend_sum_money: actualThreshold,
|
|
581
|
+
// 使用实际的门槛金额(多币种支持)
|
|
582
|
+
currency_code: currentCurrency
|
|
583
|
+
// 记录当前币种
|
|
584
|
+
})
|
|
585
|
+
}
|
|
586
|
+
]
|
|
587
|
+
};
|
|
588
|
+
}).filter((item) => item !== null);
|
|
589
|
+
const formattedGift = {
|
|
590
|
+
tier: qualifyingTier,
|
|
591
|
+
itemsToAdd
|
|
592
|
+
};
|
|
593
|
+
return formattedGift;
|
|
594
|
+
}, [qualifyingTier, activeCampaign, finalGiftProductsResult, actualThreshold, currentCurrency]);
|
|
540
595
|
return {
|
|
541
596
|
qualifyingGift,
|
|
542
597
|
nextTierGoal,
|
|
@@ -583,12 +638,14 @@ var useScriptAutoFreeGift = ({
|
|
|
583
638
|
upgrade_multiple2 = 1.2;
|
|
584
639
|
upgrade_value2 = 40;
|
|
585
640
|
}
|
|
586
|
-
effectiveCart?.lineItems?.forEach(
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
641
|
+
effectiveCart?.lineItems?.forEach(
|
|
642
|
+
({ customAttributes }) => {
|
|
643
|
+
customAttributes?.forEach(({ key, value }) => {
|
|
644
|
+
if (key === "_amount_upgrade_multiple") upgrade_multiple2 = Number(value) || 1;
|
|
645
|
+
if (key === "_amount_upgrade_value") upgrade_value2 = Number(value) || 0;
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
);
|
|
592
649
|
return [upgrade_multiple2, upgrade_value2];
|
|
593
650
|
}, [effectiveCart?.lineItems, points_subscribe]);
|
|
594
651
|
const breakpoints = useMemo(() => {
|
|
@@ -653,18 +710,24 @@ var useScriptAutoFreeGift = ({
|
|
|
653
710
|
const nextLevel = levelIndex > 0 ? sortedLevels[levelIndex - 1] ?? null : null;
|
|
654
711
|
return [currentLevel, nextLevel];
|
|
655
712
|
}, [breakpoints, involvedSubTotal, involvedLines.length]);
|
|
656
|
-
const { data: giftProductsResult } = useSWR(
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
713
|
+
const { data: giftProductsResult } = useSWR(
|
|
714
|
+
shouldFetch ? giftHandles : null,
|
|
715
|
+
async () => {
|
|
716
|
+
const res = await getProductsByHandles(client, {
|
|
717
|
+
handles: giftHandles,
|
|
718
|
+
locale
|
|
719
|
+
});
|
|
720
|
+
const result = Array.isArray(res) ? res : [];
|
|
721
|
+
giftProductsCache.current = {
|
|
722
|
+
data: result,
|
|
723
|
+
giftHandles: [...giftHandles]
|
|
724
|
+
};
|
|
725
|
+
return result;
|
|
726
|
+
},
|
|
727
|
+
{
|
|
728
|
+
revalidateOnFocus: false
|
|
729
|
+
}
|
|
730
|
+
);
|
|
668
731
|
const finalGiftProductsResult = useMemo(() => {
|
|
669
732
|
if (giftProductsCache.current && !shouldFetch) {
|
|
670
733
|
return giftProductsCache.current.data || void 0;
|
|
@@ -731,11 +794,23 @@ function useAddCartLines(options) {
|
|
|
731
794
|
const { mutateCart, metafieldIdentifiers } = useCartContext();
|
|
732
795
|
const addLines = useCallback(
|
|
733
796
|
async (_key, { arg }) => {
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
797
|
+
const { cartId, lines } = arg;
|
|
798
|
+
const id = cartId || cartCookieAdapter?.getCartId(locale);
|
|
799
|
+
let updatedCart;
|
|
800
|
+
if (!id) {
|
|
801
|
+
updatedCart = await createCart(client, {
|
|
802
|
+
lines,
|
|
803
|
+
metafieldIdentifiers,
|
|
804
|
+
cookieAdapter: cartCookieAdapter
|
|
805
|
+
});
|
|
806
|
+
} else {
|
|
807
|
+
updatedCart = await addCartLines(client, {
|
|
808
|
+
cartId: id,
|
|
809
|
+
lines,
|
|
810
|
+
metafieldIdentifiers,
|
|
811
|
+
cookieAdapter: cartCookieAdapter
|
|
812
|
+
});
|
|
813
|
+
}
|
|
739
814
|
if (updatedCart) {
|
|
740
815
|
const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
741
816
|
if (unApplicableCodes.length > 0) {
|
|
@@ -782,7 +857,7 @@ var trackAddToCartGA = ({
|
|
|
782
857
|
const currencyCode = variant.product?.price?.currencyCode;
|
|
783
858
|
const totalPrice = lineItems?.reduce(
|
|
784
859
|
(prev, { variant: variant2 }) => prev.plus(
|
|
785
|
-
variant2?.finalPrice?.amount
|
|
860
|
+
variant2?.finalPrice?.amount === void 0 ? Number(variant2?.price?.amount) || 0 : Number(variant2?.finalPrice?.amount) || 0
|
|
786
861
|
),
|
|
787
862
|
new Decimal2(0)
|
|
788
863
|
).toNumber();
|
|
@@ -817,7 +892,7 @@ var trackBeginCheckoutGA = ({
|
|
|
817
892
|
}
|
|
818
893
|
const totalPrice = lineItems?.reduce(
|
|
819
894
|
(prev, { variant }) => prev.plus(
|
|
820
|
-
variant?.finalPrice?.amount
|
|
895
|
+
variant?.finalPrice?.amount === void 0 ? Number(variant?.price?.amount) || 0 : Number(variant?.finalPrice?.amount) || 0
|
|
821
896
|
),
|
|
822
897
|
new Decimal2(0)
|
|
823
898
|
).toNumber();
|
|
@@ -850,10 +925,10 @@ var trackBuyNowGA = ({
|
|
|
850
925
|
return;
|
|
851
926
|
}
|
|
852
927
|
const { variant } = lineItems[0];
|
|
853
|
-
const currencyCode = variant.price?.currencyCode;
|
|
928
|
+
const currencyCode = variant.product?.price?.currencyCode || variant.price?.currencyCode;
|
|
854
929
|
const totalPrice = lineItems?.reduce(
|
|
855
930
|
(prev, { variant: variant2 }) => prev.plus(
|
|
856
|
-
variant2?.finalPrice?.amount
|
|
931
|
+
variant2?.finalPrice?.amount === void 0 ? Number(variant2?.price?.amount) || 0 : Number(variant2?.finalPrice?.amount) || 0
|
|
857
932
|
),
|
|
858
933
|
new Decimal2(0)
|
|
859
934
|
).toNumber();
|
|
@@ -927,7 +1002,7 @@ function useApplyCartCodes(options) {
|
|
|
927
1002
|
if (!discountCodes?.length) {
|
|
928
1003
|
throw new Error("Invalid input used for this operation: Miss discountCode");
|
|
929
1004
|
}
|
|
930
|
-
const cartId = providedCartId
|
|
1005
|
+
const cartId = providedCartId || cart?.id;
|
|
931
1006
|
if (!cartId) {
|
|
932
1007
|
return void 0;
|
|
933
1008
|
}
|
|
@@ -940,12 +1015,18 @@ function useApplyCartCodes(options) {
|
|
|
940
1015
|
cookieAdapter: cartCookieAdapter,
|
|
941
1016
|
metafieldIdentifiers
|
|
942
1017
|
});
|
|
1018
|
+
const unApplicableCodes = discountCodes.filter(
|
|
1019
|
+
(code) => updatedCart?.discountCodes?.find((item) => item.code === code && !item.applicable)
|
|
1020
|
+
);
|
|
1021
|
+
if (unApplicableCodes.length) {
|
|
1022
|
+
throw new Error(`${unApplicableCodes.join(", ")} is not applicable to the cart`);
|
|
1023
|
+
}
|
|
943
1024
|
if (updatedCart) {
|
|
944
1025
|
mutateCart(updatedCart);
|
|
945
1026
|
}
|
|
946
1027
|
return updatedCart;
|
|
947
1028
|
},
|
|
948
|
-
[client, locale, cartCookieAdapter, mutateCart, cart]
|
|
1029
|
+
[client, locale, cartCookieAdapter, mutateCart, cart, metafieldIdentifiers]
|
|
949
1030
|
);
|
|
950
1031
|
return useSWRMutation("apply-codes", applyCodes, options);
|
|
951
1032
|
}
|
|
@@ -955,7 +1036,7 @@ function useRemoveCartCodes(options) {
|
|
|
955
1036
|
const removeCodes = useCallback(
|
|
956
1037
|
async (_key, { arg }) => {
|
|
957
1038
|
const { cartId: providedCartId, discountCodes } = arg;
|
|
958
|
-
const cartId = providedCartId
|
|
1039
|
+
const cartId = providedCartId || cart?.id;
|
|
959
1040
|
const codes = cart?.discountCodes?.filter((code) => !!code.applicable) || [];
|
|
960
1041
|
const leftCodes = codes.filter((code) => discountCodes?.length ? !discountCodes.includes(code.code) : code.code).map((code) => code.code);
|
|
961
1042
|
const updatedCart = await updateCartCodes(client, {
|
|
@@ -969,18 +1050,105 @@ function useRemoveCartCodes(options) {
|
|
|
969
1050
|
}
|
|
970
1051
|
return updatedCart;
|
|
971
1052
|
},
|
|
972
|
-
[client, locale, cartCookieAdapter, mutateCart, cart]
|
|
1053
|
+
[client, locale, cartCookieAdapter, mutateCart, cart, metafieldIdentifiers]
|
|
973
1054
|
);
|
|
974
1055
|
return useSWRMutation("remove-codes", removeCodes, options);
|
|
975
1056
|
}
|
|
1057
|
+
var initSameLinesAttributes = ({
|
|
1058
|
+
cart,
|
|
1059
|
+
line
|
|
1060
|
+
}) => {
|
|
1061
|
+
const sameLineInCart = cart?.lineItems.find(
|
|
1062
|
+
(lineInCart) => lineInCart.variant.sku === line.variant?.sku && lineInCart.product?.handle === line.variant?.product?.handle
|
|
1063
|
+
);
|
|
1064
|
+
const codeAmountAttribute = sameLineInCart?.customAttributes?.find(
|
|
1065
|
+
(attr) => attr.key === CODE_AMOUNT_KEY
|
|
1066
|
+
);
|
|
1067
|
+
const scriptCodeAmountAttribute = sameLineInCart?.customAttributes?.find(
|
|
1068
|
+
(attr) => attr.key === SCRIPT_CODE_AMOUNT_KEY
|
|
1069
|
+
);
|
|
1070
|
+
let functionAttribute = null;
|
|
1071
|
+
try {
|
|
1072
|
+
functionAttribute = sameLineInCart?.customAttributes?.find(
|
|
1073
|
+
(attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY && JSON.parse(attr.value)?.discounted_amount
|
|
1074
|
+
);
|
|
1075
|
+
} catch (error) {
|
|
1076
|
+
}
|
|
1077
|
+
if (codeAmountAttribute || functionAttribute || scriptCodeAmountAttribute) {
|
|
1078
|
+
return {
|
|
1079
|
+
...line,
|
|
1080
|
+
attributes: [
|
|
1081
|
+
...line.attributes || [],
|
|
1082
|
+
codeAmountAttribute,
|
|
1083
|
+
functionAttribute,
|
|
1084
|
+
scriptCodeAmountAttribute
|
|
1085
|
+
].filter(Boolean)
|
|
1086
|
+
};
|
|
1087
|
+
}
|
|
1088
|
+
return line;
|
|
1089
|
+
};
|
|
1090
|
+
var initDiscountAttributes = ({ line }) => {
|
|
1091
|
+
let itemAttributes = line.attributes || [];
|
|
1092
|
+
const functionEnvAttribute = itemAttributes.find((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY);
|
|
1093
|
+
if (!functionEnvAttribute) {
|
|
1094
|
+
itemAttributes = itemAttributes.concat([
|
|
1095
|
+
{
|
|
1096
|
+
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
1097
|
+
value: JSON.stringify({
|
|
1098
|
+
is_gift: false,
|
|
1099
|
+
discounted_amount: line.variant?.finalPrice?.amount === void 0 ? Number(line.variant?.price?.amount) * (line.quantity || 1) : Number(line.variant?.finalPrice?.amount) * (line.quantity || 1)
|
|
1100
|
+
})
|
|
1101
|
+
}
|
|
1102
|
+
]);
|
|
1103
|
+
}
|
|
1104
|
+
const memberPriceAttribute = itemAttributes.find(
|
|
1105
|
+
(attr) => attr.key === MEMBER_PRICE_ATTRIBUTE_KEY
|
|
1106
|
+
);
|
|
1107
|
+
const coupon = line.coupon;
|
|
1108
|
+
if (!memberPriceAttribute && coupon) {
|
|
1109
|
+
itemAttributes = itemAttributes.concat([
|
|
1110
|
+
{
|
|
1111
|
+
key: MEMBER_PRICE_ATTRIBUTE_KEY,
|
|
1112
|
+
value: JSON.stringify({ code: coupon.code })
|
|
1113
|
+
}
|
|
1114
|
+
]);
|
|
1115
|
+
}
|
|
1116
|
+
const couponDiscountAttribute = itemAttributes.find(
|
|
1117
|
+
(attr) => attr.key === CODE_AMOUNT_KEY || attr.key === SCRIPT_CODE_AMOUNT_KEY
|
|
1118
|
+
);
|
|
1119
|
+
if (!couponDiscountAttribute && coupon && Number(coupon?.amount) > 0) {
|
|
1120
|
+
itemAttributes = itemAttributes.concat([
|
|
1121
|
+
{
|
|
1122
|
+
key: CODE_AMOUNT_KEY,
|
|
1123
|
+
value: new Decimal2(coupon.amount).times(line.quantity || 1).toString()
|
|
1124
|
+
},
|
|
1125
|
+
{
|
|
1126
|
+
key: SCRIPT_CODE_AMOUNT_KEY,
|
|
1127
|
+
value: new Decimal2(coupon.amount).times(line.quantity || 1).toString()
|
|
1128
|
+
}
|
|
1129
|
+
]);
|
|
1130
|
+
}
|
|
1131
|
+
return { ...line, attributes: itemAttributes };
|
|
1132
|
+
};
|
|
1133
|
+
var getLinesWithAttributes = ({
|
|
1134
|
+
cart,
|
|
1135
|
+
lineItems
|
|
1136
|
+
}) => {
|
|
1137
|
+
return lineItems.map((line) => {
|
|
1138
|
+
const sameLine = initSameLinesAttributes({ cart, line });
|
|
1139
|
+
const functionLine = initDiscountAttributes({ line: sameLine });
|
|
1140
|
+
return functionLine;
|
|
1141
|
+
});
|
|
1142
|
+
};
|
|
976
1143
|
|
|
977
1144
|
// src/hooks/cart/use-add-to-cart.ts
|
|
978
1145
|
function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
979
|
-
const { client, config, locale, cartCookieAdapter, userAdapter } = useShopify();
|
|
980
|
-
const { cart } = useCartContext();
|
|
1146
|
+
const { client, config, locale, cartCookieAdapter, userAdapter, performanceAdapter } = useShopify();
|
|
1147
|
+
const { cart, addCustomAttributes } = useCartContext();
|
|
981
1148
|
const { trigger: applyCartCodes } = useApplyCartCodes();
|
|
982
1149
|
const { trigger: removeInvalidCodes } = useRemoveCartCodes();
|
|
983
1150
|
const { trigger: addCartLines2 } = useAddCartLines();
|
|
1151
|
+
const { trigger: createCart4 } = useCreateCart();
|
|
984
1152
|
const addToCart = useCallback(
|
|
985
1153
|
async (_key, { arg }) => {
|
|
986
1154
|
const {
|
|
@@ -991,12 +1159,15 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
991
1159
|
buyerIdentity,
|
|
992
1160
|
needCreateCart = false,
|
|
993
1161
|
onCodesInvalid,
|
|
994
|
-
replaceExistingCodes
|
|
1162
|
+
replaceExistingCodes,
|
|
1163
|
+
customAttributes
|
|
995
1164
|
} = arg;
|
|
996
1165
|
if (!lineItems || lineItems.length === 0) {
|
|
997
1166
|
return;
|
|
998
1167
|
}
|
|
999
|
-
|
|
1168
|
+
performanceAdapter?.addToCartStart();
|
|
1169
|
+
const linesWithFunctionAttributes = getLinesWithAttributes({ cart, lineItems });
|
|
1170
|
+
const lines = linesWithFunctionAttributes.map((item) => ({
|
|
1000
1171
|
merchandiseId: item.variant?.id || "",
|
|
1001
1172
|
quantity: item.quantity || 1,
|
|
1002
1173
|
attributes: item.attributes,
|
|
@@ -1005,36 +1176,45 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
1005
1176
|
if (lines.length === 0) {
|
|
1006
1177
|
return;
|
|
1007
1178
|
}
|
|
1008
|
-
|
|
1009
|
-
let resultCart =
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1179
|
+
let cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
|
|
1180
|
+
let resultCart = null;
|
|
1181
|
+
if (!cartId) {
|
|
1182
|
+
resultCart = await createCart4({
|
|
1183
|
+
lines,
|
|
1184
|
+
buyerIdentity,
|
|
1185
|
+
discountCodes,
|
|
1186
|
+
customAttributes
|
|
1187
|
+
});
|
|
1188
|
+
} else {
|
|
1189
|
+
resultCart = await addCartLines2({
|
|
1190
|
+
cartId,
|
|
1191
|
+
lines
|
|
1192
|
+
});
|
|
1193
|
+
console.log("npm addCartLines resultCart", resultCart);
|
|
1194
|
+
if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
|
|
1195
|
+
const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
1196
|
+
if (unapplicableCodes.length > 0) {
|
|
1197
|
+
if (onCodesInvalid) {
|
|
1198
|
+
const handledCart = await onCodesInvalid(resultCart, unapplicableCodes);
|
|
1199
|
+
if (handledCart) {
|
|
1200
|
+
resultCart = handledCart;
|
|
1201
|
+
}
|
|
1202
|
+
} else {
|
|
1203
|
+
await removeInvalidCodes({
|
|
1204
|
+
discountCodes: unapplicableCodes
|
|
1205
|
+
});
|
|
1025
1206
|
}
|
|
1026
|
-
} else {
|
|
1027
|
-
await removeInvalidCodes({
|
|
1028
|
-
discountCodes: unapplicableCodes
|
|
1029
|
-
});
|
|
1030
1207
|
}
|
|
1031
1208
|
}
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
}
|
|
1209
|
+
if (resultCart && discountCodes && discountCodes.length > 0) {
|
|
1210
|
+
applyCartCodes({
|
|
1211
|
+
replaceExistingCodes,
|
|
1212
|
+
discountCodes
|
|
1213
|
+
});
|
|
1214
|
+
}
|
|
1215
|
+
if (customAttributes && customAttributes.length > 0) {
|
|
1216
|
+
addCustomAttributes(customAttributes);
|
|
1217
|
+
}
|
|
1038
1218
|
}
|
|
1039
1219
|
if (withTrack) {
|
|
1040
1220
|
trackAddToCartGA({
|
|
@@ -1043,9 +1223,24 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
1043
1223
|
});
|
|
1044
1224
|
trackAddToCartFBQ({ lineItems });
|
|
1045
1225
|
}
|
|
1226
|
+
performanceAdapter?.addToCartEnd();
|
|
1046
1227
|
return resultCart;
|
|
1047
1228
|
},
|
|
1048
|
-
[
|
|
1229
|
+
[
|
|
1230
|
+
client,
|
|
1231
|
+
locale,
|
|
1232
|
+
cartCookieAdapter,
|
|
1233
|
+
userAdapter,
|
|
1234
|
+
cart,
|
|
1235
|
+
withTrack,
|
|
1236
|
+
performanceAdapter,
|
|
1237
|
+
createCart4,
|
|
1238
|
+
addCartLines2,
|
|
1239
|
+
applyCartCodes,
|
|
1240
|
+
removeInvalidCodes,
|
|
1241
|
+
addCustomAttributes,
|
|
1242
|
+
config
|
|
1243
|
+
]
|
|
1049
1244
|
);
|
|
1050
1245
|
return useSWRMutation("add-to-cart", addToCart, swrOptions);
|
|
1051
1246
|
}
|
|
@@ -1062,9 +1257,10 @@ function useUpdateCartLines(options) {
|
|
|
1062
1257
|
if (updatedCart) {
|
|
1063
1258
|
mutateCart(updatedCart);
|
|
1064
1259
|
}
|
|
1260
|
+
console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
|
|
1065
1261
|
return updatedCart;
|
|
1066
1262
|
},
|
|
1067
|
-
[client, locale, cartCookieAdapter, mutateCart]
|
|
1263
|
+
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
1068
1264
|
);
|
|
1069
1265
|
return useSWRMutation("update-cart-lines", updateLines, options);
|
|
1070
1266
|
}
|
|
@@ -1103,7 +1299,7 @@ function useRemoveCartLines(options) {
|
|
|
1103
1299
|
}
|
|
1104
1300
|
return updatedCart;
|
|
1105
1301
|
},
|
|
1106
|
-
[client, locale, cartCookieAdapter, mutateCart]
|
|
1302
|
+
[client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
|
|
1107
1303
|
);
|
|
1108
1304
|
return useSWRMutation("remove-cart-lines", removeLines, options);
|
|
1109
1305
|
}
|
|
@@ -1122,7 +1318,7 @@ function useUpdateCartAttributes(mutate, metafieldIdentifiers, options) {
|
|
|
1122
1318
|
}
|
|
1123
1319
|
return updatedCart;
|
|
1124
1320
|
},
|
|
1125
|
-
[client, locale, cartCookieAdapter, mutate]
|
|
1321
|
+
[client, locale, cartCookieAdapter, mutate, metafieldIdentifiers]
|
|
1126
1322
|
);
|
|
1127
1323
|
return useSWRMutation("update-cart-attributes", updateAttributes, options);
|
|
1128
1324
|
}
|
|
@@ -1144,12 +1340,15 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
1144
1340
|
if (!lineItems || lineItems.length === 0) {
|
|
1145
1341
|
return;
|
|
1146
1342
|
}
|
|
1147
|
-
const
|
|
1343
|
+
const linesWithFunctionAttributes = getLinesWithAttributes({
|
|
1344
|
+
lineItems
|
|
1345
|
+
});
|
|
1346
|
+
const lines = linesWithFunctionAttributes.map((item) => ({
|
|
1148
1347
|
merchandiseId: item.variant?.id || "",
|
|
1149
1348
|
quantity: item.quantity || 1,
|
|
1150
1349
|
attributes: item.attributes,
|
|
1151
1350
|
sellingPlanId: item.sellingPlanId
|
|
1152
|
-
})).filter((item) => item.merchandiseId
|
|
1351
|
+
})).filter((item) => item.merchandiseId);
|
|
1153
1352
|
if (lines.length === 0) {
|
|
1154
1353
|
return;
|
|
1155
1354
|
}
|
|
@@ -1204,17 +1403,41 @@ function useCalcGiftsFromLines({
|
|
|
1204
1403
|
lines
|
|
1205
1404
|
});
|
|
1206
1405
|
const allGiftLines = useMemo(() => {
|
|
1207
|
-
const functionGiftLines = functionGift.qualifyingGift?.itemsToAdd || []
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
(p) => p.handle === product.handle
|
|
1406
|
+
const functionGiftLines = (functionGift.qualifyingGift?.itemsToAdd || []).map((item) => {
|
|
1407
|
+
const product = functionGift.giftProductsResult?.find(
|
|
1408
|
+
(product2) => product2.handle === item.variant.handle
|
|
1211
1409
|
);
|
|
1212
|
-
const
|
|
1410
|
+
const variants = product?.variants;
|
|
1411
|
+
const variant = Array.isArray(variants) ? variants.find((v) => v.sku === item.variant.sku) : void 0;
|
|
1412
|
+
if (!variant) {
|
|
1413
|
+
console.warn(
|
|
1414
|
+
`Function gift: Variant not found for handle=${item.variant.handle}, sku=${item.variant.sku}`
|
|
1415
|
+
);
|
|
1416
|
+
return null;
|
|
1417
|
+
}
|
|
1418
|
+
return {
|
|
1419
|
+
variant: {
|
|
1420
|
+
...variant,
|
|
1421
|
+
product
|
|
1422
|
+
},
|
|
1423
|
+
quantity: item.quantity ?? 1,
|
|
1424
|
+
attributes: item.attributes
|
|
1425
|
+
};
|
|
1426
|
+
}).filter((item) => item !== null);
|
|
1427
|
+
const scriptGiftLines = scriptGift.freeGiftLevel ? scriptGift.freeGiftLevel.giveawayProducts.map((item) => {
|
|
1428
|
+
const product = scriptGift.giftProductsResult?.find(
|
|
1429
|
+
(product2) => product2.handle === item.handle
|
|
1430
|
+
);
|
|
1431
|
+
const variants = product?.variants;
|
|
1432
|
+
const variant = Array.isArray(variants) ? variants.find((v) => v.sku === item.sku) : void 0;
|
|
1433
|
+
if (!variant) {
|
|
1434
|
+
console.warn(`Script gift: Variant not found for handle=${item.handle}, sku=${item.sku}`);
|
|
1435
|
+
return null;
|
|
1436
|
+
}
|
|
1213
1437
|
return {
|
|
1214
1438
|
variant: {
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
sku: product.sku
|
|
1439
|
+
...variant,
|
|
1440
|
+
product
|
|
1218
1441
|
},
|
|
1219
1442
|
quantity: 1,
|
|
1220
1443
|
attributes: [
|
|
@@ -1224,10 +1447,11 @@ function useCalcGiftsFromLines({
|
|
|
1224
1447
|
}
|
|
1225
1448
|
]
|
|
1226
1449
|
};
|
|
1227
|
-
}).filter((item) => item
|
|
1450
|
+
}).filter((item) => item !== null) : [];
|
|
1228
1451
|
return [...functionGiftLines, ...scriptGiftLines];
|
|
1229
1452
|
}, [
|
|
1230
1453
|
functionGift.qualifyingGift,
|
|
1454
|
+
functionGift.giftProductsResult,
|
|
1231
1455
|
scriptGift.freeGiftLevel,
|
|
1232
1456
|
scriptGift.giftProductsResult,
|
|
1233
1457
|
scriptGiveawayKey
|
|
@@ -1262,7 +1486,7 @@ var useCalcOrderDiscount = (cart, orderDiscountConfig, customer) => {
|
|
|
1262
1486
|
const isCustomerLoading = useMemo(() => !customer ? true : false, [customer]);
|
|
1263
1487
|
const dealsType = "";
|
|
1264
1488
|
const { activeCampaign, subtotal } = useMemo(() => {
|
|
1265
|
-
for (const campaign of orderDiscountConfig) {
|
|
1489
|
+
for (const campaign of orderDiscountConfig || []) {
|
|
1266
1490
|
const { rule_conditions = [], result_detail } = campaign;
|
|
1267
1491
|
const { main_product, order_discount_conf } = result_detail || {};
|
|
1268
1492
|
const isPreCheckPassed = preCheck(rule_conditions, tags, []);
|
|
@@ -1292,9 +1516,12 @@ var useCalcOrderDiscount = (cart, orderDiscountConfig, customer) => {
|
|
|
1292
1516
|
discountAmount: 0
|
|
1293
1517
|
};
|
|
1294
1518
|
}
|
|
1295
|
-
const
|
|
1296
|
-
|
|
1297
|
-
const
|
|
1519
|
+
const currentCurrency = cart?.currency?.code || "";
|
|
1520
|
+
console.log("currentCurrency", cart, currentCurrency);
|
|
1521
|
+
const orderDiscountConf = activeCampaign.result_detail.order_discount_conf;
|
|
1522
|
+
const tieredDiscounts = orderDiscountConf.tiered_discounts_markets?.[currentCurrency] || orderDiscountConf.tiered_discounts;
|
|
1523
|
+
const qualifyingTier = [...tieredDiscounts].sort((a, b) => Number(b.amount) - Number(a.amount)).find((tier) => subtotal >= Number(tier.amount));
|
|
1524
|
+
const nextGoal = [...tieredDiscounts].sort((a, b) => Number(a.amount) - Number(b.amount)).find((tier) => subtotal < Number(tier.amount));
|
|
1298
1525
|
if (!qualifyingTier) {
|
|
1299
1526
|
return {
|
|
1300
1527
|
qualifyingDiscount: null,
|
|
@@ -1362,12 +1589,10 @@ function useHasPlusMemberInCart({
|
|
|
1362
1589
|
};
|
|
1363
1590
|
}, [cart?.lineItems, plus_monthly_product, plus_annual_product]);
|
|
1364
1591
|
}
|
|
1365
|
-
|
|
1366
|
-
// src/hooks/cart/feature/use-cart-attributes.ts
|
|
1367
1592
|
var getReferralAttributes = () => {
|
|
1368
|
-
const inviteCode = Cookies5.get("
|
|
1369
|
-
const playModeId = Cookies5.get("playModeId");
|
|
1370
|
-
const popup = Cookies5.get("_popup");
|
|
1593
|
+
const inviteCode = getLocalStorage("inviteCode") || Cookies5.get("inviteCode");
|
|
1594
|
+
const playModeId = getLocalStorage("playModeId") || Cookies5.get("playModeId");
|
|
1595
|
+
const popup = getLocalStorage("_popup") || Cookies5.get("_popup");
|
|
1371
1596
|
if (inviteCode && playModeId) {
|
|
1372
1597
|
return popup ? [
|
|
1373
1598
|
{ key: "_invite_code", value: inviteCode ? inviteCode : "" },
|
|
@@ -1391,8 +1616,6 @@ var useCartAttributes = ({
|
|
|
1391
1616
|
memberSetting,
|
|
1392
1617
|
cart
|
|
1393
1618
|
});
|
|
1394
|
-
console.log("memberSetting", memberSetting);
|
|
1395
|
-
console.log("hasPlusMember", hasPlusMember);
|
|
1396
1619
|
useEffect(() => {
|
|
1397
1620
|
setCurrentUrl(window.location.href);
|
|
1398
1621
|
}, []);
|
|
@@ -1418,7 +1641,7 @@ var useCartAttributes = ({
|
|
|
1418
1641
|
return "new_user_login";
|
|
1419
1642
|
}, [customer]);
|
|
1420
1643
|
const memberAttributes = useMemo(() => {
|
|
1421
|
-
|
|
1644
|
+
const attributes = [
|
|
1422
1645
|
{
|
|
1423
1646
|
key: "_token",
|
|
1424
1647
|
value: profile?.token
|
|
@@ -1439,17 +1662,28 @@ var useCartAttributes = ({
|
|
|
1439
1662
|
value: profile?.token ? "true" : "false"
|
|
1440
1663
|
}
|
|
1441
1664
|
];
|
|
1665
|
+
if (profile?.token) {
|
|
1666
|
+
attributes.push({
|
|
1667
|
+
key: "_login_user",
|
|
1668
|
+
value: "1"
|
|
1669
|
+
});
|
|
1670
|
+
}
|
|
1671
|
+
return attributes;
|
|
1442
1672
|
}, [profile?.memberType, profile?.token, userType, hasPlusMember]);
|
|
1443
1673
|
const functionAttributes = useMemo(() => {
|
|
1444
|
-
|
|
1445
|
-
|
|
1674
|
+
const hasFunctionEnvAttribute = cart?.lineItems.some(
|
|
1675
|
+
(item) => item.customAttributes?.some((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY)
|
|
1676
|
+
);
|
|
1677
|
+
const discountCodes = cart?.discountCodes.map((item) => item.code).filter((code) => code) || [];
|
|
1678
|
+
return hasFunctionEnvAttribute ? [
|
|
1679
|
+
{
|
|
1446
1680
|
key: "_discounts_function_env",
|
|
1447
1681
|
value: JSON.stringify({
|
|
1448
|
-
discount_code:
|
|
1682
|
+
discount_code: discountCodes,
|
|
1449
1683
|
user_tags: customer?.tags || []
|
|
1450
1684
|
})
|
|
1451
1685
|
}
|
|
1452
|
-
];
|
|
1686
|
+
] : [];
|
|
1453
1687
|
}, [cart]);
|
|
1454
1688
|
const presellAttributes = useMemo(() => {
|
|
1455
1689
|
return [
|
|
@@ -1481,18 +1715,50 @@ var useCartAttributes = ({
|
|
|
1481
1715
|
}
|
|
1482
1716
|
];
|
|
1483
1717
|
}, [currentUrl]);
|
|
1718
|
+
const commonAttributes = useMemo(
|
|
1719
|
+
() => [
|
|
1720
|
+
...memberAttributes,
|
|
1721
|
+
...functionAttributes,
|
|
1722
|
+
...presellAttributes,
|
|
1723
|
+
...weightAttributes,
|
|
1724
|
+
...trackingAttributes,
|
|
1725
|
+
...getReferralAttributes()
|
|
1726
|
+
].filter((item) => item?.value),
|
|
1727
|
+
[memberAttributes, functionAttributes, presellAttributes, weightAttributes, trackingAttributes]
|
|
1728
|
+
);
|
|
1729
|
+
const extraAttributesInCart = useMemo(() => {
|
|
1730
|
+
const commonAttributeKeys = [
|
|
1731
|
+
// member attributes
|
|
1732
|
+
"_token",
|
|
1733
|
+
"_member_type",
|
|
1734
|
+
"_user_type",
|
|
1735
|
+
"_is_login",
|
|
1736
|
+
"_login_user",
|
|
1737
|
+
// function attributes
|
|
1738
|
+
"_discounts_function_env",
|
|
1739
|
+
// presell attributes
|
|
1740
|
+
"_presale",
|
|
1741
|
+
// weight attributes
|
|
1742
|
+
"_weight",
|
|
1743
|
+
"_app_source_name",
|
|
1744
|
+
// tracking attributes
|
|
1745
|
+
"utm_params",
|
|
1746
|
+
// referral attributes
|
|
1747
|
+
"_invite_code",
|
|
1748
|
+
"_play_mode_id",
|
|
1749
|
+
"_popup"
|
|
1750
|
+
];
|
|
1751
|
+
return cart?.customAttributes?.filter(
|
|
1752
|
+
(item) => !commonAttributeKeys.includes(item.key)
|
|
1753
|
+
) || [];
|
|
1754
|
+
}, [cart]);
|
|
1484
1755
|
return useMemo(
|
|
1485
1756
|
() => ({
|
|
1486
|
-
attributes: [
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
...presellAttributes,
|
|
1490
|
-
...weightAttributes,
|
|
1491
|
-
...trackingAttributes,
|
|
1492
|
-
...getReferralAttributes()
|
|
1493
|
-
].filter((item) => item?.value)
|
|
1757
|
+
attributes: [...commonAttributes, ...extraAttributesInCart].filter(
|
|
1758
|
+
(item) => item?.value
|
|
1759
|
+
)
|
|
1494
1760
|
}),
|
|
1495
|
-
[
|
|
1761
|
+
[commonAttributes, extraAttributesInCart]
|
|
1496
1762
|
);
|
|
1497
1763
|
};
|
|
1498
1764
|
var DEFAULT_MIN = 1;
|
|
@@ -1555,7 +1821,7 @@ var useUpdateLineCodeAmountAttributes = ({
|
|
|
1555
1821
|
);
|
|
1556
1822
|
const functionEnvValue = getDiscountEnvAttributeValue(line.customAttributes);
|
|
1557
1823
|
const hasSameFunctionEnvAttribute = Number(functionEnvValue.discounted_amount) === Number(line.totalAmount);
|
|
1558
|
-
if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute) {
|
|
1824
|
+
if (!hasSameFunctionEnvAttribute && hasFunctionEnvAttribute && !functionEnvValue.is_gift) {
|
|
1559
1825
|
attrNeedUpdate.push({
|
|
1560
1826
|
key: CUSTOMER_ATTRIBUTE_KEY,
|
|
1561
1827
|
value: JSON.stringify({
|
|
@@ -1594,29 +1860,22 @@ var useUpdateLineCodeAmountAttributes = ({
|
|
|
1594
1860
|
}).filter(
|
|
1595
1861
|
({ attrNeedUpdate, attrNeedDelete }) => attrNeedUpdate.length || attrNeedDelete.length
|
|
1596
1862
|
).map(({ line, attrNeedUpdate, attrNeedDelete }) => {
|
|
1863
|
+
let lineId = line.id;
|
|
1864
|
+
let attributes = line.customAttributes || [];
|
|
1865
|
+
if (attrNeedDelete.length) {
|
|
1866
|
+
attributes = attributes.filter(
|
|
1867
|
+
(attr) => !attrNeedDelete.includes(attr.key)
|
|
1868
|
+
);
|
|
1869
|
+
}
|
|
1597
1870
|
if (attrNeedUpdate.length) {
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
...line.customAttributes?.filter(
|
|
1602
|
-
(attr) => !attrNeedUpdate.some((updateAttr) => updateAttr.key === attr.key)
|
|
1603
|
-
) || [],
|
|
1604
|
-
...attrNeedUpdate
|
|
1605
|
-
]
|
|
1606
|
-
};
|
|
1607
|
-
} else if (attrNeedDelete.length) {
|
|
1608
|
-
return {
|
|
1609
|
-
id: line.id,
|
|
1610
|
-
attributes: line.customAttributes?.filter(
|
|
1611
|
-
(attr) => !attrNeedDelete.includes(attr.key)
|
|
1612
|
-
) || []
|
|
1613
|
-
};
|
|
1614
|
-
} else {
|
|
1615
|
-
return {
|
|
1616
|
-
id: line.id,
|
|
1617
|
-
attributes: line.customAttributes || []
|
|
1618
|
-
};
|
|
1871
|
+
attributes = attributes.filter(
|
|
1872
|
+
(attr) => !attrNeedUpdate.some((updateAttr) => updateAttr.key === attr.key)
|
|
1873
|
+
).concat(attrNeedUpdate);
|
|
1619
1874
|
}
|
|
1875
|
+
return {
|
|
1876
|
+
id: lineId,
|
|
1877
|
+
attributes
|
|
1878
|
+
};
|
|
1620
1879
|
}),
|
|
1621
1880
|
[cart?.lineItems, mainProductDiscountCodes]
|
|
1622
1881
|
);
|
|
@@ -1709,8 +1968,9 @@ function useProductsByHandles(options = {}) {
|
|
|
1709
1968
|
metafieldIdentifiers
|
|
1710
1969
|
});
|
|
1711
1970
|
},
|
|
1712
|
-
|
|
1713
|
-
revalidateOnFocus: false
|
|
1971
|
+
{
|
|
1972
|
+
revalidateOnFocus: false,
|
|
1973
|
+
...swrOptions
|
|
1714
1974
|
}
|
|
1715
1975
|
);
|
|
1716
1976
|
}
|
|
@@ -2339,7 +2599,10 @@ var createInitialValue = () => ({
|
|
|
2339
2599
|
freeShippingMethods: [],
|
|
2340
2600
|
paymentShippingMethods: [],
|
|
2341
2601
|
nddOverweight: false,
|
|
2342
|
-
tddOverweight: false
|
|
2602
|
+
tddOverweight: false,
|
|
2603
|
+
nddCoupon: void 0,
|
|
2604
|
+
tddCoupon: void 0,
|
|
2605
|
+
isLoadingCoupon: false
|
|
2343
2606
|
},
|
|
2344
2607
|
selectedPlusMemberProduct: null,
|
|
2345
2608
|
plusMemberProducts: [],
|
|
@@ -2384,15 +2647,29 @@ function usePlusAnnualProductVariant() {
|
|
|
2384
2647
|
}, [plusMemberProducts, plusAnnual]);
|
|
2385
2648
|
return plusAnnualProductVariant;
|
|
2386
2649
|
}
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2650
|
+
var useAvailableDeliveryCoupon = ({
|
|
2651
|
+
profile
|
|
2652
|
+
}) => {
|
|
2653
|
+
const { data: availableDeliveryCoupon, isLoading } = useSWR(
|
|
2654
|
+
profile?.email ? ["/api/multipass/subsrv/v1/prime/delivery_coupons/current/available", profile?.email] : void 0,
|
|
2655
|
+
async ([apiPath]) => {
|
|
2656
|
+
return fetch(apiPath).then((res) => res.json());
|
|
2657
|
+
}
|
|
2658
|
+
);
|
|
2659
|
+
console.log("availableDeliveryCoupon", availableDeliveryCoupon);
|
|
2660
|
+
const { ndd_coupon: nddCoupon, tdd_coupon: tddCoupon } = availableDeliveryCoupon?.data?.data || {};
|
|
2661
|
+
return {
|
|
2393
2662
|
nddCoupon,
|
|
2394
|
-
tddCoupon
|
|
2395
|
-
|
|
2663
|
+
tddCoupon,
|
|
2664
|
+
isLoading
|
|
2665
|
+
};
|
|
2666
|
+
};
|
|
2667
|
+
|
|
2668
|
+
// src/hooks/member/plus/use-shipping-methods.ts
|
|
2669
|
+
function useShippingMethods(options) {
|
|
2670
|
+
const { variant, plusMemberMetafields, selectedPlusMemberMode, isPlus = false, profile } = options;
|
|
2671
|
+
const { nddCoupon, tddCoupon, isLoading } = useAvailableDeliveryCoupon({ profile });
|
|
2672
|
+
console.log("nddCoupon", nddCoupon);
|
|
2396
2673
|
const { plus_shipping, shippingMethod } = plusMemberMetafields || {};
|
|
2397
2674
|
const nddOverweight = useMemo(() => {
|
|
2398
2675
|
return (variant?.weight || 0) > (shippingMethod?.overWeight_ndd || Infinity);
|
|
@@ -2402,12 +2679,10 @@ function useShippingMethods(options) {
|
|
|
2402
2679
|
}, [shippingMethod?.overWeight_tdd, variant?.weight]);
|
|
2403
2680
|
const paymentShippingMethods = useMemo(() => {
|
|
2404
2681
|
const weight = variant?.weight || 0;
|
|
2405
|
-
const methods = plus_shipping?.shipping_methods?.filter(
|
|
2406
|
-
(
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
}
|
|
2410
|
-
) || [];
|
|
2682
|
+
const methods = plus_shipping?.shipping_methods?.filter(({ weight_low, weight_high, __mode, __plus }) => {
|
|
2683
|
+
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
2684
|
+
return __mode !== "free" /* FREE */ && !__plus && fitWeight;
|
|
2685
|
+
}) || [];
|
|
2411
2686
|
return methods.map((method) => {
|
|
2412
2687
|
let disabled = false;
|
|
2413
2688
|
const selectedFreeMember = selectedPlusMemberMode === "free";
|
|
@@ -2434,40 +2709,34 @@ function useShippingMethods(options) {
|
|
|
2434
2709
|
]);
|
|
2435
2710
|
const nddPrice = useMemo(() => {
|
|
2436
2711
|
const weight = variant?.weight || 0;
|
|
2437
|
-
const nddMethod = paymentShippingMethods.find(
|
|
2438
|
-
(
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
}
|
|
2442
|
-
);
|
|
2712
|
+
const nddMethod = paymentShippingMethods.find(({ __mode, weight_high, weight_low }) => {
|
|
2713
|
+
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
2714
|
+
return __mode === "ndd" && fitWeight;
|
|
2715
|
+
});
|
|
2443
2716
|
return nddMethod?.price || 0;
|
|
2444
2717
|
}, [variant?.weight, paymentShippingMethods]);
|
|
2445
2718
|
const tddPrice = useMemo(() => {
|
|
2446
2719
|
const weight = variant?.weight || 0;
|
|
2447
|
-
const tddMethod = paymentShippingMethods.find(
|
|
2448
|
-
(
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
}
|
|
2452
|
-
);
|
|
2720
|
+
const tddMethod = paymentShippingMethods.find(({ __mode, weight_high, weight_low }) => {
|
|
2721
|
+
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
2722
|
+
return __mode === "tdd" && fitWeight;
|
|
2723
|
+
});
|
|
2453
2724
|
return tddMethod?.price || 0;
|
|
2454
2725
|
}, [variant?.weight, paymentShippingMethods]);
|
|
2455
2726
|
const freeShippingMethods = useMemo(() => {
|
|
2456
2727
|
const weight = variant?.weight || 0;
|
|
2457
|
-
let methods = plus_shipping?.shipping_methods?.filter(
|
|
2458
|
-
(
|
|
2459
|
-
|
|
2460
|
-
return true;
|
|
2461
|
-
}
|
|
2462
|
-
if (isPlus) {
|
|
2463
|
-
const hasCoupon = isPlus && __mode === "ndd" /* NDD */ && nddCoupon || isPlus && __mode === "tdd" /* TDD */ && (tddCoupon || nddCoupon);
|
|
2464
|
-
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
2465
|
-
return hasCoupon && fitWeight && !__plus;
|
|
2466
|
-
} else {
|
|
2467
|
-
return __plus;
|
|
2468
|
-
}
|
|
2728
|
+
let methods = plus_shipping?.shipping_methods?.filter(({ __mode, __plus, weight_low, weight_high }) => {
|
|
2729
|
+
if (__mode === "free" /* FREE */) {
|
|
2730
|
+
return true;
|
|
2469
2731
|
}
|
|
2470
|
-
|
|
2732
|
+
if (isPlus) {
|
|
2733
|
+
const hasCoupon = isPlus && __mode === "ndd" /* NDD */ && nddCoupon || isPlus && __mode === "tdd" /* TDD */ && (tddCoupon || nddCoupon);
|
|
2734
|
+
const fitWeight = (!weight_low || weight >= weight_low) && (!weight_high || weight <= weight_high);
|
|
2735
|
+
return hasCoupon && fitWeight && !__plus;
|
|
2736
|
+
} else {
|
|
2737
|
+
return __plus;
|
|
2738
|
+
}
|
|
2739
|
+
}) || [];
|
|
2471
2740
|
if (isPlus) {
|
|
2472
2741
|
methods = methods.sort((a, b) => {
|
|
2473
2742
|
if (b.__mode === "free" /* FREE */) return -1;
|
|
@@ -2521,7 +2790,10 @@ function useShippingMethods(options) {
|
|
|
2521
2790
|
freeShippingMethods,
|
|
2522
2791
|
paymentShippingMethods,
|
|
2523
2792
|
nddOverweight,
|
|
2524
|
-
tddOverweight
|
|
2793
|
+
tddOverweight,
|
|
2794
|
+
nddCoupon,
|
|
2795
|
+
tddCoupon,
|
|
2796
|
+
isLoadingCoupon: isLoading
|
|
2525
2797
|
};
|
|
2526
2798
|
}
|
|
2527
2799
|
function useShippingMethodAvailableCheck() {
|
|
@@ -2626,6 +2898,73 @@ var usePlusMemberDeliveryCodes = ({
|
|
|
2626
2898
|
[deliveryData]
|
|
2627
2899
|
);
|
|
2628
2900
|
};
|
|
2901
|
+
function useUpdateCartDeliveryOptions(mutate, metafieldIdentifiers, options) {
|
|
2902
|
+
const { client, locale, cartCookieAdapter } = useShopify();
|
|
2903
|
+
const updateDeliveryOptions = useCallback(
|
|
2904
|
+
async (_key, { arg }) => {
|
|
2905
|
+
const updatedCart = await updateCartDeliveryOptions(client, {
|
|
2906
|
+
...arg,
|
|
2907
|
+
metafieldIdentifiers,
|
|
2908
|
+
cookieAdapter: cartCookieAdapter
|
|
2909
|
+
});
|
|
2910
|
+
console.log("useUpdateCartDeliveryOptions updatedCart", updatedCart);
|
|
2911
|
+
if (updatedCart) {
|
|
2912
|
+
mutate(updatedCart);
|
|
2913
|
+
}
|
|
2914
|
+
return updatedCart;
|
|
2915
|
+
},
|
|
2916
|
+
[client, locale, cartCookieAdapter, mutate, metafieldIdentifiers]
|
|
2917
|
+
);
|
|
2918
|
+
return useSWRMutation("update-cart-delivery-options", updateDeliveryOptions, options);
|
|
2919
|
+
}
|
|
2920
|
+
|
|
2921
|
+
// src/hooks/member/plus/use-update-plus-member-delivery-options.ts
|
|
2922
|
+
var useUpdatePlusMemberDeliveryOptions = ({
|
|
2923
|
+
options
|
|
2924
|
+
} = {}) => {
|
|
2925
|
+
const { cart: cartContextData, mutateCart, metafieldIdentifiers } = useCartContext();
|
|
2926
|
+
const { trigger: updateCartDeliveryOptions2 } = useUpdateCartDeliveryOptions(
|
|
2927
|
+
mutateCart,
|
|
2928
|
+
metafieldIdentifiers
|
|
2929
|
+
);
|
|
2930
|
+
const handler = useCallback(
|
|
2931
|
+
async (_, { arg }) => {
|
|
2932
|
+
const currentCart = arg?.cart || cartContextData;
|
|
2933
|
+
const { deliveryData } = arg;
|
|
2934
|
+
const firstDeliveryGroup = currentCart?.deliveryGroups?.[0];
|
|
2935
|
+
const deliveryGroupId = firstDeliveryGroup?.id;
|
|
2936
|
+
const selectedOptionCode = deliveryData?.deliveryCustomData?.selected_delivery_option?.code;
|
|
2937
|
+
if (!deliveryGroupId || !selectedOptionCode || selectedOptionCode === firstDeliveryGroup?.selectedDeliveryOption?.code) {
|
|
2938
|
+
return null;
|
|
2939
|
+
}
|
|
2940
|
+
const deliveryGroup = currentCart?.deliveryGroups?.find(
|
|
2941
|
+
(group) => group?.id === deliveryGroupId
|
|
2942
|
+
);
|
|
2943
|
+
const matchedOption = deliveryGroup?.deliveryOptions?.find(
|
|
2944
|
+
(option) => option?.code === selectedOptionCode
|
|
2945
|
+
);
|
|
2946
|
+
if (!matchedOption?.handle) {
|
|
2947
|
+
return null;
|
|
2948
|
+
}
|
|
2949
|
+
const deliveryOptions = [
|
|
2950
|
+
{
|
|
2951
|
+
deliveryGroupId,
|
|
2952
|
+
deliveryOptionHandle: matchedOption.handle
|
|
2953
|
+
}
|
|
2954
|
+
];
|
|
2955
|
+
const updatedCart = await updateCartDeliveryOptions2({
|
|
2956
|
+
selectedDeliveryOptions: deliveryOptions,
|
|
2957
|
+
cartId: currentCart?.id
|
|
2958
|
+
});
|
|
2959
|
+
if (updatedCart && mutateCart) {
|
|
2960
|
+
mutateCart(updatedCart);
|
|
2961
|
+
}
|
|
2962
|
+
return updatedCart;
|
|
2963
|
+
},
|
|
2964
|
+
[cartContextData, updateCartDeliveryOptions2, mutateCart]
|
|
2965
|
+
);
|
|
2966
|
+
return useSWRMutation("update-cart-delivery-options", handler, options);
|
|
2967
|
+
};
|
|
2629
2968
|
var usePlusMemberItemCustomAttributes = ({
|
|
2630
2969
|
deliveryData
|
|
2631
2970
|
}) => {
|
|
@@ -2645,48 +2984,18 @@ var usePlusMemberCheckoutCustomAttributes = ({
|
|
|
2645
2984
|
deliveryData,
|
|
2646
2985
|
product,
|
|
2647
2986
|
variant,
|
|
2648
|
-
customer,
|
|
2649
2987
|
isShowShippingBenefits
|
|
2650
2988
|
}) => {
|
|
2651
2989
|
const { deliveryCustomData } = deliveryData || {};
|
|
2652
2990
|
const { profile } = usePlusMemberContext();
|
|
2653
|
-
const userType = useMemo(() => {
|
|
2654
|
-
const customerInfo = customer;
|
|
2655
|
-
if (!customerInfo) {
|
|
2656
|
-
return "new_user_unlogin";
|
|
2657
|
-
}
|
|
2658
|
-
if (customer) {
|
|
2659
|
-
const { orders = {} } = customer;
|
|
2660
|
-
const edgesLength = orders?.edges?.length;
|
|
2661
|
-
if (edgesLength === 1) {
|
|
2662
|
-
return "old_user_orders_once";
|
|
2663
|
-
} else if (edgesLength && edgesLength > 1) {
|
|
2664
|
-
return "old_user_orders_twice";
|
|
2665
|
-
}
|
|
2666
|
-
}
|
|
2667
|
-
return "new_user_login";
|
|
2668
|
-
}, [customer]);
|
|
2669
2991
|
return useMemo(() => {
|
|
2670
2992
|
const checkoutCustomAttributes = [
|
|
2671
|
-
|
|
2672
|
-
key: "_token",
|
|
2673
|
-
value: profile?.token || ""
|
|
2674
|
-
},
|
|
2993
|
+
// _last_url: 付费会员结算完成之后 checkout 有一个继续购买的按钮, 用于跳转到继续购买的页面
|
|
2675
2994
|
{
|
|
2676
2995
|
key: "_last_url",
|
|
2677
2996
|
value: typeof window !== "undefined" ? window.location.origin + window.location.pathname : ""
|
|
2678
|
-
},
|
|
2679
|
-
{
|
|
2680
|
-
key: "_user_type",
|
|
2681
|
-
value: userType
|
|
2682
2997
|
}
|
|
2683
2998
|
];
|
|
2684
|
-
if (profile) {
|
|
2685
|
-
checkoutCustomAttributes.push({
|
|
2686
|
-
key: "_login_user",
|
|
2687
|
-
value: "1"
|
|
2688
|
-
});
|
|
2689
|
-
}
|
|
2690
2999
|
if (deliveryCustomData) {
|
|
2691
3000
|
checkoutCustomAttributes.push({
|
|
2692
3001
|
key: "_checkout_delivery_custom",
|
|
@@ -2696,12 +3005,6 @@ var usePlusMemberCheckoutCustomAttributes = ({
|
|
|
2696
3005
|
})
|
|
2697
3006
|
});
|
|
2698
3007
|
}
|
|
2699
|
-
if (variant?.metafields?.presell) {
|
|
2700
|
-
checkoutCustomAttributes.push({
|
|
2701
|
-
key: "_presale",
|
|
2702
|
-
value: "true"
|
|
2703
|
-
});
|
|
2704
|
-
}
|
|
2705
3008
|
if (isShowShippingBenefits && !isShowShippingBenefits({ variant, product, setting: {} })) {
|
|
2706
3009
|
checkoutCustomAttributes.push({
|
|
2707
3010
|
key: "_hide_shipping",
|
|
@@ -2709,18 +3012,17 @@ var usePlusMemberCheckoutCustomAttributes = ({
|
|
|
2709
3012
|
});
|
|
2710
3013
|
}
|
|
2711
3014
|
return checkoutCustomAttributes;
|
|
2712
|
-
}, [deliveryCustomData, product, profile,
|
|
3015
|
+
}, [deliveryCustomData, product, profile, variant, isShowShippingBenefits]);
|
|
2713
3016
|
};
|
|
2714
3017
|
function useAutoRemovePlusMemberInCart({
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
3018
|
+
cart,
|
|
3019
|
+
profile,
|
|
3020
|
+
memberSetting
|
|
2718
3021
|
}) {
|
|
2719
|
-
const { plus_monthly_product, plus_annual_product } =
|
|
2720
|
-
const { cart } = useCartContext();
|
|
3022
|
+
const { plus_monthly_product, plus_annual_product } = memberSetting || {};
|
|
2721
3023
|
const { trigger: removeCartLines2 } = useRemoveCartLines();
|
|
2722
3024
|
useEffect(() => {
|
|
2723
|
-
if (!cart) return;
|
|
3025
|
+
if (!cart || !plus_monthly_product || !plus_annual_product) return;
|
|
2724
3026
|
const removePlusProduct = async (productType) => {
|
|
2725
3027
|
if (!productType) return;
|
|
2726
3028
|
const product = cart.lineItems?.find(
|
|
@@ -2732,33 +3034,25 @@ function useAutoRemovePlusMemberInCart({
|
|
|
2732
3034
|
});
|
|
2733
3035
|
}
|
|
2734
3036
|
};
|
|
2735
|
-
if (isMonthlyPlus) {
|
|
3037
|
+
if (profile?.isMonthlyPlus) {
|
|
2736
3038
|
removePlusProduct(plus_monthly_product);
|
|
2737
3039
|
}
|
|
2738
|
-
if (isAnnualPlus) {
|
|
3040
|
+
if (profile?.isAnnualPlus) {
|
|
2739
3041
|
removePlusProduct(plus_annual_product);
|
|
2740
3042
|
}
|
|
2741
|
-
}, [
|
|
2742
|
-
cart,
|
|
2743
|
-
plus_annual_product,
|
|
2744
|
-
plus_monthly_product,
|
|
2745
|
-
isAnnualPlus,
|
|
2746
|
-
isMonthlyPlus,
|
|
2747
|
-
removeCartLines2
|
|
2748
|
-
]);
|
|
3043
|
+
}, [cart, plus_annual_product, plus_monthly_product, profile, removeCartLines2]);
|
|
2749
3044
|
}
|
|
2750
|
-
function
|
|
3045
|
+
function usePlusMemberNeedAddToCart({
|
|
2751
3046
|
cart,
|
|
2752
|
-
|
|
2753
|
-
selectedPlusMemberMode,
|
|
2754
|
-
selectedPlusMemberProduct
|
|
3047
|
+
profile
|
|
2755
3048
|
}) {
|
|
3049
|
+
const { selectedPlusMemberMode, selectedPlusMemberProduct, plusMemberMetafields } = usePlusMemberContext();
|
|
2756
3050
|
const { hasMonthlyPlus, hasAnnualPlus } = useHasPlusMemberInCart({
|
|
2757
|
-
|
|
2758
|
-
|
|
3051
|
+
memberSetting: plusMemberMetafields,
|
|
3052
|
+
cart
|
|
2759
3053
|
});
|
|
2760
3054
|
const plusMemberProduct = useMemo(() => {
|
|
2761
|
-
if (selectedPlusMemberMode === "free" /* FREE */) {
|
|
3055
|
+
if (!selectedPlusMemberProduct || selectedPlusMemberMode === "free" /* FREE */) {
|
|
2762
3056
|
return void 0;
|
|
2763
3057
|
}
|
|
2764
3058
|
if (selectedPlusMemberMode === "monthly" /* MONTHLY */ && hasMonthlyPlus) {
|
|
@@ -2767,7 +3061,10 @@ function useAddPlusMemberProductsToCart({
|
|
|
2767
3061
|
if (selectedPlusMemberMode === "annual" /* ANNUAL */ && hasAnnualPlus) {
|
|
2768
3062
|
return void 0;
|
|
2769
3063
|
}
|
|
2770
|
-
if (
|
|
3064
|
+
if (profile?.isMonthlyPlus && selectedPlusMemberMode === "monthly" /* MONTHLY */) {
|
|
3065
|
+
return void 0;
|
|
3066
|
+
}
|
|
3067
|
+
if (profile?.isAnnualPlus && selectedPlusMemberMode === "annual" /* ANNUAL */) {
|
|
2771
3068
|
return void 0;
|
|
2772
3069
|
}
|
|
2773
3070
|
return selectedPlusMemberProduct;
|
|
@@ -2786,9 +3083,9 @@ var PlusMemberProvider = ({
|
|
|
2786
3083
|
memberSetting,
|
|
2787
3084
|
initialSelectedPlusMemberMode = "free",
|
|
2788
3085
|
profile,
|
|
2789
|
-
locale,
|
|
2790
3086
|
children
|
|
2791
3087
|
}) => {
|
|
3088
|
+
const { locale } = useShopify();
|
|
2792
3089
|
const [zipCode, setZipCode] = useState("");
|
|
2793
3090
|
const [showTip, setShowTip] = useState(false);
|
|
2794
3091
|
const [selectedPlusMemberMode, setSelectedPlusMemberMode] = useState(
|
|
@@ -2804,7 +3101,11 @@ var PlusMemberProvider = ({
|
|
|
2804
3101
|
const shippingMethodsContext = useShippingMethods({
|
|
2805
3102
|
variant,
|
|
2806
3103
|
plusMemberMetafields: memberSetting,
|
|
2807
|
-
selectedPlusMemberMode
|
|
3104
|
+
selectedPlusMemberMode,
|
|
3105
|
+
profile,
|
|
3106
|
+
isPlus: profile?.isPlus || false
|
|
3107
|
+
});
|
|
3108
|
+
console.log("shippingMethodsContext", shippingMethodsContext);
|
|
2808
3109
|
const plusMemberHandles = useMemo(() => {
|
|
2809
3110
|
return [
|
|
2810
3111
|
memberSetting?.plus_monthly_product?.handle,
|
|
@@ -3086,8 +3387,13 @@ function CartProvider({
|
|
|
3086
3387
|
const { attributes } = useCartAttributes({ profile, customer, cart, memberSetting });
|
|
3087
3388
|
useRequest(
|
|
3088
3389
|
() => {
|
|
3089
|
-
const newAttributes = [...attributes
|
|
3090
|
-
|
|
3390
|
+
const newAttributes = [...attributes];
|
|
3391
|
+
customAttributes.forEach((item) => {
|
|
3392
|
+
if (item.value && !newAttributes.some((attr) => attr.key === item.key)) {
|
|
3393
|
+
newAttributes.push(item);
|
|
3394
|
+
}
|
|
3395
|
+
});
|
|
3396
|
+
const needUpdate = cart && checkAttributesUpdateNeeded(
|
|
3091
3397
|
cart.customAttributes,
|
|
3092
3398
|
newAttributes,
|
|
3093
3399
|
customAttributesNeedDelete
|
|
@@ -3110,7 +3416,8 @@ function CartProvider({
|
|
|
3110
3416
|
cart,
|
|
3111
3417
|
mutateCart,
|
|
3112
3418
|
isCartLoading: isCartLoading || isCodeChanging,
|
|
3113
|
-
setLoadingState
|
|
3419
|
+
setLoadingState,
|
|
3420
|
+
metafieldIdentifiers
|
|
3114
3421
|
});
|
|
3115
3422
|
const removeCustomAttributes = useCallback(
|
|
3116
3423
|
(attributes2) => {
|
|
@@ -3191,8 +3498,14 @@ function CartProvider({
|
|
|
3191
3498
|
);
|
|
3192
3499
|
return result;
|
|
3193
3500
|
}, [cart?.lineItems, scriptAutoFreeGift, functionAutoFreeGift]);
|
|
3501
|
+
const totalQuantity = useMemo(() => {
|
|
3502
|
+
const cartLinesCount = cart?.lineItems.reduce((acc, item) => acc + item.quantity, 0) || 0;
|
|
3503
|
+
const giftLinesCount = giftNeedAddToCartLines?.reduce((acc, item) => acc + (item.quantity || 1), 0) || 0;
|
|
3504
|
+
return cartLinesCount + giftLinesCount;
|
|
3505
|
+
}, [cart?.lineItems, giftNeedAddToCartLines]);
|
|
3194
3506
|
const value = useMemo(
|
|
3195
3507
|
() => ({
|
|
3508
|
+
totalQuantity,
|
|
3196
3509
|
cart,
|
|
3197
3510
|
isCartLoading,
|
|
3198
3511
|
triggerFetch: fetchCart,
|
|
@@ -3220,6 +3533,7 @@ function CartProvider({
|
|
|
3220
3533
|
}),
|
|
3221
3534
|
[
|
|
3222
3535
|
cart,
|
|
3536
|
+
totalQuantity,
|
|
3223
3537
|
isCartLoading,
|
|
3224
3538
|
fetchCart,
|
|
3225
3539
|
mutateCart,
|
|
@@ -3244,14 +3558,14 @@ function CartProvider({
|
|
|
3244
3558
|
);
|
|
3245
3559
|
return /* @__PURE__ */ jsx(CartContext.Provider, { value, children });
|
|
3246
3560
|
}
|
|
3247
|
-
function useCartContext() {
|
|
3561
|
+
function useCartContext(options) {
|
|
3248
3562
|
const context = useContext(CartContext);
|
|
3249
|
-
if (!context) {
|
|
3563
|
+
if (!context && !options?.optional) {
|
|
3250
3564
|
throw new Error("useCartContext must be used within a CartProvider");
|
|
3251
3565
|
}
|
|
3252
3566
|
return context;
|
|
3253
3567
|
}
|
|
3254
3568
|
|
|
3255
|
-
export { BuyRuleType, CODE_AMOUNT_KEY, CUSTOMER_ATTRIBUTE_KEY, CUSTOMER_SCRIPT_GIFT_KEY, CartProvider, DeliveryPlusType, MAIN_PRODUCT_CODE, OrderBasePriceType, OrderDiscountType, PLUS_MEMBER_TYPE, PlusMemberContext, PlusMemberMode, PlusMemberProvider, PriceBasePriceType, PriceDiscountType, RuleType, SCRIPT_CODE_AMOUNT_KEY, ShippingMethodMode, ShopifyContext, ShopifyProvider, SpendMoneyType,
|
|
3569
|
+
export { BrowserPerformanceAdapter, BuyRuleType, CODE_AMOUNT_KEY, CUSTOMER_ATTRIBUTE_KEY, CUSTOMER_SCRIPT_GIFT_KEY, CartProvider, DeliveryPlusType, MAIN_PRODUCT_CODE, MEMBER_PRICE_ATTRIBUTE_KEY, OrderBasePriceType, OrderDiscountType, PLUS_MEMBER_TYPE, PlusMemberContext, PlusMemberMode, PlusMemberProvider, PriceBasePriceType, PriceDiscountType, RuleType, SCRIPT_CODE_AMOUNT_KEY, ShippingMethodMode, ShopifyContext, ShopifyProvider, SpendMoneyType, browserCartCookieAdapter, browserCookieAdapter, checkAttributesUpdateNeeded, clearGeoLocationCache, createMockCartFromLines, currencyCodeMapping, defaultSWRMutationConfiguration, formatFunctionAutoFreeGift, formatScriptAutoFreeGift, gaTrack, getCachedGeoLocation, getDiscountEnvAttributeValue, getMatchedMainProductSubTotal, getQuery, getReferralAttributes, normalizeAddToCartLines, preCheck, safeParse, trackAddToCartFBQ, trackAddToCartGA, trackBeginCheckoutGA, trackBuyNowFBQ, trackBuyNowGA, useAddCartLines, useAddToCart, useAllBlogs, useAllCollections, useAllProducts, useApplyCartCodes, useArticle, useArticles, useArticlesInBlog, useAutoRemovePlusMemberInCart, useAvailableDeliveryCoupon, useBlog, useBuyNow, useCalcAutoFreeGift, useCalcGiftsFromLines, useCalcOrderDiscount, useCartAttributes, useCartContext, useCartItemQuantityLimit, useCollection, useCollections, useCreateCart, useExposure, useGeoLocation, useHasPlusMemberInCart, useIntersection, usePlusAnnualProductVariant, usePlusMemberCheckoutCustomAttributes, usePlusMemberContext, usePlusMemberDeliveryCodes, usePlusMemberItemCustomAttributes, usePlusMemberNeedAddToCart, usePlusMonthlyProductVariant, usePrice, useProduct, useProductUrl, useProductsByHandles, useRemoveCartCodes, useRemoveCartLines, useReplaceCartPlusMember, useScriptAutoFreeGift, useSearch, useSelectedOptions, useShippingMethodAvailableCheck, useShippingMethods, useShopify, useSite, useUpdateCartAttributes, useUpdateCartLines, useUpdateLineCodeAmountAttributes, useUpdatePlusMemberDeliveryOptions, useUpdateVariantQuery, useVariant, useVariantMedia };
|
|
3256
3570
|
//# sourceMappingURL=index.mjs.map
|
|
3257
3571
|
//# sourceMappingURL=index.mjs.map
|