@anker-in/shopify-react 0.1.1-beta.2 → 0.1.1-beta.3
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 +154 -8
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +152 -9
- 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 +156 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +154 -9
- 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 +95 -8
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +95 -8
- package/dist/provider/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/hooks/index.mjs
CHANGED
|
@@ -62,6 +62,81 @@ var CODE_AMOUNT_KEY = "_sku_code_money";
|
|
|
62
62
|
var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
|
|
63
63
|
var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
|
|
64
64
|
|
|
65
|
+
// src/hooks/cart/utils/normalize-add-to-cart-lines.ts
|
|
66
|
+
function normalizeAddToCartLines(lines) {
|
|
67
|
+
return lines.filter((line) => line.variant?.id).map((line, index) => {
|
|
68
|
+
const variant = line.variant;
|
|
69
|
+
const product = variant.product;
|
|
70
|
+
const quantity = line.quantity || 1;
|
|
71
|
+
const price = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
|
|
72
|
+
const subtotalAmount = price * quantity;
|
|
73
|
+
const totalAmount = subtotalAmount;
|
|
74
|
+
return {
|
|
75
|
+
id: `temp-line-${index}-${variant.id}`,
|
|
76
|
+
// Temporary ID for pre-cart lines
|
|
77
|
+
name: product?.title || variant.title || "",
|
|
78
|
+
quantity,
|
|
79
|
+
variantId: variant.id,
|
|
80
|
+
productId: product?.id || variant.id.split("/").slice(0, -2).join("/"),
|
|
81
|
+
totalAmount,
|
|
82
|
+
subtotalAmount,
|
|
83
|
+
discountAllocations: [],
|
|
84
|
+
customAttributes: line.attributes || [],
|
|
85
|
+
variant: {
|
|
86
|
+
id: variant.id,
|
|
87
|
+
price,
|
|
88
|
+
listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
|
|
89
|
+
sku: variant.sku || "",
|
|
90
|
+
name: variant.title || "",
|
|
91
|
+
image: variant.image ? {
|
|
92
|
+
url: variant.image.url,
|
|
93
|
+
altText: variant.image.altText || void 0
|
|
94
|
+
} : void 0,
|
|
95
|
+
requiresShipping: false,
|
|
96
|
+
// Default value, not available in NormalizedProductVariant
|
|
97
|
+
availableForSale: variant.availableForSale ?? true,
|
|
98
|
+
quantityAvailable: variant.quantityAvailable ?? 0,
|
|
99
|
+
currentlyNotInStock: false,
|
|
100
|
+
// Default value, will be updated when added to cart
|
|
101
|
+
weight: variant.weight,
|
|
102
|
+
metafields: variant.metafields
|
|
103
|
+
},
|
|
104
|
+
product,
|
|
105
|
+
path: product?.handle ? `/products/${product.handle}` : "",
|
|
106
|
+
discounts: [],
|
|
107
|
+
options: variant.selectedOptions?.map((opt) => ({
|
|
108
|
+
name: opt.name,
|
|
109
|
+
value: opt.value
|
|
110
|
+
}))
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
function createMockCartFromLines(lines, existingCart) {
|
|
115
|
+
const normalizedLines = normalizeAddToCartLines(lines);
|
|
116
|
+
const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
|
|
117
|
+
const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
|
|
118
|
+
return {
|
|
119
|
+
id: existingCart?.id || "temp-cart-id",
|
|
120
|
+
customerId: existingCart?.customerId,
|
|
121
|
+
email: existingCart?.email,
|
|
122
|
+
createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
123
|
+
currency: existingCart?.currency || { code: "USD" },
|
|
124
|
+
taxesIncluded: existingCart?.taxesIncluded,
|
|
125
|
+
lineItems: normalizedLines,
|
|
126
|
+
totallineItemsDiscount: 0,
|
|
127
|
+
orderDiscounts: 0,
|
|
128
|
+
lineItemsSubtotalPrice: subtotalPrice,
|
|
129
|
+
subtotalPrice,
|
|
130
|
+
totalPrice,
|
|
131
|
+
totalTaxAmount: 0,
|
|
132
|
+
discountCodes: existingCart?.discountCodes || [],
|
|
133
|
+
discountAllocations: [],
|
|
134
|
+
url: existingCart?.url || "",
|
|
135
|
+
ready: true,
|
|
136
|
+
customAttributes: existingCart?.customAttributes
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
65
140
|
// src/hooks/cart/utils/index.ts
|
|
66
141
|
var getQuery = () => {
|
|
67
142
|
const url = typeof window !== "undefined" ? window.location.search : "";
|
|
@@ -286,12 +361,18 @@ var formatFunctionAutoFreeGift = ({
|
|
|
286
361
|
};
|
|
287
362
|
return result;
|
|
288
363
|
};
|
|
289
|
-
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
364
|
+
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
290
365
|
const tags = useMemo(() => customer?.tags || [], [customer?.tags]);
|
|
291
366
|
const isCustomerLoading = useMemo(() => !customer ? true : false, [customer]);
|
|
292
367
|
const dealsType = "";
|
|
293
368
|
const { client, locale } = useShopify();
|
|
294
369
|
const giftProductsCache = useRef(null);
|
|
370
|
+
const effectiveCart = useMemo(() => {
|
|
371
|
+
if (lines && lines.length > 0) {
|
|
372
|
+
return createMockCartFromLines(lines, cart);
|
|
373
|
+
}
|
|
374
|
+
return cart;
|
|
375
|
+
}, [lines, cart]);
|
|
295
376
|
const { activeCampaign, subtotal } = useMemo(() => {
|
|
296
377
|
for (const campaign of autoFreeGiftConfig) {
|
|
297
378
|
const { rule_conditions = [], rule_result } = campaign;
|
|
@@ -299,7 +380,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
299
380
|
const isPreCheckPassed = preCheck(rule_conditions, tags, []);
|
|
300
381
|
if (isPreCheckPassed && spend_get_reward) {
|
|
301
382
|
const matchedSubtotal = getMatchedMainProductSubTotal(
|
|
302
|
-
|
|
383
|
+
effectiveCart,
|
|
303
384
|
spend_get_reward.main_product?.variant_list?.map((v) => v.variant_id) || [],
|
|
304
385
|
{
|
|
305
386
|
spend_money_type: spend_get_reward.main_product?.spend_money_type || 1,
|
|
@@ -313,7 +394,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
313
394
|
}
|
|
314
395
|
}
|
|
315
396
|
return { activeCampaign: null, subtotal: 0 };
|
|
316
|
-
}, [autoFreeGiftConfig,
|
|
397
|
+
}, [autoFreeGiftConfig, effectiveCart, tags, dealsType]);
|
|
317
398
|
const { qualifyingGift, nextTierGoal } = useMemo(() => {
|
|
318
399
|
if (!activeCampaign || !activeCampaign.rule_result?.spend_get_reward?.gift_product) {
|
|
319
400
|
return { qualifyingGift: null, nextTierGoal: null };
|
|
@@ -396,12 +477,19 @@ var useScriptAutoFreeGift = ({
|
|
|
396
477
|
campaign,
|
|
397
478
|
_giveaway,
|
|
398
479
|
cart,
|
|
399
|
-
locale: providedLocale
|
|
480
|
+
locale: providedLocale,
|
|
481
|
+
lines
|
|
400
482
|
}) => {
|
|
401
483
|
const { client, locale: contextLocale } = useShopify();
|
|
402
484
|
const locale = providedLocale || contextLocale;
|
|
403
485
|
const [points_subscribe, set_points_subscribe] = useState(false);
|
|
404
486
|
const giftProductsCache = useRef(null);
|
|
487
|
+
const effectiveCart = useMemo(() => {
|
|
488
|
+
if (lines && lines.length > 0) {
|
|
489
|
+
return createMockCartFromLines(lines, cart);
|
|
490
|
+
}
|
|
491
|
+
return cart;
|
|
492
|
+
}, [lines, cart]);
|
|
405
493
|
useEffect(() => {
|
|
406
494
|
if (locale === "au") {
|
|
407
495
|
const isPointsSubscribe = Cookies5.get("points_subscribe");
|
|
@@ -423,14 +511,14 @@ var useScriptAutoFreeGift = ({
|
|
|
423
511
|
upgrade_multiple2 = 1.2;
|
|
424
512
|
upgrade_value2 = 40;
|
|
425
513
|
}
|
|
426
|
-
|
|
514
|
+
effectiveCart?.lineItems?.forEach(({ customAttributes }) => {
|
|
427
515
|
customAttributes?.forEach(({ key, value }) => {
|
|
428
516
|
if (key === "_amount_upgrade_multiple") upgrade_multiple2 = Number(value) || 1;
|
|
429
517
|
if (key === "_amount_upgrade_value") upgrade_value2 = Number(value) || 0;
|
|
430
518
|
});
|
|
431
519
|
});
|
|
432
520
|
return [upgrade_multiple2, upgrade_value2];
|
|
433
|
-
}, [
|
|
521
|
+
}, [effectiveCart?.lineItems, points_subscribe]);
|
|
434
522
|
const breakpoints = useMemo(() => {
|
|
435
523
|
if (!isActivityAvailable) return [];
|
|
436
524
|
return (campaign?.breakpoints || []).map((item) => ({
|
|
@@ -458,7 +546,7 @@ var useScriptAutoFreeGift = ({
|
|
|
458
546
|
}, [giftHandles]);
|
|
459
547
|
const involvedLines = useMemo(() => {
|
|
460
548
|
if (!isActivityAvailable) return [];
|
|
461
|
-
return (
|
|
549
|
+
return (effectiveCart?.lineItems || []).filter((line) => {
|
|
462
550
|
const isNotGift = line?.totalAmount && Number(line.totalAmount) > 0 && line.customAttributes?.every(
|
|
463
551
|
(item) => item.key !== _giveaway
|
|
464
552
|
);
|
|
@@ -467,7 +555,7 @@ var useScriptAutoFreeGift = ({
|
|
|
467
555
|
);
|
|
468
556
|
return isNotGift && hasCampaignTag;
|
|
469
557
|
});
|
|
470
|
-
}, [
|
|
558
|
+
}, [effectiveCart?.lineItems, isActivityAvailable, _giveaway]);
|
|
471
559
|
const involvedSubTotal = useMemo(() => {
|
|
472
560
|
if (!isActivityAvailable) return new Decimal2(0);
|
|
473
561
|
return involvedLines.reduce((prev, item) => {
|
|
@@ -829,6 +917,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
829
917
|
if (!resultCart) {
|
|
830
918
|
return void 0;
|
|
831
919
|
}
|
|
920
|
+
console.log("npm addCartLines resultCart", resultCart);
|
|
832
921
|
if (resultCart.discountCodes && resultCart.discountCodes.length > 0) {
|
|
833
922
|
const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
834
923
|
if (unapplicableCodes.length > 0) {
|
|
@@ -1002,6 +1091,60 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
1002
1091
|
);
|
|
1003
1092
|
return useSWRMutation("buy-now", buyNow, swrOptions);
|
|
1004
1093
|
}
|
|
1094
|
+
function useCalcGiftsFromLines({
|
|
1095
|
+
lines,
|
|
1096
|
+
customer,
|
|
1097
|
+
scriptGiveawayKey = CUSTOMER_SCRIPT_GIFT_KEY
|
|
1098
|
+
}) {
|
|
1099
|
+
const { locale } = useShopify();
|
|
1100
|
+
const { cart, autoFreeGiftConfig, gradientGiftsConfig } = useCartContext();
|
|
1101
|
+
const functionGift = useCalcAutoFreeGift(cart, autoFreeGiftConfig || [], customer, lines);
|
|
1102
|
+
const scriptGift = useScriptAutoFreeGift({
|
|
1103
|
+
campaign: gradientGiftsConfig || null,
|
|
1104
|
+
_giveaway: scriptGiveawayKey,
|
|
1105
|
+
cart,
|
|
1106
|
+
locale,
|
|
1107
|
+
lines
|
|
1108
|
+
});
|
|
1109
|
+
const allGiftLines = useMemo(() => {
|
|
1110
|
+
const functionGiftLines = functionGift.qualifyingGift?.itemsToAdd || [];
|
|
1111
|
+
const scriptGiftLines = scriptGift.freeGiftLevel ? scriptGift.freeGiftLevel.giveawayProducts.map((product) => {
|
|
1112
|
+
const giftProduct = scriptGift.giftProductsResult?.find(
|
|
1113
|
+
(p) => p.handle === product.handle
|
|
1114
|
+
);
|
|
1115
|
+
const variant = giftProduct?.variants?.[0];
|
|
1116
|
+
return {
|
|
1117
|
+
variant: {
|
|
1118
|
+
id: variant?.id || "",
|
|
1119
|
+
handle: product.handle,
|
|
1120
|
+
sku: product.sku
|
|
1121
|
+
},
|
|
1122
|
+
quantity: 1,
|
|
1123
|
+
attributes: [
|
|
1124
|
+
{
|
|
1125
|
+
key: scriptGiveawayKey,
|
|
1126
|
+
value: "true"
|
|
1127
|
+
}
|
|
1128
|
+
]
|
|
1129
|
+
};
|
|
1130
|
+
}).filter((item) => item.variant.id) : [];
|
|
1131
|
+
return [...functionGiftLines, ...scriptGiftLines];
|
|
1132
|
+
}, [
|
|
1133
|
+
functionGift.qualifyingGift,
|
|
1134
|
+
scriptGift.freeGiftLevel,
|
|
1135
|
+
scriptGift.giftProductsResult,
|
|
1136
|
+
scriptGiveawayKey
|
|
1137
|
+
]);
|
|
1138
|
+
const hasGifts = useMemo(() => {
|
|
1139
|
+
return allGiftLines.length > 0;
|
|
1140
|
+
}, [allGiftLines]);
|
|
1141
|
+
return {
|
|
1142
|
+
functionGift,
|
|
1143
|
+
scriptGift,
|
|
1144
|
+
allGiftLines,
|
|
1145
|
+
hasGifts
|
|
1146
|
+
};
|
|
1147
|
+
}
|
|
1005
1148
|
|
|
1006
1149
|
// src/hooks/cart/types/order-discount.ts
|
|
1007
1150
|
var OrderDiscountType = /* @__PURE__ */ ((OrderDiscountType2) => {
|
|
@@ -2801,6 +2944,6 @@ function clearGeoLocationCache(cacheKey = "geoLocation") {
|
|
|
2801
2944
|
}
|
|
2802
2945
|
}
|
|
2803
2946
|
|
|
2804
|
-
export { BuyRuleType, CODE_AMOUNT_KEY, CUSTOMER_ATTRIBUTE_KEY, CUSTOMER_SCRIPT_GIFT_KEY, DeliveryPlusType, MAIN_PRODUCT_CODE, OrderBasePriceType, OrderDiscountType, PLUS_MEMBER_TYPE, PlusMemberContext, PlusMemberMode, PlusMemberProvider, PriceBasePriceType, PriceDiscountType, RuleType, SCRIPT_CODE_AMOUNT_KEY, ShippingMethodMode, SpendMoneyType, atobID, btoaID, checkAttributesUpdateNeeded, clearGeoLocationCache, currencyCodeMapping, defaultSWRMutationConfiguration, formatFunctionAutoFreeGift, formatScriptAutoFreeGift, getCachedGeoLocation, getDiscountEnvAttributeValue, getMatchedMainProductSubTotal, getQuery, getReferralAttributes, preCheck, safeParse, useAddCartLines, useAddPlusMemberProductsToCart, useAddToCart, useAllBlogs, useAllCollections, useAllProducts, useApplyCartCodes, useArticle, useArticles, useArticlesInBlog, useAutoRemovePlusMemberInCart, useBlog, useBuyNow, useCalcAutoFreeGift, useCalcOrderDiscount, useCartAttributes, useCartItemQuantityLimit, useCollection, useCollections, useCreateCart, useExposure, useGeoLocation, useHasPlusMemberInCart, useIntersection, usePlusAnnualProductVariant, usePlusMemberCheckoutCustomAttributes, usePlusMemberContext, usePlusMemberDeliveryCodes, usePlusMemberItemCustomAttributes, usePlusMonthlyProductVariant, usePrice, useProduct, useProductUrl, useProductsByHandles, useRemoveCartCodes, useRemoveCartLines, useReplaceCartPlusMember, useScriptAutoFreeGift, useSearch, useSelectedOptions, useShippingMethodAvailableCheck, useShippingMethods, useSite, useUpdateCartAttributes, useUpdateCartLines, useUpdateLineCodeAmountAttributes, useUpdateVariantQuery, useVariant, useVariantMedia };
|
|
2947
|
+
export { BuyRuleType, CODE_AMOUNT_KEY, CUSTOMER_ATTRIBUTE_KEY, CUSTOMER_SCRIPT_GIFT_KEY, DeliveryPlusType, MAIN_PRODUCT_CODE, OrderBasePriceType, OrderDiscountType, PLUS_MEMBER_TYPE, PlusMemberContext, PlusMemberMode, PlusMemberProvider, PriceBasePriceType, PriceDiscountType, RuleType, SCRIPT_CODE_AMOUNT_KEY, ShippingMethodMode, SpendMoneyType, atobID, btoaID, checkAttributesUpdateNeeded, clearGeoLocationCache, createMockCartFromLines, currencyCodeMapping, defaultSWRMutationConfiguration, formatFunctionAutoFreeGift, formatScriptAutoFreeGift, getCachedGeoLocation, getDiscountEnvAttributeValue, getMatchedMainProductSubTotal, getQuery, getReferralAttributes, normalizeAddToCartLines, preCheck, safeParse, useAddCartLines, useAddPlusMemberProductsToCart, useAddToCart, useAllBlogs, useAllCollections, useAllProducts, useApplyCartCodes, useArticle, useArticles, useArticlesInBlog, useAutoRemovePlusMemberInCart, useBlog, useBuyNow, useCalcAutoFreeGift, useCalcGiftsFromLines, useCalcOrderDiscount, useCartAttributes, useCartItemQuantityLimit, useCollection, useCollections, useCreateCart, useExposure, useGeoLocation, useHasPlusMemberInCart, useIntersection, usePlusAnnualProductVariant, usePlusMemberCheckoutCustomAttributes, usePlusMemberContext, usePlusMemberDeliveryCodes, usePlusMemberItemCustomAttributes, usePlusMonthlyProductVariant, usePrice, useProduct, useProductUrl, useProductsByHandles, useRemoveCartCodes, useRemoveCartLines, useReplaceCartPlusMember, useScriptAutoFreeGift, useSearch, useSelectedOptions, useShippingMethodAvailableCheck, useShippingMethods, useSite, useUpdateCartAttributes, useUpdateCartLines, useUpdateLineCodeAmountAttributes, useUpdateVariantQuery, useVariant, useVariantMedia };
|
|
2805
2948
|
//# sourceMappingURL=index.mjs.map
|
|
2806
2949
|
//# sourceMappingURL=index.mjs.map
|