@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/index.mjs
CHANGED
|
@@ -134,6 +134,81 @@ var CODE_AMOUNT_KEY = "_sku_code_money";
|
|
|
134
134
|
var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
|
|
135
135
|
var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
|
|
136
136
|
|
|
137
|
+
// src/hooks/cart/utils/normalize-add-to-cart-lines.ts
|
|
138
|
+
function normalizeAddToCartLines(lines) {
|
|
139
|
+
return lines.filter((line) => line.variant?.id).map((line, index) => {
|
|
140
|
+
const variant = line.variant;
|
|
141
|
+
const product = variant.product;
|
|
142
|
+
const quantity = line.quantity || 1;
|
|
143
|
+
const price = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
|
|
144
|
+
const subtotalAmount = price * quantity;
|
|
145
|
+
const totalAmount = subtotalAmount;
|
|
146
|
+
return {
|
|
147
|
+
id: `temp-line-${index}-${variant.id}`,
|
|
148
|
+
// Temporary ID for pre-cart lines
|
|
149
|
+
name: product?.title || variant.title || "",
|
|
150
|
+
quantity,
|
|
151
|
+
variantId: variant.id,
|
|
152
|
+
productId: product?.id || variant.id.split("/").slice(0, -2).join("/"),
|
|
153
|
+
totalAmount,
|
|
154
|
+
subtotalAmount,
|
|
155
|
+
discountAllocations: [],
|
|
156
|
+
customAttributes: line.attributes || [],
|
|
157
|
+
variant: {
|
|
158
|
+
id: variant.id,
|
|
159
|
+
price,
|
|
160
|
+
listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
|
|
161
|
+
sku: variant.sku || "",
|
|
162
|
+
name: variant.title || "",
|
|
163
|
+
image: variant.image ? {
|
|
164
|
+
url: variant.image.url,
|
|
165
|
+
altText: variant.image.altText || void 0
|
|
166
|
+
} : void 0,
|
|
167
|
+
requiresShipping: false,
|
|
168
|
+
// Default value, not available in NormalizedProductVariant
|
|
169
|
+
availableForSale: variant.availableForSale ?? true,
|
|
170
|
+
quantityAvailable: variant.quantityAvailable ?? 0,
|
|
171
|
+
currentlyNotInStock: false,
|
|
172
|
+
// Default value, will be updated when added to cart
|
|
173
|
+
weight: variant.weight,
|
|
174
|
+
metafields: variant.metafields
|
|
175
|
+
},
|
|
176
|
+
product,
|
|
177
|
+
path: product?.handle ? `/products/${product.handle}` : "",
|
|
178
|
+
discounts: [],
|
|
179
|
+
options: variant.selectedOptions?.map((opt) => ({
|
|
180
|
+
name: opt.name,
|
|
181
|
+
value: opt.value
|
|
182
|
+
}))
|
|
183
|
+
};
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
function createMockCartFromLines(lines, existingCart) {
|
|
187
|
+
const normalizedLines = normalizeAddToCartLines(lines);
|
|
188
|
+
const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
|
|
189
|
+
const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
|
|
190
|
+
return {
|
|
191
|
+
id: existingCart?.id || "temp-cart-id",
|
|
192
|
+
customerId: existingCart?.customerId,
|
|
193
|
+
email: existingCart?.email,
|
|
194
|
+
createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
195
|
+
currency: existingCart?.currency || { code: "USD" },
|
|
196
|
+
taxesIncluded: existingCart?.taxesIncluded,
|
|
197
|
+
lineItems: normalizedLines,
|
|
198
|
+
totallineItemsDiscount: 0,
|
|
199
|
+
orderDiscounts: 0,
|
|
200
|
+
lineItemsSubtotalPrice: subtotalPrice,
|
|
201
|
+
subtotalPrice,
|
|
202
|
+
totalPrice,
|
|
203
|
+
totalTaxAmount: 0,
|
|
204
|
+
discountCodes: existingCart?.discountCodes || [],
|
|
205
|
+
discountAllocations: [],
|
|
206
|
+
url: existingCart?.url || "",
|
|
207
|
+
ready: true,
|
|
208
|
+
customAttributes: existingCart?.customAttributes
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
137
212
|
// src/hooks/cart/utils/index.ts
|
|
138
213
|
var getQuery = () => {
|
|
139
214
|
const url = typeof window !== "undefined" ? window.location.search : "";
|
|
@@ -358,12 +433,18 @@ var formatFunctionAutoFreeGift = ({
|
|
|
358
433
|
};
|
|
359
434
|
return result;
|
|
360
435
|
};
|
|
361
|
-
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
436
|
+
var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
362
437
|
const tags = useMemo(() => customer?.tags || [], [customer?.tags]);
|
|
363
438
|
const isCustomerLoading = useMemo(() => !customer ? true : false, [customer]);
|
|
364
439
|
const dealsType = "";
|
|
365
440
|
const { client, locale } = useShopify();
|
|
366
441
|
const giftProductsCache = useRef(null);
|
|
442
|
+
const effectiveCart = useMemo(() => {
|
|
443
|
+
if (lines && lines.length > 0) {
|
|
444
|
+
return createMockCartFromLines(lines, cart);
|
|
445
|
+
}
|
|
446
|
+
return cart;
|
|
447
|
+
}, [lines, cart]);
|
|
367
448
|
const { activeCampaign, subtotal } = useMemo(() => {
|
|
368
449
|
for (const campaign of autoFreeGiftConfig) {
|
|
369
450
|
const { rule_conditions = [], rule_result } = campaign;
|
|
@@ -371,7 +452,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
371
452
|
const isPreCheckPassed = preCheck(rule_conditions, tags, []);
|
|
372
453
|
if (isPreCheckPassed && spend_get_reward) {
|
|
373
454
|
const matchedSubtotal = getMatchedMainProductSubTotal(
|
|
374
|
-
|
|
455
|
+
effectiveCart,
|
|
375
456
|
spend_get_reward.main_product?.variant_list?.map((v) => v.variant_id) || [],
|
|
376
457
|
{
|
|
377
458
|
spend_money_type: spend_get_reward.main_product?.spend_money_type || 1,
|
|
@@ -385,7 +466,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
|
|
|
385
466
|
}
|
|
386
467
|
}
|
|
387
468
|
return { activeCampaign: null, subtotal: 0 };
|
|
388
|
-
}, [autoFreeGiftConfig,
|
|
469
|
+
}, [autoFreeGiftConfig, effectiveCart, tags, dealsType]);
|
|
389
470
|
const { qualifyingGift, nextTierGoal } = useMemo(() => {
|
|
390
471
|
if (!activeCampaign || !activeCampaign.rule_result?.spend_get_reward?.gift_product) {
|
|
391
472
|
return { qualifyingGift: null, nextTierGoal: null };
|
|
@@ -468,12 +549,19 @@ var useScriptAutoFreeGift = ({
|
|
|
468
549
|
campaign,
|
|
469
550
|
_giveaway,
|
|
470
551
|
cart,
|
|
471
|
-
locale: providedLocale
|
|
552
|
+
locale: providedLocale,
|
|
553
|
+
lines
|
|
472
554
|
}) => {
|
|
473
555
|
const { client, locale: contextLocale } = useShopify();
|
|
474
556
|
const locale = providedLocale || contextLocale;
|
|
475
557
|
const [points_subscribe, set_points_subscribe] = useState(false);
|
|
476
558
|
const giftProductsCache = useRef(null);
|
|
559
|
+
const effectiveCart = useMemo(() => {
|
|
560
|
+
if (lines && lines.length > 0) {
|
|
561
|
+
return createMockCartFromLines(lines, cart);
|
|
562
|
+
}
|
|
563
|
+
return cart;
|
|
564
|
+
}, [lines, cart]);
|
|
477
565
|
useEffect(() => {
|
|
478
566
|
if (locale === "au") {
|
|
479
567
|
const isPointsSubscribe = Cookies5.get("points_subscribe");
|
|
@@ -495,14 +583,14 @@ var useScriptAutoFreeGift = ({
|
|
|
495
583
|
upgrade_multiple2 = 1.2;
|
|
496
584
|
upgrade_value2 = 40;
|
|
497
585
|
}
|
|
498
|
-
|
|
586
|
+
effectiveCart?.lineItems?.forEach(({ customAttributes }) => {
|
|
499
587
|
customAttributes?.forEach(({ key, value }) => {
|
|
500
588
|
if (key === "_amount_upgrade_multiple") upgrade_multiple2 = Number(value) || 1;
|
|
501
589
|
if (key === "_amount_upgrade_value") upgrade_value2 = Number(value) || 0;
|
|
502
590
|
});
|
|
503
591
|
});
|
|
504
592
|
return [upgrade_multiple2, upgrade_value2];
|
|
505
|
-
}, [
|
|
593
|
+
}, [effectiveCart?.lineItems, points_subscribe]);
|
|
506
594
|
const breakpoints = useMemo(() => {
|
|
507
595
|
if (!isActivityAvailable) return [];
|
|
508
596
|
return (campaign?.breakpoints || []).map((item) => ({
|
|
@@ -530,7 +618,7 @@ var useScriptAutoFreeGift = ({
|
|
|
530
618
|
}, [giftHandles]);
|
|
531
619
|
const involvedLines = useMemo(() => {
|
|
532
620
|
if (!isActivityAvailable) return [];
|
|
533
|
-
return (
|
|
621
|
+
return (effectiveCart?.lineItems || []).filter((line) => {
|
|
534
622
|
const isNotGift = line?.totalAmount && Number(line.totalAmount) > 0 && line.customAttributes?.every(
|
|
535
623
|
(item) => item.key !== _giveaway
|
|
536
624
|
);
|
|
@@ -539,7 +627,7 @@ var useScriptAutoFreeGift = ({
|
|
|
539
627
|
);
|
|
540
628
|
return isNotGift && hasCampaignTag;
|
|
541
629
|
});
|
|
542
|
-
}, [
|
|
630
|
+
}, [effectiveCart?.lineItems, isActivityAvailable, _giveaway]);
|
|
543
631
|
const involvedSubTotal = useMemo(() => {
|
|
544
632
|
if (!isActivityAvailable) return new Decimal2(0);
|
|
545
633
|
return involvedLines.reduce((prev, item) => {
|
|
@@ -926,6 +1014,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
|
|
|
926
1014
|
if (!resultCart) {
|
|
927
1015
|
return void 0;
|
|
928
1016
|
}
|
|
1017
|
+
console.log("npm addCartLines resultCart", resultCart);
|
|
929
1018
|
if (resultCart.discountCodes && resultCart.discountCodes.length > 0) {
|
|
930
1019
|
const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
|
|
931
1020
|
if (unapplicableCodes.length > 0) {
|
|
@@ -1099,6 +1188,60 @@ function useBuyNow({ withTrack = true } = {}, swrOptions) {
|
|
|
1099
1188
|
);
|
|
1100
1189
|
return useSWRMutation("buy-now", buyNow, swrOptions);
|
|
1101
1190
|
}
|
|
1191
|
+
function useCalcGiftsFromLines({
|
|
1192
|
+
lines,
|
|
1193
|
+
customer,
|
|
1194
|
+
scriptGiveawayKey = CUSTOMER_SCRIPT_GIFT_KEY
|
|
1195
|
+
}) {
|
|
1196
|
+
const { locale } = useShopify();
|
|
1197
|
+
const { cart, autoFreeGiftConfig, gradientGiftsConfig } = useCartContext();
|
|
1198
|
+
const functionGift = useCalcAutoFreeGift(cart, autoFreeGiftConfig || [], customer, lines);
|
|
1199
|
+
const scriptGift = useScriptAutoFreeGift({
|
|
1200
|
+
campaign: gradientGiftsConfig || null,
|
|
1201
|
+
_giveaway: scriptGiveawayKey,
|
|
1202
|
+
cart,
|
|
1203
|
+
locale,
|
|
1204
|
+
lines
|
|
1205
|
+
});
|
|
1206
|
+
const allGiftLines = useMemo(() => {
|
|
1207
|
+
const functionGiftLines = functionGift.qualifyingGift?.itemsToAdd || [];
|
|
1208
|
+
const scriptGiftLines = scriptGift.freeGiftLevel ? scriptGift.freeGiftLevel.giveawayProducts.map((product) => {
|
|
1209
|
+
const giftProduct = scriptGift.giftProductsResult?.find(
|
|
1210
|
+
(p) => p.handle === product.handle
|
|
1211
|
+
);
|
|
1212
|
+
const variant = giftProduct?.variants?.[0];
|
|
1213
|
+
return {
|
|
1214
|
+
variant: {
|
|
1215
|
+
id: variant?.id || "",
|
|
1216
|
+
handle: product.handle,
|
|
1217
|
+
sku: product.sku
|
|
1218
|
+
},
|
|
1219
|
+
quantity: 1,
|
|
1220
|
+
attributes: [
|
|
1221
|
+
{
|
|
1222
|
+
key: scriptGiveawayKey,
|
|
1223
|
+
value: "true"
|
|
1224
|
+
}
|
|
1225
|
+
]
|
|
1226
|
+
};
|
|
1227
|
+
}).filter((item) => item.variant.id) : [];
|
|
1228
|
+
return [...functionGiftLines, ...scriptGiftLines];
|
|
1229
|
+
}, [
|
|
1230
|
+
functionGift.qualifyingGift,
|
|
1231
|
+
scriptGift.freeGiftLevel,
|
|
1232
|
+
scriptGift.giftProductsResult,
|
|
1233
|
+
scriptGiveawayKey
|
|
1234
|
+
]);
|
|
1235
|
+
const hasGifts = useMemo(() => {
|
|
1236
|
+
return allGiftLines.length > 0;
|
|
1237
|
+
}, [allGiftLines]);
|
|
1238
|
+
return {
|
|
1239
|
+
functionGift,
|
|
1240
|
+
scriptGift,
|
|
1241
|
+
allGiftLines,
|
|
1242
|
+
hasGifts
|
|
1243
|
+
};
|
|
1244
|
+
}
|
|
1102
1245
|
|
|
1103
1246
|
// src/hooks/cart/types/order-discount.ts
|
|
1104
1247
|
var OrderDiscountType = /* @__PURE__ */ ((OrderDiscountType2) => {
|
|
@@ -3061,6 +3204,7 @@ function CartProvider({
|
|
|
3061
3204
|
isCodeChanging,
|
|
3062
3205
|
setIsCodeChanging,
|
|
3063
3206
|
autoFreeGiftConfig,
|
|
3207
|
+
gradientGiftsConfig,
|
|
3064
3208
|
setLoadingState,
|
|
3065
3209
|
loadingState,
|
|
3066
3210
|
// function满赠
|
|
@@ -3084,6 +3228,7 @@ function CartProvider({
|
|
|
3084
3228
|
locale,
|
|
3085
3229
|
isCodeChanging,
|
|
3086
3230
|
autoFreeGiftConfig,
|
|
3231
|
+
gradientGiftsConfig,
|
|
3087
3232
|
loadingState,
|
|
3088
3233
|
// function满赠
|
|
3089
3234
|
functionAutoFreeGift,
|
|
@@ -3107,6 +3252,6 @@ function useCartContext() {
|
|
|
3107
3252
|
return context;
|
|
3108
3253
|
}
|
|
3109
3254
|
|
|
3110
|
-
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, atobID, browserCartCookieAdapter, browserCookieAdapter, btoaID, checkAttributesUpdateNeeded, clearGeoLocationCache, currencyCodeMapping, defaultSWRMutationConfiguration, formatFunctionAutoFreeGift, formatScriptAutoFreeGift, gaTrack, getCachedGeoLocation, getDiscountEnvAttributeValue, getMatchedMainProductSubTotal, getQuery, getReferralAttributes, preCheck, safeParse, trackAddToCartFBQ, trackAddToCartGA, trackBeginCheckoutGA, trackBuyNowFBQ, trackBuyNowGA, useAddCartLines, useAddPlusMemberProductsToCart, useAddToCart, useAllBlogs, useAllCollections, useAllProducts, useApplyCartCodes, useArticle, useArticles, useArticlesInBlog, useAutoRemovePlusMemberInCart, useBlog, useBuyNow, useCalcAutoFreeGift, useCalcOrderDiscount, useCartAttributes, useCartContext, 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, useShopify, useSite, useUpdateCartAttributes, useUpdateCartLines, useUpdateLineCodeAmountAttributes, useUpdateVariantQuery, useVariant, useVariantMedia };
|
|
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, atobID, browserCartCookieAdapter, browserCookieAdapter, btoaID, checkAttributesUpdateNeeded, clearGeoLocationCache, createMockCartFromLines, currencyCodeMapping, defaultSWRMutationConfiguration, formatFunctionAutoFreeGift, formatScriptAutoFreeGift, gaTrack, getCachedGeoLocation, getDiscountEnvAttributeValue, getMatchedMainProductSubTotal, getQuery, getReferralAttributes, normalizeAddToCartLines, preCheck, safeParse, trackAddToCartFBQ, trackAddToCartGA, trackBeginCheckoutGA, trackBuyNowFBQ, trackBuyNowGA, useAddCartLines, useAddPlusMemberProductsToCart, useAddToCart, useAllBlogs, useAllCollections, useAllProducts, useApplyCartCodes, useArticle, useArticles, useArticlesInBlog, useAutoRemovePlusMemberInCart, useBlog, useBuyNow, useCalcAutoFreeGift, useCalcGiftsFromLines, useCalcOrderDiscount, useCartAttributes, useCartContext, 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, useShopify, useSite, useUpdateCartAttributes, useUpdateCartLines, useUpdateLineCodeAmountAttributes, useUpdateVariantQuery, useVariant, useVariantMedia };
|
|
3111
3256
|
//# sourceMappingURL=index.mjs.map
|
|
3112
3257
|
//# sourceMappingURL=index.mjs.map
|