@anker-in/shopify-react 0.1.1-beta.20 → 0.1.1-beta.22
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 +20 -0
- package/dist/hooks/index.d.ts +20 -0
- package/dist/hooks/index.js +39 -14
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +38 -13
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/index.js +39 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +38 -13
- package/dist/index.mjs.map +1 -1
- package/dist/provider/index.js +27 -11
- package/dist/provider/index.js.map +1 -1
- package/dist/provider/index.mjs +26 -10
- package/dist/provider/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/provider/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var shopifySdk = require('@anker-in/shopify-sdk');
|
|
|
5
5
|
var Cookies5 = require('js-cookie');
|
|
6
6
|
var jsxRuntime = require('react/jsx-runtime');
|
|
7
7
|
var Decimal2 = require('decimal.js');
|
|
8
|
+
var shopifyCore = require('@anker-in/shopify-core');
|
|
8
9
|
var useSWR = require('swr');
|
|
9
10
|
var useSWRMutation8 = require('swr/mutation');
|
|
10
11
|
var ahooks = require('ahooks');
|
|
@@ -110,9 +111,10 @@ function normalizeAddToCartLines(lines) {
|
|
|
110
111
|
const variant = line.variant;
|
|
111
112
|
const product = variant.product;
|
|
112
113
|
const quantity = line.quantity || 1;
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
const
|
|
114
|
+
const originalPrice = variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
|
|
115
|
+
const finalPrice = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : originalPrice;
|
|
116
|
+
const subtotalAmount = originalPrice * quantity;
|
|
117
|
+
const totalAmount = finalPrice * quantity;
|
|
116
118
|
return {
|
|
117
119
|
id: `temp-line-${index}-${variant.id}`,
|
|
118
120
|
// Temporary ID for pre-cart lines
|
|
@@ -126,7 +128,7 @@ function normalizeAddToCartLines(lines) {
|
|
|
126
128
|
customAttributes: line.attributes || [],
|
|
127
129
|
variant: {
|
|
128
130
|
id: variant.id,
|
|
129
|
-
price,
|
|
131
|
+
price: variant.price?.amount ? Number(variant.price.amount) : 0,
|
|
130
132
|
listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
|
|
131
133
|
sku: variant.sku || "",
|
|
132
134
|
name: variant.title || "",
|
|
@@ -157,12 +159,14 @@ function createMockCartFromLines(lines, existingCart) {
|
|
|
157
159
|
const normalizedLines = normalizeAddToCartLines(lines);
|
|
158
160
|
const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
|
|
159
161
|
const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
|
|
162
|
+
console.log("lines createMockCartFromLines", lines);
|
|
163
|
+
const currency = lines[0]?.variant?.price?.currencyCode;
|
|
160
164
|
return {
|
|
161
165
|
id: existingCart?.id || "temp-cart-id",
|
|
162
166
|
customerId: existingCart?.customerId,
|
|
163
167
|
email: existingCart?.email,
|
|
164
168
|
createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
165
|
-
currency: existingCart?.currency || { code:
|
|
169
|
+
currency: existingCart?.currency?.code || { code: currency },
|
|
166
170
|
taxesIncluded: existingCart?.taxesIncluded,
|
|
167
171
|
lineItems: normalizedLines,
|
|
168
172
|
totalLineItemsDiscount: 0,
|
|
@@ -201,7 +205,7 @@ var getMatchedMainProductSubTotal = (cartData, variant_list, main_product) => {
|
|
|
201
205
|
const matchedList = cartData?.lineItems?.filter((line) => {
|
|
202
206
|
const { is_gift } = getDiscountEnvAttributeValue(line.customAttributes);
|
|
203
207
|
return isAllStoreVariant ? !is_gift : variant_list?.find((item) => {
|
|
204
|
-
return !is_gift &&
|
|
208
|
+
return !is_gift && shopifyCore.atobID(line.variantId) === item;
|
|
205
209
|
});
|
|
206
210
|
});
|
|
207
211
|
return matchedList?.reduce((acc, line) => {
|
|
@@ -429,11 +433,20 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
429
433
|
return { qualifyingGift: null, nextTierGoal: null };
|
|
430
434
|
}
|
|
431
435
|
const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
|
|
432
|
-
const
|
|
433
|
-
|
|
436
|
+
const currentCurrency = effectiveCart?.currency?.code || "";
|
|
437
|
+
console.log("currentCurrency useCalcAutoFreeGift", effectiveCart, currentCurrency);
|
|
438
|
+
const getThresholdAmount = (tier) => {
|
|
439
|
+
if (tier.spend_sum_money_multi_markets?.[currentCurrency]?.value) {
|
|
440
|
+
return Number(tier.spend_sum_money_multi_markets[currentCurrency].value);
|
|
441
|
+
}
|
|
442
|
+
return Number(tier.spend_sum_money || 0);
|
|
443
|
+
};
|
|
444
|
+
const qualifyingTier = [...giftTiers].sort((a, b) => getThresholdAmount(b) - getThresholdAmount(a)).find((tier) => subtotal >= getThresholdAmount(tier));
|
|
445
|
+
const nextGoal = giftTiers.find((tier) => subtotal < getThresholdAmount(tier));
|
|
434
446
|
if (!qualifyingTier) {
|
|
435
447
|
return { qualifyingGift: null, nextTierGoal: nextGoal || null };
|
|
436
448
|
}
|
|
449
|
+
const actualThreshold = getThresholdAmount(qualifyingTier);
|
|
437
450
|
const formattedGift = {
|
|
438
451
|
tier: qualifyingTier,
|
|
439
452
|
itemsToAdd: qualifyingTier.reward_list?.map((reward) => {
|
|
@@ -441,7 +454,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
441
454
|
if (!giftProduct) return null;
|
|
442
455
|
return {
|
|
443
456
|
variant: {
|
|
444
|
-
id:
|
|
457
|
+
id: shopifyCore.btoaID(giftProduct.variant_id),
|
|
445
458
|
handle: giftProduct.handle,
|
|
446
459
|
sku: giftProduct.sku
|
|
447
460
|
},
|
|
@@ -452,7 +465,10 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
452
465
|
value: JSON.stringify({
|
|
453
466
|
is_gift: true,
|
|
454
467
|
rule_id: activeCampaign.rule_id,
|
|
455
|
-
spend_sum_money:
|
|
468
|
+
spend_sum_money: actualThreshold,
|
|
469
|
+
// 使用实际的门槛金额(多币种支持)
|
|
470
|
+
currency_code: currentCurrency
|
|
471
|
+
// 记录当前币种
|
|
456
472
|
})
|
|
457
473
|
}
|
|
458
474
|
]
|
|
@@ -460,7 +476,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
460
476
|
}).filter((item) => item !== null)
|
|
461
477
|
};
|
|
462
478
|
return { qualifyingGift: formattedGift, nextTierGoal: nextGoal || null };
|
|
463
|
-
}, [activeCampaign, subtotal]);
|
|
479
|
+
}, [activeCampaign, subtotal, effectiveCart]);
|
|
464
480
|
const giftHandles = react.useMemo(() => {
|
|
465
481
|
const giftVariant = autoFreeGiftConfig.map(
|
|
466
482
|
(item) => item.rule_result?.spend_get_reward?.gift_product?.map(
|