@anker-in/shopify-react 0.1.1-beta.2 → 0.1.1-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/index.d.mts +1 -1
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.js +155 -9
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +153 -10
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/{index-Bea95u2X.d.mts → index-Utuz9i5x.d.mts} +113 -36
- package/dist/{index-BZ6WbAdZ.d.ts → index-aSsTcW2O.d.ts} +113 -36
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +157 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +155 -10
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.d.mts +2 -0
- package/dist/provider/index.d.ts +2 -0
- package/dist/provider/index.js +96 -9
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +96 -9
- package/dist/provider/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -142,6 +142,81 @@ var CODE_AMOUNT_KEY = "_sku_code_money";
|
|
|
142
142
|
var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
|
|
143
143
|
var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
|
|
144
144
|
|
|
145
|
+
// src/hooks/cart/utils/normalize-add-to-cart-lines.ts
|
|
146
|
+
function normalizeAddToCartLines(lines) {
|
|
147
|
+
return lines.filter((line) => line.variant?.id).map((line, index) => {
|
|
148
|
+
const variant = line.variant;
|
|
149
|
+
const product = variant.product;
|
|
150
|
+
const quantity = line.quantity || 1;
|
|
151
|
+
const price = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
|
|
152
|
+
const subtotalAmount = price * quantity;
|
|
153
|
+
const totalAmount = subtotalAmount;
|
|
154
|
+
return {
|
|
155
|
+
id: `temp-line-${index}-${variant.id}`,
|
|
156
|
+
// Temporary ID for pre-cart lines
|
|
157
|
+
name: product?.title || variant.title || "",
|
|
158
|
+
quantity,
|
|
159
|
+
variantId: variant.id,
|
|
160
|
+
productId: product?.id || variant.id.split("/").slice(0, -2).join("/"),
|
|
161
|
+
totalAmount,
|
|
162
|
+
subtotalAmount,
|
|
163
|
+
discountAllocations: [],
|
|
164
|
+
customAttributes: line.attributes || [],
|
|
165
|
+
variant: {
|
|
166
|
+
id: variant.id,
|
|
167
|
+
price,
|
|
168
|
+
listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
|
|
169
|
+
sku: variant.sku || "",
|
|
170
|
+
name: variant.title || "",
|
|
171
|
+
image: variant.image ? {
|
|
172
|
+
url: variant.image.url,
|
|
173
|
+
altText: variant.image.altText || void 0
|
|
174
|
+
} : void 0,
|
|
175
|
+
requiresShipping: false,
|
|
176
|
+
// Default value, not available in NormalizedProductVariant
|
|
177
|
+
availableForSale: variant.availableForSale ?? true,
|
|
178
|
+
quantityAvailable: variant.quantityAvailable ?? 0,
|
|
179
|
+
currentlyNotInStock: false,
|
|
180
|
+
// Default value, will be updated when added to cart
|
|
181
|
+
weight: variant.weight,
|
|
182
|
+
metafields: variant.metafields
|
|
183
|
+
},
|
|
184
|
+
product,
|
|
185
|
+
path: product?.handle ? `/products/${product.handle}` : "",
|
|
186
|
+
discounts: [],
|
|
187
|
+
options: variant.selectedOptions?.map((opt) => ({
|
|
188
|
+
name: opt.name,
|
|
189
|
+
value: opt.value
|
|
190
|
+
}))
|
|
191
|
+
};
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
function createMockCartFromLines(lines, existingCart) {
|
|
195
|
+
const normalizedLines = normalizeAddToCartLines(lines);
|
|
196
|
+
const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
|
|
197
|
+
const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
|
|
198
|
+
return {
|
|
199
|
+
id: existingCart?.id || "temp-cart-id",
|
|
200
|
+
customerId: existingCart?.customerId,
|
|
201
|
+
email: existingCart?.email,
|
|
202
|
+
createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
203
|
+
currency: existingCart?.currency || { code: "USD" },
|
|
204
|
+
taxesIncluded: existingCart?.taxesIncluded,
|
|
205
|
+
lineItems: normalizedLines,
|
|
206
|
+
totallineItemsDiscount: 0,
|
|
207
|
+
orderDiscounts: 0,
|
|
208
|
+
lineItemsSubtotalPrice: subtotalPrice,
|
|
209
|
+
subtotalPrice,
|
|
210
|
+
totalPrice,
|
|
211
|
+
totalTaxAmount: 0,
|
|
212
|
+
discountCodes: existingCart?.discountCodes || [],
|
|
213
|
+
discountAllocations: [],
|
|
214
|
+
url: existingCart?.url || "",
|
|
215
|
+
ready: true,
|
|
216
|
+
customAttributes: existingCart?.customAttributes
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
145
220
|
// src/hooks/cart/utils/index.ts
|
|
146
221
|
var getQuery = () => {
|
|
147
222
|
const url = typeof window !== "undefined" ? window.location.search : "";
|
|
@@ -366,12 +441,18 @@ var formatFunctionAutoFreeGift = ({
|
|
|
366
441
|
};
|
|
367
442
|
return result;
|
|
368
443
|
};
|
|
369
|
-
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
444
|
+
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
370
445
|
const tags = react.useMemo(() => customer?.tags || [], [customer?.tags]);
|
|
371
446
|
const isCustomerLoading = react.useMemo(() => !customer ? true : false, [customer]);
|
|
372
447
|
const dealsType = "";
|
|
373
448
|
const { client, locale } = useShopify();
|
|
374
449
|
const giftProductsCache = react.useRef(null);
|
|
450
|
+
const effectiveCart = react.useMemo(() => {
|
|
451
|
+
if (lines && lines.length > 0) {
|
|
452
|
+
return createMockCartFromLines(lines, cart);
|
|
453
|
+
}
|
|
454
|
+
return cart;
|
|
455
|
+
}, [lines, cart]);
|
|
375
456
|
const { activeCampaign, subtotal } = react.useMemo(() => {
|
|
376
457
|
for (const campaign of autoFreeGiftConfig) {
|
|
377
458
|
const { rule_conditions = [], rule_result } = campaign;
|
|
@@ -379,7 +460,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
379
460
|
const isPreCheckPassed = preCheck(rule_conditions, tags, []);
|
|
380
461
|
if (isPreCheckPassed && spend_get_reward) {
|
|
381
462
|
const matchedSubtotal = getMatchedMainProductSubTotal(
|
|
382
|
-
|
|
463
|
+
effectiveCart,
|
|
383
464
|
spend_get_reward.main_product?.variant_list?.map((v) => v.variant_id) || [],
|
|
384
465
|
{
|
|
385
466
|
spend_money_type: spend_get_reward.main_product?.spend_money_type || 1,
|
|
@@ -393,13 +474,13 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
393
474
|
}
|
|
394
475
|
}
|
|
395
476
|
return { activeCampaign: null, subtotal: 0 };
|
|
396
|
-
}, [autoFreeGiftConfig,
|
|
477
|
+
}, [autoFreeGiftConfig, effectiveCart, tags, dealsType]);
|
|
397
478
|
const { qualifyingGift, nextTierGoal } = react.useMemo(() => {
|
|
398
479
|
if (!activeCampaign || !activeCampaign.rule_result?.spend_get_reward?.gift_product) {
|
|
399
480
|
return { qualifyingGift: null, nextTierGoal: null };
|
|
400
481
|
}
|
|
401
482
|
const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
|
|
402
|
-
const qualifyingTier = [...giftTiers].
|
|
483
|
+
const qualifyingTier = [...giftTiers].sort((a, b) => Number(b.spend_sum_money) - Number(a.spend_sum_money)).find((tier) => subtotal >= Number(tier.spend_sum_money));
|
|
403
484
|
const nextGoal = giftTiers.find((tier) => subtotal < Number(tier.spend_sum_money));
|
|
404
485
|
if (!qualifyingTier) {
|
|
405
486
|
return { qualifyingGift: null, nextTierGoal: nextGoal || null };
|
|
@@ -476,12 +557,19 @@ var useScriptAutoFreeGift = ({
|
|
|
476
557
|
campaign,
|
|
477
558
|
_giveaway,
|
|
478
559
|
cart,
|
|
479
|
-
locale: providedLocale
|
|
560
|
+
locale: providedLocale,
|
|
561
|
+
lines
|
|
480
562
|
}) => {
|
|
481
563
|
const { client, locale: contextLocale } = useShopify();
|
|
482
564
|
const locale = providedLocale || contextLocale;
|
|
483
565
|
const [points_subscribe, set_points_subscribe] = react.useState(false);
|
|
484
566
|
const giftProductsCache = react.useRef(null);
|
|
567
|
+
const effectiveCart = react.useMemo(() => {
|
|
568
|
+
if (lines && lines.length > 0) {
|
|
569
|
+
return createMockCartFromLines(lines, cart);
|
|
570
|
+
}
|
|
571
|
+
return cart;
|
|
572
|
+
}, [lines, cart]);
|
|
485
573
|
react.useEffect(() => {
|
|
486
574
|
if (locale === "au") {
|
|
487
575
|
const isPointsSubscribe = Cookies5__default.default.get("points_subscribe");
|
|
@@ -503,14 +591,14 @@ var useScriptAutoFreeGift = ({
|
|
|
503
591
|
upgrade_multiple2 = 1.2;
|
|
504
592
|
upgrade_value2 = 40;
|
|
505
593
|
}
|
|
506
|
-
|
|
594
|
+
effectiveCart?.lineItems?.forEach(({ customAttributes }) => {
|
|
507
595
|
customAttributes?.forEach(({ key, value }) => {
|
|
508
596
|
if (key === "_amount_upgrade_multiple") upgrade_multiple2 = Number(value) || 1;
|
|
509
597
|
if (key === "_amount_upgrade_value") upgrade_value2 = Number(value) || 0;
|
|
510
598
|
});
|
|
511
599
|
});
|
|
512
600
|
return [upgrade_multiple2, upgrade_value2];
|
|
513
|
-
}, [
|
|
601
|
+
}, [effectiveCart?.lineItems, points_subscribe]);
|
|
514
602
|
const breakpoints = react.useMemo(() => {
|
|
515
603
|
if (!isActivityAvailable) return [];
|
|
516
604
|
return (campaign?.breakpoints || []).map((item) => ({
|
|
@@ -538,7 +626,7 @@ var useScriptAutoFreeGift = ({
|
|
|
538
626
|
}, [giftHandles]);
|
|
539
627
|
const involvedLines = react.useMemo(() => {
|
|
540
628
|
if (!isActivityAvailable) return [];
|
|
541
|
-
return (
|
|
629
|
+
return (effectiveCart?.lineItems || []).filter((line) => {
|
|
542
630
|
const isNotGift = line?.totalAmount && Number(line.totalAmount) > 0 && line.customAttributes?.every(
|
|
543
631
|
(item) => item.key !== _giveaway
|
|
544
632
|
);
|
|
@@ -547,7 +635,7 @@ var useScriptAutoFreeGift = ({
|
|
|
547
635
|
);
|
|
548
636
|
return isNotGift && hasCampaignTag;
|
|
549
637
|
});
|
|
550
|
-
}, [
|
|
638
|
+
}, [effectiveCart?.lineItems, isActivityAvailable, _giveaway]);
|
|
551
639
|
const involvedSubTotal = react.useMemo(() => {
|
|
552
640
|
if (!isActivityAvailable) return new Decimal2__default.default(0);
|
|
553
641
|
return involvedLines.reduce((prev, item) => {
|
|
@@ -934,6 +1022,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
934
1022
|
if (!resultCart) {
|
|
935
1023
|
return void 0;
|
|
936
1024
|
}
|
|
1025
|
+
console.log("npm addCartLines resultCart", resultCart);
|
|
937
1026
|
if (resultCart.discountCodes && resultCart.discountCodes.length > 0) {
|
|
938
1027
|
const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
939
1028
|
if (unapplicableCodes.length > 0) {
|
|
@@ -1107,6 +1196,60 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
1107
1196
|
);
|
|
1108
1197
|
return useSWRMutation__default.default("buy-now", buyNow, swrOptions);
|
|
1109
1198
|
}
|
|
1199
|
+
function useCalcGiftsFromLines({
|
|
1200
|
+
lines,
|
|
1201
|
+
customer,
|
|
1202
|
+
scriptGiveawayKey = CUSTOMER_SCRIPT_GIFT_KEY
|
|
1203
|
+
}) {
|
|
1204
|
+
const { locale } = useShopify();
|
|
1205
|
+
const { cart, autoFreeGiftConfig, gradientGiftsConfig } = useCartContext();
|
|
1206
|
+
const functionGift = useCalcAutoFreeGift(cart, autoFreeGiftConfig || [], customer, lines);
|
|
1207
|
+
const scriptGift = useScriptAutoFreeGift({
|
|
1208
|
+
campaign: gradientGiftsConfig || null,
|
|
1209
|
+
_giveaway: scriptGiveawayKey,
|
|
1210
|
+
cart,
|
|
1211
|
+
locale,
|
|
1212
|
+
lines
|
|
1213
|
+
});
|
|
1214
|
+
const allGiftLines = react.useMemo(() => {
|
|
1215
|
+
const functionGiftLines = functionGift.qualifyingGift?.itemsToAdd || [];
|
|
1216
|
+
const scriptGiftLines = scriptGift.freeGiftLevel ? scriptGift.freeGiftLevel.giveawayProducts.map((product) => {
|
|
1217
|
+
const giftProduct = scriptGift.giftProductsResult?.find(
|
|
1218
|
+
(p) => p.handle === product.handle
|
|
1219
|
+
);
|
|
1220
|
+
const variant = giftProduct?.variants?.[0];
|
|
1221
|
+
return {
|
|
1222
|
+
variant: {
|
|
1223
|
+
id: variant?.id || "",
|
|
1224
|
+
handle: product.handle,
|
|
1225
|
+
sku: product.sku
|
|
1226
|
+
},
|
|
1227
|
+
quantity: 1,
|
|
1228
|
+
attributes: [
|
|
1229
|
+
{
|
|
1230
|
+
key: scriptGiveawayKey,
|
|
1231
|
+
value: "true"
|
|
1232
|
+
}
|
|
1233
|
+
]
|
|
1234
|
+
};
|
|
1235
|
+
}).filter((item) => item.variant.id) : [];
|
|
1236
|
+
return [...functionGiftLines, ...scriptGiftLines];
|
|
1237
|
+
}, [
|
|
1238
|
+
functionGift.qualifyingGift,
|
|
1239
|
+
scriptGift.freeGiftLevel,
|
|
1240
|
+
scriptGift.giftProductsResult,
|
|
1241
|
+
scriptGiveawayKey
|
|
1242
|
+
]);
|
|
1243
|
+
const hasGifts = react.useMemo(() => {
|
|
1244
|
+
return allGiftLines.length > 0;
|
|
1245
|
+
}, [allGiftLines]);
|
|
1246
|
+
return {
|
|
1247
|
+
functionGift,
|
|
1248
|
+
scriptGift,
|
|
1249
|
+
allGiftLines,
|
|
1250
|
+
hasGifts
|
|
1251
|
+
};
|
|
1252
|
+
}
|
|
1110
1253
|
|
|
1111
1254
|
// src/hooks/cart/types/order-discount.ts
|
|
1112
1255
|
var OrderDiscountType = /* @__PURE__ */ ((OrderDiscountType2) => {
|
|
@@ -3069,6 +3212,7 @@ function CartProvider({
|
|
|
3069
3212
|
isCodeChanging,
|
|
3070
3213
|
setIsCodeChanging,
|
|
3071
3214
|
autoFreeGiftConfig,
|
|
3215
|
+
gradientGiftsConfig,
|
|
3072
3216
|
setLoadingState,
|
|
3073
3217
|
loadingState,
|
|
3074
3218
|
// function满赠
|
|
@@ -3092,6 +3236,7 @@ function CartProvider({
|
|
|
3092
3236
|
locale,
|
|
3093
3237
|
isCodeChanging,
|
|
3094
3238
|
autoFreeGiftConfig,
|
|
3239
|
+
gradientGiftsConfig,
|
|
3095
3240
|
loadingState,
|
|
3096
3241
|
// function满赠
|
|
3097
3242
|
functionAutoFreeGift,
|
|
@@ -3166,6 +3311,7 @@ exports.browserCookieAdapter = browserCookieAdapter;
|
|
|
3166
3311
|
exports.btoaID = btoaID;
|
|
3167
3312
|
exports.checkAttributesUpdateNeeded = checkAttributesUpdateNeeded;
|
|
3168
3313
|
exports.clearGeoLocationCache = clearGeoLocationCache;
|
|
3314
|
+
exports.createMockCartFromLines = createMockCartFromLines;
|
|
3169
3315
|
exports.currencyCodeMapping = currencyCodeMapping;
|
|
3170
3316
|
exports.defaultSWRMutationConfiguration = defaultSWRMutationConfiguration;
|
|
3171
3317
|
exports.formatFunctionAutoFreeGift = formatFunctionAutoFreeGift;
|
|
@@ -3176,6 +3322,7 @@ exports.getDiscountEnvAttributeValue = getDiscountEnvAttributeValue;
|
|
|
3176
3322
|
exports.getMatchedMainProductSubTotal = getMatchedMainProductSubTotal;
|
|
3177
3323
|
exports.getQuery = getQuery;
|
|
3178
3324
|
exports.getReferralAttributes = getReferralAttributes;
|
|
3325
|
+
exports.normalizeAddToCartLines = normalizeAddToCartLines;
|
|
3179
3326
|
exports.preCheck = preCheck;
|
|
3180
3327
|
exports.safeParse = safeParse;
|
|
3181
3328
|
exports.trackAddToCartFBQ = trackAddToCartFBQ;
|
|
@@ -3197,6 +3344,7 @@ exports.useAutoRemovePlusMemberInCart = useAutoRemovePlusMemberInCart;
|
|
|
3197
3344
|
exports.useBlog = useBlog;
|
|
3198
3345
|
exports.useBuyNow = useBuyNow;
|
|
3199
3346
|
exports.useCalcAutoFreeGift = useCalcAutoFreeGift;
|
|
3347
|
+
exports.useCalcGiftsFromLines = useCalcGiftsFromLines;
|
|
3200
3348
|
exports.useCalcOrderDiscount = useCalcOrderDiscount;
|
|
3201
3349
|
exports.useCartAttributes = useCartAttributes;
|
|
3202
3350
|
exports.useCartContext = useCartContext;
|