@anker-in/shopify-react 0.1.1-beta.2 → 0.1.1-beta.21
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 +2 -2
- package/dist/adapters/index.d.ts +2 -2
- package/dist/hooks/index.d.mts +1899 -9
- package/dist/hooks/index.d.ts +1899 -9
- package/dist/hooks/index.js +387 -146
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +383 -144
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/index.d.mts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +409 -172
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +400 -147
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +9 -9
- package/dist/provider/index.d.ts +9 -9
- package/dist/provider/index.js +232 -76
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +231 -75
- package/dist/provider/index.mjs.map +1 -1
- package/dist/{types-CICUnw0v.d.mts → types-C1So3siD.d.mts} +6 -18
- package/dist/{types-CICUnw0v.d.ts → types-C1So3siD.d.ts} +6 -18
- package/dist/{types-BLMoxbOk.d.mts → types-Dt0DUG00.d.mts} +1 -9
- package/dist/{types-BLMoxbOk.d.ts → types-Dt0DUG00.d.ts} +1 -9
- package/package.json +3 -3
- package/dist/index-BZ6WbAdZ.d.ts +0 -1889
- package/dist/index-Bea95u2X.d.mts +0 -1889
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import React__default from 'react';
|
|
3
|
-
import { ShopifyClient, ShopifyConfig, CartCookieAdapter
|
|
4
|
-
import { C as CookieAdapter,
|
|
3
|
+
import { ShopifyClient, ShopifyConfig, CartCookieAdapter, NormalizedCart, AttributeInput, HasMetafieldsIdentifier } from '@anker-in/shopify-sdk';
|
|
4
|
+
import { C as CookieAdapter, R as RouterAdapter, U as UserContextAdapter } from '../types-Dt0DUG00.mjs';
|
|
5
5
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
6
|
import { SWRConfiguration } from 'swr';
|
|
7
|
-
import { e as PlusMemberSettingsMetafields } from '../types-
|
|
7
|
+
import { e as PlusMemberSettingsMetafields } from '../types-C1So3siD.mjs';
|
|
8
8
|
import 'swr/mutation';
|
|
9
9
|
|
|
10
10
|
interface ShopifyContextValue {
|
|
@@ -24,7 +24,7 @@ interface ShopifyProviderProps {
|
|
|
24
24
|
locale: string;
|
|
25
25
|
locales?: string[];
|
|
26
26
|
cookieAdapter?: CookieAdapter;
|
|
27
|
-
cartCookieAdapter?: CartCookieAdapter
|
|
27
|
+
cartCookieAdapter?: CartCookieAdapter;
|
|
28
28
|
routerAdapter?: RouterAdapter;
|
|
29
29
|
userAdapter?: UserContextAdapter;
|
|
30
30
|
children: React__default.ReactNode;
|
|
@@ -41,10 +41,6 @@ declare function ShopifyProvider({ config, locale, locales, cookieAdapter, cartC
|
|
|
41
41
|
*/
|
|
42
42
|
declare function useShopify(): ShopifyContextValue;
|
|
43
43
|
|
|
44
|
-
type AttributeInput = {
|
|
45
|
-
key: string;
|
|
46
|
-
value: string;
|
|
47
|
-
};
|
|
48
44
|
type LoadingState = {
|
|
49
45
|
editLineQuantityLoading: boolean;
|
|
50
46
|
editLineCodeAmountLoading: boolean;
|
|
@@ -54,6 +50,8 @@ type LoadingState = {
|
|
|
54
50
|
interface CartContextValue {
|
|
55
51
|
/** Current cart data */
|
|
56
52
|
cart: NormalizedCart | undefined;
|
|
53
|
+
/** Total quantity of items in cart */
|
|
54
|
+
totalQuantity: number;
|
|
57
55
|
/** Whether cart is loading */
|
|
58
56
|
isCartLoading: boolean;
|
|
59
57
|
/** Manually trigger cart fetch */
|
|
@@ -96,6 +94,8 @@ interface CartContextValue {
|
|
|
96
94
|
giftNeedAddToCartLines: any[];
|
|
97
95
|
/** Auto free gift config */
|
|
98
96
|
autoFreeGiftConfig?: any;
|
|
97
|
+
/** Gradient gifts config */
|
|
98
|
+
gradientGiftsConfig?: any;
|
|
99
99
|
/** Metafield identifiers */
|
|
100
100
|
metafieldIdentifiers?: {
|
|
101
101
|
variant: HasMetafieldsIdentifier[];
|
|
@@ -159,4 +159,4 @@ declare function CartProvider({ children, autoFreeGiftConfig, gradientGiftsConfi
|
|
|
159
159
|
*/
|
|
160
160
|
declare function useCartContext(): CartContextValue;
|
|
161
161
|
|
|
162
|
-
export { type
|
|
162
|
+
export { type CartContextValue, CartProvider, type CartProviderProps, type LoadingState, ShopifyContext, type ShopifyContextValue, ShopifyProvider, type ShopifyProviderProps, useCartContext, useShopify };
|
package/dist/provider/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import React__default from 'react';
|
|
3
|
-
import { ShopifyClient, ShopifyConfig, CartCookieAdapter
|
|
4
|
-
import { C as CookieAdapter,
|
|
3
|
+
import { ShopifyClient, ShopifyConfig, CartCookieAdapter, NormalizedCart, AttributeInput, HasMetafieldsIdentifier } from '@anker-in/shopify-sdk';
|
|
4
|
+
import { C as CookieAdapter, R as RouterAdapter, U as UserContextAdapter } from '../types-Dt0DUG00.js';
|
|
5
5
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
6
|
import { SWRConfiguration } from 'swr';
|
|
7
|
-
import { e as PlusMemberSettingsMetafields } from '../types-
|
|
7
|
+
import { e as PlusMemberSettingsMetafields } from '../types-C1So3siD.js';
|
|
8
8
|
import 'swr/mutation';
|
|
9
9
|
|
|
10
10
|
interface ShopifyContextValue {
|
|
@@ -24,7 +24,7 @@ interface ShopifyProviderProps {
|
|
|
24
24
|
locale: string;
|
|
25
25
|
locales?: string[];
|
|
26
26
|
cookieAdapter?: CookieAdapter;
|
|
27
|
-
cartCookieAdapter?: CartCookieAdapter
|
|
27
|
+
cartCookieAdapter?: CartCookieAdapter;
|
|
28
28
|
routerAdapter?: RouterAdapter;
|
|
29
29
|
userAdapter?: UserContextAdapter;
|
|
30
30
|
children: React__default.ReactNode;
|
|
@@ -41,10 +41,6 @@ declare function ShopifyProvider({ config, locale, locales, cookieAdapter, cartC
|
|
|
41
41
|
*/
|
|
42
42
|
declare function useShopify(): ShopifyContextValue;
|
|
43
43
|
|
|
44
|
-
type AttributeInput = {
|
|
45
|
-
key: string;
|
|
46
|
-
value: string;
|
|
47
|
-
};
|
|
48
44
|
type LoadingState = {
|
|
49
45
|
editLineQuantityLoading: boolean;
|
|
50
46
|
editLineCodeAmountLoading: boolean;
|
|
@@ -54,6 +50,8 @@ type LoadingState = {
|
|
|
54
50
|
interface CartContextValue {
|
|
55
51
|
/** Current cart data */
|
|
56
52
|
cart: NormalizedCart | undefined;
|
|
53
|
+
/** Total quantity of items in cart */
|
|
54
|
+
totalQuantity: number;
|
|
57
55
|
/** Whether cart is loading */
|
|
58
56
|
isCartLoading: boolean;
|
|
59
57
|
/** Manually trigger cart fetch */
|
|
@@ -96,6 +94,8 @@ interface CartContextValue {
|
|
|
96
94
|
giftNeedAddToCartLines: any[];
|
|
97
95
|
/** Auto free gift config */
|
|
98
96
|
autoFreeGiftConfig?: any;
|
|
97
|
+
/** Gradient gifts config */
|
|
98
|
+
gradientGiftsConfig?: any;
|
|
99
99
|
/** Metafield identifiers */
|
|
100
100
|
metafieldIdentifiers?: {
|
|
101
101
|
variant: HasMetafieldsIdentifier[];
|
|
@@ -159,4 +159,4 @@ declare function CartProvider({ children, autoFreeGiftConfig, gradientGiftsConfi
|
|
|
159
159
|
*/
|
|
160
160
|
declare function useCartContext(): CartContextValue;
|
|
161
161
|
|
|
162
|
-
export { type
|
|
162
|
+
export { type CartContextValue, CartProvider, type CartProviderProps, type LoadingState, ShopifyContext, type ShopifyContextValue, ShopifyProvider, type ShopifyProviderProps, useCartContext, useShopify };
|
package/dist/provider/index.js
CHANGED
|
@@ -104,6 +104,83 @@ var CODE_AMOUNT_KEY = "_sku_code_money";
|
|
|
104
104
|
var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
|
|
105
105
|
var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
|
|
106
106
|
|
|
107
|
+
// src/hooks/cart/utils/normalize-add-to-cart-lines.ts
|
|
108
|
+
function normalizeAddToCartLines(lines) {
|
|
109
|
+
return lines.filter((line) => line.variant?.id).map((line, index) => {
|
|
110
|
+
const variant = line.variant;
|
|
111
|
+
const product = variant.product;
|
|
112
|
+
const quantity = line.quantity || 1;
|
|
113
|
+
const price = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
|
|
114
|
+
const subtotalAmount = price * quantity;
|
|
115
|
+
const totalAmount = subtotalAmount;
|
|
116
|
+
return {
|
|
117
|
+
id: `temp-line-${index}-${variant.id}`,
|
|
118
|
+
// Temporary ID for pre-cart lines
|
|
119
|
+
name: product?.title || variant.title || "",
|
|
120
|
+
quantity,
|
|
121
|
+
variantId: variant.id,
|
|
122
|
+
productId: product?.id || variant.id.split("/").slice(0, -2).join("/"),
|
|
123
|
+
totalAmount,
|
|
124
|
+
subtotalAmount,
|
|
125
|
+
discountAllocations: [],
|
|
126
|
+
customAttributes: line.attributes || [],
|
|
127
|
+
variant: {
|
|
128
|
+
id: variant.id,
|
|
129
|
+
price,
|
|
130
|
+
listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
|
|
131
|
+
sku: variant.sku || "",
|
|
132
|
+
name: variant.title || "",
|
|
133
|
+
image: variant.image ? {
|
|
134
|
+
url: variant.image.url,
|
|
135
|
+
altText: variant.image.altText || void 0
|
|
136
|
+
} : void 0,
|
|
137
|
+
requiresShipping: false,
|
|
138
|
+
// Default value, not available in NormalizedProductVariant
|
|
139
|
+
availableForSale: variant.availableForSale ?? true,
|
|
140
|
+
quantityAvailable: variant.quantityAvailable ?? 0,
|
|
141
|
+
currentlyNotInStock: false,
|
|
142
|
+
// Default value, will be updated when added to cart
|
|
143
|
+
weight: variant.weight,
|
|
144
|
+
metafields: variant.metafields
|
|
145
|
+
},
|
|
146
|
+
product,
|
|
147
|
+
path: product?.handle ? `/products/${product.handle}` : "",
|
|
148
|
+
discounts: [],
|
|
149
|
+
options: variant.selectedOptions?.map((opt) => ({
|
|
150
|
+
name: opt.name,
|
|
151
|
+
value: opt.value
|
|
152
|
+
}))
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function createMockCartFromLines(lines, existingCart) {
|
|
157
|
+
const normalizedLines = normalizeAddToCartLines(lines);
|
|
158
|
+
const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
|
|
159
|
+
const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
|
|
160
|
+
console.log("lines createMockCartFromLines", lines);
|
|
161
|
+
const currency = lines[0]?.variant?.price?.currencyCode;
|
|
162
|
+
return {
|
|
163
|
+
id: existingCart?.id || "temp-cart-id",
|
|
164
|
+
customerId: existingCart?.customerId,
|
|
165
|
+
email: existingCart?.email,
|
|
166
|
+
createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
167
|
+
currency: existingCart?.currency?.code || { code: currency },
|
|
168
|
+
taxesIncluded: existingCart?.taxesIncluded,
|
|
169
|
+
lineItems: normalizedLines,
|
|
170
|
+
totalLineItemsDiscount: 0,
|
|
171
|
+
orderDiscounts: 0,
|
|
172
|
+
lineItemsSubtotalPrice: subtotalPrice,
|
|
173
|
+
subtotalPrice,
|
|
174
|
+
totalPrice,
|
|
175
|
+
totalTaxAmount: 0,
|
|
176
|
+
discountCodes: existingCart?.discountCodes || [],
|
|
177
|
+
discountAllocations: [],
|
|
178
|
+
url: existingCart?.url || "",
|
|
179
|
+
ready: true,
|
|
180
|
+
customAttributes: existingCart?.customAttributes
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
107
184
|
// src/hooks/cart/utils/index.ts
|
|
108
185
|
var getQuery = () => {
|
|
109
186
|
const url = typeof window !== "undefined" ? window.location.search : "";
|
|
@@ -121,22 +198,12 @@ var getQuery = () => {
|
|
|
121
198
|
}
|
|
122
199
|
return theRequest;
|
|
123
200
|
};
|
|
124
|
-
function atobID(id) {
|
|
125
|
-
if (id && typeof id === "string" && id.includes("/")) {
|
|
126
|
-
return id.split("/").pop()?.split("?")?.shift();
|
|
127
|
-
} else {
|
|
128
|
-
return id;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
function btoaID(id, type = "ProductVariant") {
|
|
132
|
-
return `gid://shopify/${type}/${id}`;
|
|
133
|
-
}
|
|
134
201
|
var getMatchedMainProductSubTotal = (cartData, variant_list, main_product) => {
|
|
135
202
|
const isAllStoreVariant = main_product?.all_store_variant ?? false;
|
|
136
203
|
const matchedList = cartData?.lineItems?.filter((line) => {
|
|
137
204
|
const { is_gift } = getDiscountEnvAttributeValue(line.customAttributes);
|
|
138
205
|
return isAllStoreVariant ? !is_gift : variant_list?.find((item) => {
|
|
139
|
-
return !is_gift && atobID(line.variantId) === item;
|
|
206
|
+
return !is_gift && shopifySdk.atobID(line.variantId) === item;
|
|
140
207
|
});
|
|
141
208
|
});
|
|
142
209
|
return matchedList?.reduce((acc, line) => {
|
|
@@ -328,12 +395,15 @@ var formatFunctionAutoFreeGift = ({
|
|
|
328
395
|
};
|
|
329
396
|
return result;
|
|
330
397
|
};
|
|
331
|
-
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
398
|
+
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
332
399
|
const tags = react.useMemo(() => customer?.tags || [], [customer?.tags]);
|
|
333
400
|
const isCustomerLoading = react.useMemo(() => !customer ? true : false, [customer]);
|
|
334
401
|
const dealsType = "";
|
|
335
402
|
const { client, locale } = useShopify();
|
|
336
403
|
const giftProductsCache = react.useRef(null);
|
|
404
|
+
const effectiveCart = react.useMemo(() => {
|
|
405
|
+
return cart;
|
|
406
|
+
}, [lines, cart]);
|
|
337
407
|
const { activeCampaign, subtotal } = react.useMemo(() => {
|
|
338
408
|
for (const campaign of autoFreeGiftConfig) {
|
|
339
409
|
const { rule_conditions = [], rule_result } = campaign;
|
|
@@ -341,7 +411,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
341
411
|
const isPreCheckPassed = preCheck(rule_conditions, tags, []);
|
|
342
412
|
if (isPreCheckPassed && spend_get_reward) {
|
|
343
413
|
const matchedSubtotal = getMatchedMainProductSubTotal(
|
|
344
|
-
|
|
414
|
+
effectiveCart,
|
|
345
415
|
spend_get_reward.main_product?.variant_list?.map((v) => v.variant_id) || [],
|
|
346
416
|
{
|
|
347
417
|
spend_money_type: spend_get_reward.main_product?.spend_money_type || 1,
|
|
@@ -355,17 +425,26 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
355
425
|
}
|
|
356
426
|
}
|
|
357
427
|
return { activeCampaign: null, subtotal: 0 };
|
|
358
|
-
}, [autoFreeGiftConfig,
|
|
428
|
+
}, [autoFreeGiftConfig, effectiveCart, tags, dealsType]);
|
|
359
429
|
const { qualifyingGift, nextTierGoal } = react.useMemo(() => {
|
|
360
430
|
if (!activeCampaign || !activeCampaign.rule_result?.spend_get_reward?.gift_product) {
|
|
361
431
|
return { qualifyingGift: null, nextTierGoal: null };
|
|
362
432
|
}
|
|
363
433
|
const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
|
|
364
|
-
const
|
|
365
|
-
|
|
434
|
+
const currentCurrency = effectiveCart?.currency?.code || "";
|
|
435
|
+
console.log("currentCurrency useCalcAutoFreeGift", effectiveCart, currentCurrency);
|
|
436
|
+
const getThresholdAmount = (tier) => {
|
|
437
|
+
if (tier.spend_sum_money_multi_markets?.[currentCurrency]?.value) {
|
|
438
|
+
return Number(tier.spend_sum_money_multi_markets[currentCurrency].value);
|
|
439
|
+
}
|
|
440
|
+
return Number(tier.spend_sum_money || 0);
|
|
441
|
+
};
|
|
442
|
+
const qualifyingTier = [...giftTiers].sort((a, b) => getThresholdAmount(b) - getThresholdAmount(a)).find((tier) => subtotal >= getThresholdAmount(tier));
|
|
443
|
+
const nextGoal = giftTiers.find((tier) => subtotal < getThresholdAmount(tier));
|
|
366
444
|
if (!qualifyingTier) {
|
|
367
445
|
return { qualifyingGift: null, nextTierGoal: nextGoal || null };
|
|
368
446
|
}
|
|
447
|
+
const actualThreshold = getThresholdAmount(qualifyingTier);
|
|
369
448
|
const formattedGift = {
|
|
370
449
|
tier: qualifyingTier,
|
|
371
450
|
itemsToAdd: qualifyingTier.reward_list?.map((reward) => {
|
|
@@ -373,7 +452,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
373
452
|
if (!giftProduct) return null;
|
|
374
453
|
return {
|
|
375
454
|
variant: {
|
|
376
|
-
id: btoaID(giftProduct.variant_id),
|
|
455
|
+
id: shopifySdk.btoaID(giftProduct.variant_id),
|
|
377
456
|
handle: giftProduct.handle,
|
|
378
457
|
sku: giftProduct.sku
|
|
379
458
|
},
|
|
@@ -384,7 +463,10 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
384
463
|
value: JSON.stringify({
|
|
385
464
|
is_gift: true,
|
|
386
465
|
rule_id: activeCampaign.rule_id,
|
|
387
|
-
spend_sum_money:
|
|
466
|
+
spend_sum_money: actualThreshold,
|
|
467
|
+
// 使用实际的门槛金额(多币种支持)
|
|
468
|
+
currency_code: currentCurrency
|
|
469
|
+
// 记录当前币种
|
|
388
470
|
})
|
|
389
471
|
}
|
|
390
472
|
]
|
|
@@ -392,7 +474,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
392
474
|
}).filter((item) => item !== null)
|
|
393
475
|
};
|
|
394
476
|
return { qualifyingGift: formattedGift, nextTierGoal: nextGoal || null };
|
|
395
|
-
}, [activeCampaign, subtotal]);
|
|
477
|
+
}, [activeCampaign, subtotal, effectiveCart]);
|
|
396
478
|
const giftHandles = react.useMemo(() => {
|
|
397
479
|
const giftVariant = autoFreeGiftConfig.map(
|
|
398
480
|
(item) => item.rule_result?.spend_get_reward?.gift_product?.map(
|
|
@@ -408,18 +490,24 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
408
490
|
}
|
|
409
491
|
return true;
|
|
410
492
|
}, [giftHandles]);
|
|
411
|
-
const { data: giftProductsResult } = useSWR__default.default(
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
493
|
+
const { data: giftProductsResult } = useSWR__default.default(
|
|
494
|
+
shouldFetch ? giftHandles : null,
|
|
495
|
+
async () => {
|
|
496
|
+
const res = await shopifySdk.getProductsByHandles(client, {
|
|
497
|
+
handles: giftHandles,
|
|
498
|
+
locale
|
|
499
|
+
});
|
|
500
|
+
const result = Array.isArray(res) ? res : [];
|
|
501
|
+
giftProductsCache.current = {
|
|
502
|
+
data: result,
|
|
503
|
+
giftHandles: [...giftHandles]
|
|
504
|
+
};
|
|
505
|
+
return result;
|
|
506
|
+
},
|
|
507
|
+
{
|
|
508
|
+
revalidateOnFocus: false
|
|
509
|
+
}
|
|
510
|
+
);
|
|
423
511
|
const finalGiftProductsResult = react.useMemo(() => {
|
|
424
512
|
if (giftProductsCache.current && !shouldFetch) {
|
|
425
513
|
return giftProductsCache.current.data || void 0;
|
|
@@ -438,12 +526,19 @@ var useScriptAutoFreeGift = ({
|
|
|
438
526
|
campaign,
|
|
439
527
|
_giveaway,
|
|
440
528
|
cart,
|
|
441
|
-
locale: providedLocale
|
|
529
|
+
locale: providedLocale,
|
|
530
|
+
lines
|
|
442
531
|
}) => {
|
|
443
532
|
const { client, locale: contextLocale } = useShopify();
|
|
444
533
|
const locale = providedLocale || contextLocale;
|
|
445
534
|
const [points_subscribe, set_points_subscribe] = react.useState(false);
|
|
446
535
|
const giftProductsCache = react.useRef(null);
|
|
536
|
+
const effectiveCart = react.useMemo(() => {
|
|
537
|
+
if (lines && lines.length > 0) {
|
|
538
|
+
return createMockCartFromLines(lines, cart);
|
|
539
|
+
}
|
|
540
|
+
return cart;
|
|
541
|
+
}, [lines, cart]);
|
|
447
542
|
react.useEffect(() => {
|
|
448
543
|
if (locale === "au") {
|
|
449
544
|
const isPointsSubscribe = Cookies5__default.default.get("points_subscribe");
|
|
@@ -465,14 +560,16 @@ var useScriptAutoFreeGift = ({
|
|
|
465
560
|
upgrade_multiple2 = 1.2;
|
|
466
561
|
upgrade_value2 = 40;
|
|
467
562
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
563
|
+
effectiveCart?.lineItems?.forEach(
|
|
564
|
+
({ customAttributes }) => {
|
|
565
|
+
customAttributes?.forEach(({ key, value }) => {
|
|
566
|
+
if (key === "_amount_upgrade_multiple") upgrade_multiple2 = Number(value) || 1;
|
|
567
|
+
if (key === "_amount_upgrade_value") upgrade_value2 = Number(value) || 0;
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
);
|
|
474
571
|
return [upgrade_multiple2, upgrade_value2];
|
|
475
|
-
}, [
|
|
572
|
+
}, [effectiveCart?.lineItems, points_subscribe]);
|
|
476
573
|
const breakpoints = react.useMemo(() => {
|
|
477
574
|
if (!isActivityAvailable) return [];
|
|
478
575
|
return (campaign?.breakpoints || []).map((item) => ({
|
|
@@ -500,7 +597,7 @@ var useScriptAutoFreeGift = ({
|
|
|
500
597
|
}, [giftHandles]);
|
|
501
598
|
const involvedLines = react.useMemo(() => {
|
|
502
599
|
if (!isActivityAvailable) return [];
|
|
503
|
-
return (
|
|
600
|
+
return (effectiveCart?.lineItems || []).filter((line) => {
|
|
504
601
|
const isNotGift = line?.totalAmount && Number(line.totalAmount) > 0 && line.customAttributes?.every(
|
|
505
602
|
(item) => item.key !== _giveaway
|
|
506
603
|
);
|
|
@@ -509,7 +606,7 @@ var useScriptAutoFreeGift = ({
|
|
|
509
606
|
);
|
|
510
607
|
return isNotGift && hasCampaignTag;
|
|
511
608
|
});
|
|
512
|
-
}, [
|
|
609
|
+
}, [effectiveCart?.lineItems, isActivityAvailable, _giveaway]);
|
|
513
610
|
const involvedSubTotal = react.useMemo(() => {
|
|
514
611
|
if (!isActivityAvailable) return new Decimal2__default.default(0);
|
|
515
612
|
return involvedLines.reduce((prev, item) => {
|
|
@@ -535,18 +632,24 @@ var useScriptAutoFreeGift = ({
|
|
|
535
632
|
const nextLevel = levelIndex > 0 ? sortedLevels[levelIndex - 1] ?? null : null;
|
|
536
633
|
return [currentLevel, nextLevel];
|
|
537
634
|
}, [breakpoints, involvedSubTotal, involvedLines.length]);
|
|
538
|
-
const { data: giftProductsResult } = useSWR__default.default(
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
635
|
+
const { data: giftProductsResult } = useSWR__default.default(
|
|
636
|
+
shouldFetch ? giftHandles : null,
|
|
637
|
+
async () => {
|
|
638
|
+
const res = await shopifySdk.getProductsByHandles(client, {
|
|
639
|
+
handles: giftHandles,
|
|
640
|
+
locale
|
|
641
|
+
});
|
|
642
|
+
const result = Array.isArray(res) ? res : [];
|
|
643
|
+
giftProductsCache.current = {
|
|
644
|
+
data: result,
|
|
645
|
+
giftHandles: [...giftHandles]
|
|
646
|
+
};
|
|
647
|
+
return result;
|
|
648
|
+
},
|
|
649
|
+
{
|
|
650
|
+
revalidateOnFocus: false
|
|
651
|
+
}
|
|
652
|
+
);
|
|
550
653
|
const finalGiftProductsResult = react.useMemo(() => {
|
|
551
654
|
if (giftProductsCache.current && !shouldFetch) {
|
|
552
655
|
return giftProductsCache.current.data || void 0;
|
|
@@ -628,12 +731,10 @@ function useHasPlusMemberInCart({
|
|
|
628
731
|
};
|
|
629
732
|
}, [cart?.lineItems, plus_monthly_product, plus_annual_product]);
|
|
630
733
|
}
|
|
631
|
-
|
|
632
|
-
// src/hooks/cart/feature/use-cart-attributes.ts
|
|
633
734
|
var getReferralAttributes = () => {
|
|
634
|
-
const inviteCode = Cookies5__default.default.get("invite_code");
|
|
635
|
-
const playModeId = Cookies5__default.default.get("playModeId");
|
|
636
|
-
const popup = Cookies5__default.default.get("_popup");
|
|
735
|
+
const inviteCode = shopifySdk.getLocalStorage("invite_code") || Cookies5__default.default.get("invite_code");
|
|
736
|
+
const playModeId = shopifySdk.getLocalStorage("playModeId") || Cookies5__default.default.get("playModeId");
|
|
737
|
+
const popup = shopifySdk.getLocalStorage("_popup") || Cookies5__default.default.get("_popup");
|
|
637
738
|
if (inviteCode && playModeId) {
|
|
638
739
|
return popup ? [
|
|
639
740
|
{ key: "_invite_code", value: inviteCode ? inviteCode : "" },
|
|
@@ -657,8 +758,6 @@ var useCartAttributes = ({
|
|
|
657
758
|
memberSetting,
|
|
658
759
|
cart
|
|
659
760
|
});
|
|
660
|
-
console.log("memberSetting", memberSetting);
|
|
661
|
-
console.log("hasPlusMember", hasPlusMember);
|
|
662
761
|
react.useEffect(() => {
|
|
663
762
|
setCurrentUrl(window.location.href);
|
|
664
763
|
}, []);
|
|
@@ -684,7 +783,7 @@ var useCartAttributes = ({
|
|
|
684
783
|
return "new_user_login";
|
|
685
784
|
}, [customer]);
|
|
686
785
|
const memberAttributes = react.useMemo(() => {
|
|
687
|
-
|
|
786
|
+
const attributes = [
|
|
688
787
|
{
|
|
689
788
|
key: "_token",
|
|
690
789
|
value: profile?.token
|
|
@@ -705,17 +804,28 @@ var useCartAttributes = ({
|
|
|
705
804
|
value: profile?.token ? "true" : "false"
|
|
706
805
|
}
|
|
707
806
|
];
|
|
807
|
+
if (profile?.token) {
|
|
808
|
+
attributes.push({
|
|
809
|
+
key: "_login_user",
|
|
810
|
+
value: "1"
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
return attributes;
|
|
708
814
|
}, [profile?.memberType, profile?.token, userType, hasPlusMember]);
|
|
709
815
|
const functionAttributes = react.useMemo(() => {
|
|
710
|
-
|
|
711
|
-
|
|
816
|
+
const hasFunctionEnvAttribute = cart?.lineItems.some(
|
|
817
|
+
(item) => item.customAttributes?.some((attr) => attr.key === CUSTOMER_ATTRIBUTE_KEY)
|
|
818
|
+
);
|
|
819
|
+
const discountCodes = cart?.discountCodes.map((item) => item.code).filter((code) => code) || [];
|
|
820
|
+
return hasFunctionEnvAttribute ? [
|
|
821
|
+
{
|
|
712
822
|
key: "_discounts_function_env",
|
|
713
823
|
value: JSON.stringify({
|
|
714
|
-
discount_code:
|
|
824
|
+
discount_code: discountCodes,
|
|
715
825
|
user_tags: customer?.tags || []
|
|
716
826
|
})
|
|
717
827
|
}
|
|
718
|
-
];
|
|
828
|
+
] : [];
|
|
719
829
|
}, [cart]);
|
|
720
830
|
const presellAttributes = react.useMemo(() => {
|
|
721
831
|
return [
|
|
@@ -747,18 +857,50 @@ var useCartAttributes = ({
|
|
|
747
857
|
}
|
|
748
858
|
];
|
|
749
859
|
}, [currentUrl]);
|
|
860
|
+
const commonAttributes = react.useMemo(
|
|
861
|
+
() => [
|
|
862
|
+
...memberAttributes,
|
|
863
|
+
...functionAttributes,
|
|
864
|
+
...presellAttributes,
|
|
865
|
+
...weightAttributes,
|
|
866
|
+
...trackingAttributes,
|
|
867
|
+
...getReferralAttributes()
|
|
868
|
+
].filter((item) => item?.value),
|
|
869
|
+
[memberAttributes, functionAttributes, presellAttributes, weightAttributes, trackingAttributes]
|
|
870
|
+
);
|
|
871
|
+
const extraAttributesInCart = react.useMemo(() => {
|
|
872
|
+
const commonAttributeKeys = [
|
|
873
|
+
// member attributes
|
|
874
|
+
"_token",
|
|
875
|
+
"_member_type",
|
|
876
|
+
"_user_type",
|
|
877
|
+
"_is_login",
|
|
878
|
+
"_login_user",
|
|
879
|
+
// function attributes
|
|
880
|
+
"_discounts_function_env",
|
|
881
|
+
// presell attributes
|
|
882
|
+
"_presale",
|
|
883
|
+
// weight attributes
|
|
884
|
+
"_weight",
|
|
885
|
+
"_app_source_name",
|
|
886
|
+
// tracking attributes
|
|
887
|
+
"utm_params",
|
|
888
|
+
// referral attributes
|
|
889
|
+
"_invite_code",
|
|
890
|
+
"_play_mode_id",
|
|
891
|
+
"_popup"
|
|
892
|
+
];
|
|
893
|
+
return cart?.customAttributes?.filter(
|
|
894
|
+
(item) => !commonAttributeKeys.includes(item.key)
|
|
895
|
+
) || [];
|
|
896
|
+
}, [cart]);
|
|
750
897
|
return react.useMemo(
|
|
751
898
|
() => ({
|
|
752
|
-
attributes: [
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
...presellAttributes,
|
|
756
|
-
...weightAttributes,
|
|
757
|
-
...trackingAttributes,
|
|
758
|
-
...getReferralAttributes()
|
|
759
|
-
].filter((item) => item?.value)
|
|
899
|
+
attributes: [...commonAttributes, ...extraAttributesInCart].filter(
|
|
900
|
+
(item) => item?.value
|
|
901
|
+
)
|
|
760
902
|
}),
|
|
761
|
-
[
|
|
903
|
+
[commonAttributes, extraAttributesInCart]
|
|
762
904
|
);
|
|
763
905
|
};
|
|
764
906
|
var useUpdateLineCodeAmountAttributes = ({
|
|
@@ -974,8 +1116,13 @@ function CartProvider({
|
|
|
974
1116
|
const { attributes } = useCartAttributes({ profile, customer, cart, memberSetting });
|
|
975
1117
|
ahooks.useRequest(
|
|
976
1118
|
() => {
|
|
977
|
-
const newAttributes = [...attributes
|
|
978
|
-
|
|
1119
|
+
const newAttributes = [...attributes];
|
|
1120
|
+
customAttributes.forEach((item) => {
|
|
1121
|
+
if (item.value && !newAttributes.some((attr) => attr.key === item.key)) {
|
|
1122
|
+
newAttributes.push(item);
|
|
1123
|
+
}
|
|
1124
|
+
});
|
|
1125
|
+
const needUpdate = cart && checkAttributesUpdateNeeded(
|
|
979
1126
|
cart.customAttributes,
|
|
980
1127
|
newAttributes,
|
|
981
1128
|
customAttributesNeedDelete
|
|
@@ -1079,8 +1226,14 @@ function CartProvider({
|
|
|
1079
1226
|
);
|
|
1080
1227
|
return result;
|
|
1081
1228
|
}, [cart?.lineItems, scriptAutoFreeGift, functionAutoFreeGift]);
|
|
1229
|
+
const totalQuantity = react.useMemo(() => {
|
|
1230
|
+
const cartLinesCount = cart?.lineItems.reduce((acc, item) => acc + item.quantity, 0) || 0;
|
|
1231
|
+
const giftLinesCount = giftNeedAddToCartLines?.reduce((acc, item) => acc + (item.quantity || 1), 0) || 0;
|
|
1232
|
+
return cartLinesCount + giftLinesCount;
|
|
1233
|
+
}, [cart?.lineItems, giftNeedAddToCartLines]);
|
|
1082
1234
|
const value = react.useMemo(
|
|
1083
1235
|
() => ({
|
|
1236
|
+
totalQuantity,
|
|
1084
1237
|
cart,
|
|
1085
1238
|
isCartLoading,
|
|
1086
1239
|
triggerFetch: fetchCart,
|
|
@@ -1092,6 +1245,7 @@ function CartProvider({
|
|
|
1092
1245
|
isCodeChanging,
|
|
1093
1246
|
setIsCodeChanging,
|
|
1094
1247
|
autoFreeGiftConfig,
|
|
1248
|
+
gradientGiftsConfig,
|
|
1095
1249
|
setLoadingState,
|
|
1096
1250
|
loadingState,
|
|
1097
1251
|
// function满赠
|
|
@@ -1107,6 +1261,7 @@ function CartProvider({
|
|
|
1107
1261
|
}),
|
|
1108
1262
|
[
|
|
1109
1263
|
cart,
|
|
1264
|
+
totalQuantity,
|
|
1110
1265
|
isCartLoading,
|
|
1111
1266
|
fetchCart,
|
|
1112
1267
|
mutateCart,
|
|
@@ -1115,6 +1270,7 @@ function CartProvider({
|
|
|
1115
1270
|
locale,
|
|
1116
1271
|
isCodeChanging,
|
|
1117
1272
|
autoFreeGiftConfig,
|
|
1273
|
+
gradientGiftsConfig,
|
|
1118
1274
|
loadingState,
|
|
1119
1275
|
// function满赠
|
|
1120
1276
|
functionAutoFreeGift,
|