@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/hooks/index.d.mts
CHANGED
|
@@ -558,12 +558,32 @@ type OrderDiscountConfig = {
|
|
|
558
558
|
handle: string;
|
|
559
559
|
}>;
|
|
560
560
|
all_store_variant: boolean;
|
|
561
|
+
skip_variants?: Array<{
|
|
562
|
+
variant_id: string;
|
|
563
|
+
sku: string;
|
|
564
|
+
handle: string;
|
|
565
|
+
}>;
|
|
561
566
|
};
|
|
562
567
|
order_discount_conf: {
|
|
563
568
|
base_price: OrderBasePriceType;
|
|
564
569
|
tiered_discounts: TieredDiscount[];
|
|
570
|
+
tiered_discounts_markets?: Record<string, TieredDiscount[]>;
|
|
571
|
+
is_reusable?: boolean;
|
|
572
|
+
min_quantity_items?: number;
|
|
573
|
+
max_apply_times_of_per_order?: number;
|
|
565
574
|
};
|
|
566
575
|
};
|
|
576
|
+
discount_label?: {
|
|
577
|
+
cart_checkout_label?: string;
|
|
578
|
+
product_label?: string;
|
|
579
|
+
sold_out_label?: string;
|
|
580
|
+
};
|
|
581
|
+
frontend_labels?: any[];
|
|
582
|
+
discount_combinations?: {
|
|
583
|
+
other_order_discounts?: boolean;
|
|
584
|
+
other_product_discounts?: boolean;
|
|
585
|
+
shipping_discounts?: boolean;
|
|
586
|
+
};
|
|
567
587
|
};
|
|
568
588
|
|
|
569
589
|
interface OrderDiscountResult {
|
package/dist/hooks/index.d.ts
CHANGED
|
@@ -558,12 +558,32 @@ type OrderDiscountConfig = {
|
|
|
558
558
|
handle: string;
|
|
559
559
|
}>;
|
|
560
560
|
all_store_variant: boolean;
|
|
561
|
+
skip_variants?: Array<{
|
|
562
|
+
variant_id: string;
|
|
563
|
+
sku: string;
|
|
564
|
+
handle: string;
|
|
565
|
+
}>;
|
|
561
566
|
};
|
|
562
567
|
order_discount_conf: {
|
|
563
568
|
base_price: OrderBasePriceType;
|
|
564
569
|
tiered_discounts: TieredDiscount[];
|
|
570
|
+
tiered_discounts_markets?: Record<string, TieredDiscount[]>;
|
|
571
|
+
is_reusable?: boolean;
|
|
572
|
+
min_quantity_items?: number;
|
|
573
|
+
max_apply_times_of_per_order?: number;
|
|
565
574
|
};
|
|
566
575
|
};
|
|
576
|
+
discount_label?: {
|
|
577
|
+
cart_checkout_label?: string;
|
|
578
|
+
product_label?: string;
|
|
579
|
+
sold_out_label?: string;
|
|
580
|
+
};
|
|
581
|
+
frontend_labels?: any[];
|
|
582
|
+
discount_combinations?: {
|
|
583
|
+
other_order_discounts?: boolean;
|
|
584
|
+
other_product_discounts?: boolean;
|
|
585
|
+
shipping_discounts?: boolean;
|
|
586
|
+
};
|
|
567
587
|
};
|
|
568
588
|
|
|
569
589
|
interface OrderDiscountResult {
|
package/dist/hooks/index.js
CHANGED
|
@@ -6,6 +6,7 @@ var shopifySdk = require('@anker-in/shopify-sdk');
|
|
|
6
6
|
var Cookies5 = require('js-cookie');
|
|
7
7
|
var jsxRuntime = require('react/jsx-runtime');
|
|
8
8
|
var Decimal2 = require('decimal.js');
|
|
9
|
+
var shopifyCore = require('@anker-in/shopify-core');
|
|
9
10
|
var useSWR = require('swr');
|
|
10
11
|
var ahooks = require('ahooks');
|
|
11
12
|
|
|
@@ -77,9 +78,10 @@ function normalizeAddToCartLines(lines) {
|
|
|
77
78
|
const variant = line.variant;
|
|
78
79
|
const product = variant.product;
|
|
79
80
|
const quantity = line.quantity || 1;
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
const
|
|
81
|
+
const originalPrice = variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
|
|
82
|
+
const finalPrice = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : originalPrice;
|
|
83
|
+
const subtotalAmount = originalPrice * quantity;
|
|
84
|
+
const totalAmount = finalPrice * quantity;
|
|
83
85
|
return {
|
|
84
86
|
id: `temp-line-${index}-${variant.id}`,
|
|
85
87
|
// Temporary ID for pre-cart lines
|
|
@@ -93,7 +95,7 @@ function normalizeAddToCartLines(lines) {
|
|
|
93
95
|
customAttributes: line.attributes || [],
|
|
94
96
|
variant: {
|
|
95
97
|
id: variant.id,
|
|
96
|
-
price,
|
|
98
|
+
price: variant.price?.amount ? Number(variant.price.amount) : 0,
|
|
97
99
|
listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
|
|
98
100
|
sku: variant.sku || "",
|
|
99
101
|
name: variant.title || "",
|
|
@@ -124,12 +126,14 @@ function createMockCartFromLines(lines, existingCart) {
|
|
|
124
126
|
const normalizedLines = normalizeAddToCartLines(lines);
|
|
125
127
|
const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
|
|
126
128
|
const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
|
|
129
|
+
console.log("lines createMockCartFromLines", lines);
|
|
130
|
+
const currency = lines[0]?.variant?.price?.currencyCode;
|
|
127
131
|
return {
|
|
128
132
|
id: existingCart?.id || "temp-cart-id",
|
|
129
133
|
customerId: existingCart?.customerId,
|
|
130
134
|
email: existingCart?.email,
|
|
131
135
|
createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
132
|
-
currency: existingCart?.currency || { code:
|
|
136
|
+
currency: existingCart?.currency?.code || { code: currency },
|
|
133
137
|
taxesIncluded: existingCart?.taxesIncluded,
|
|
134
138
|
lineItems: normalizedLines,
|
|
135
139
|
totalLineItemsDiscount: 0,
|
|
@@ -168,7 +172,7 @@ var getMatchedMainProductSubTotal = (cartData, variant_list, main_product) => {
|
|
|
168
172
|
const matchedList = cartData?.lineItems?.filter((line) => {
|
|
169
173
|
const { is_gift } = getDiscountEnvAttributeValue(line.customAttributes);
|
|
170
174
|
return isAllStoreVariant ? !is_gift : variant_list?.find((item) => {
|
|
171
|
-
return !is_gift &&
|
|
175
|
+
return !is_gift && shopifyCore.atobID(line.variantId) === item;
|
|
172
176
|
});
|
|
173
177
|
});
|
|
174
178
|
return matchedList?.reduce((acc, line) => {
|
|
@@ -399,11 +403,20 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
399
403
|
return { qualifyingGift: null, nextTierGoal: null };
|
|
400
404
|
}
|
|
401
405
|
const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
|
|
402
|
-
const
|
|
403
|
-
|
|
406
|
+
const currentCurrency = effectiveCart?.currency?.code || "";
|
|
407
|
+
console.log("currentCurrency useCalcAutoFreeGift", effectiveCart, currentCurrency);
|
|
408
|
+
const getThresholdAmount = (tier) => {
|
|
409
|
+
if (tier.spend_sum_money_multi_markets?.[currentCurrency]?.value) {
|
|
410
|
+
return Number(tier.spend_sum_money_multi_markets[currentCurrency].value);
|
|
411
|
+
}
|
|
412
|
+
return Number(tier.spend_sum_money || 0);
|
|
413
|
+
};
|
|
414
|
+
const qualifyingTier = [...giftTiers].sort((a, b) => getThresholdAmount(b) - getThresholdAmount(a)).find((tier) => subtotal >= getThresholdAmount(tier));
|
|
415
|
+
const nextGoal = giftTiers.find((tier) => subtotal < getThresholdAmount(tier));
|
|
404
416
|
if (!qualifyingTier) {
|
|
405
417
|
return { qualifyingGift: null, nextTierGoal: nextGoal || null };
|
|
406
418
|
}
|
|
419
|
+
const actualThreshold = getThresholdAmount(qualifyingTier);
|
|
407
420
|
const formattedGift = {
|
|
408
421
|
tier: qualifyingTier,
|
|
409
422
|
itemsToAdd: qualifyingTier.reward_list?.map((reward) => {
|
|
@@ -411,7 +424,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
411
424
|
if (!giftProduct) return null;
|
|
412
425
|
return {
|
|
413
426
|
variant: {
|
|
414
|
-
id:
|
|
427
|
+
id: shopifyCore.btoaID(giftProduct.variant_id),
|
|
415
428
|
handle: giftProduct.handle,
|
|
416
429
|
sku: giftProduct.sku
|
|
417
430
|
},
|
|
@@ -422,7 +435,10 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
422
435
|
value: JSON.stringify({
|
|
423
436
|
is_gift: true,
|
|
424
437
|
rule_id: activeCampaign.rule_id,
|
|
425
|
-
spend_sum_money:
|
|
438
|
+
spend_sum_money: actualThreshold,
|
|
439
|
+
// 使用实际的门槛金额(多币种支持)
|
|
440
|
+
currency_code: currentCurrency
|
|
441
|
+
// 记录当前币种
|
|
426
442
|
})
|
|
427
443
|
}
|
|
428
444
|
]
|
|
@@ -430,7 +446,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
|
|
|
430
446
|
}).filter((item) => item !== null)
|
|
431
447
|
};
|
|
432
448
|
return { qualifyingGift: formattedGift, nextTierGoal: nextGoal || null };
|
|
433
|
-
}, [activeCampaign, subtotal]);
|
|
449
|
+
}, [activeCampaign, subtotal, effectiveCart]);
|
|
434
450
|
const giftHandles = react.useMemo(() => {
|
|
435
451
|
const giftVariant = autoFreeGiftConfig.map(
|
|
436
452
|
(item) => item.rule_result?.spend_get_reward?.gift_product?.map(
|
|
@@ -856,6 +872,12 @@ function useApplyCartCodes(options) {
|
|
|
856
872
|
cookieAdapter: cartCookieAdapter,
|
|
857
873
|
metafieldIdentifiers
|
|
858
874
|
});
|
|
875
|
+
const unApplicableCodes = discountCodes.filter(
|
|
876
|
+
(code) => updatedCart?.discountCodes?.find((item) => item.code === code && !item.applicable)
|
|
877
|
+
);
|
|
878
|
+
if (unApplicableCodes.length) {
|
|
879
|
+
throw new Error(`${unApplicableCodes.join(", ")} is not applicable to the cart`);
|
|
880
|
+
}
|
|
859
881
|
if (updatedCart) {
|
|
860
882
|
mutateCart(updatedCart);
|
|
861
883
|
}
|
|
@@ -1212,9 +1234,12 @@ var useCalcOrderDiscount = (cart, orderDiscountConfig, customer) => {
|
|
|
1212
1234
|
discountAmount: 0
|
|
1213
1235
|
};
|
|
1214
1236
|
}
|
|
1215
|
-
const
|
|
1216
|
-
|
|
1217
|
-
const
|
|
1237
|
+
const currentCurrency = cart?.currency?.code || "";
|
|
1238
|
+
console.log("currentCurrency", cart, currentCurrency);
|
|
1239
|
+
const orderDiscountConf = activeCampaign.result_detail.order_discount_conf;
|
|
1240
|
+
const tieredDiscounts = orderDiscountConf.tiered_discounts_markets?.[currentCurrency] || orderDiscountConf.tiered_discounts;
|
|
1241
|
+
const qualifyingTier = [...tieredDiscounts].sort((a, b) => Number(b.amount) - Number(a.amount)).find((tier) => subtotal >= Number(tier.amount));
|
|
1242
|
+
const nextGoal = [...tieredDiscounts].sort((a, b) => Number(a.amount) - Number(b.amount)).find((tier) => subtotal < Number(tier.amount));
|
|
1218
1243
|
if (!qualifyingTier) {
|
|
1219
1244
|
return {
|
|
1220
1245
|
qualifyingDiscount: null,
|