@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/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 useSWRMutation = require('swr/mutation');
10
11
  var ahooks = require('ahooks');
@@ -148,9 +149,10 @@ function normalizeAddToCartLines(lines) {
148
149
  const variant = line.variant;
149
150
  const product = variant.product;
150
151
  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;
152
+ const originalPrice = variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
153
+ const finalPrice = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : originalPrice;
154
+ const subtotalAmount = originalPrice * quantity;
155
+ const totalAmount = finalPrice * quantity;
154
156
  return {
155
157
  id: `temp-line-${index}-${variant.id}`,
156
158
  // Temporary ID for pre-cart lines
@@ -164,7 +166,7 @@ function normalizeAddToCartLines(lines) {
164
166
  customAttributes: line.attributes || [],
165
167
  variant: {
166
168
  id: variant.id,
167
- price,
169
+ price: variant.price?.amount ? Number(variant.price.amount) : 0,
168
170
  listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
169
171
  sku: variant.sku || "",
170
172
  name: variant.title || "",
@@ -195,12 +197,14 @@ function createMockCartFromLines(lines, existingCart) {
195
197
  const normalizedLines = normalizeAddToCartLines(lines);
196
198
  const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
197
199
  const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
200
+ console.log("lines createMockCartFromLines", lines);
201
+ const currency = lines[0]?.variant?.price?.currencyCode;
198
202
  return {
199
203
  id: existingCart?.id || "temp-cart-id",
200
204
  customerId: existingCart?.customerId,
201
205
  email: existingCart?.email,
202
206
  createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
203
- currency: existingCart?.currency || { code: "USD" },
207
+ currency: existingCart?.currency?.code || { code: currency },
204
208
  taxesIncluded: existingCart?.taxesIncluded,
205
209
  lineItems: normalizedLines,
206
210
  totalLineItemsDiscount: 0,
@@ -239,7 +243,7 @@ var getMatchedMainProductSubTotal = (cartData, variant_list, main_product) => {
239
243
  const matchedList = cartData?.lineItems?.filter((line) => {
240
244
  const { is_gift } = getDiscountEnvAttributeValue(line.customAttributes);
241
245
  return isAllStoreVariant ? !is_gift : variant_list?.find((item) => {
242
- return !is_gift && shopifySdk.atobID(line.variantId) === item;
246
+ return !is_gift && shopifyCore.atobID(line.variantId) === item;
243
247
  });
244
248
  });
245
249
  return matchedList?.reduce((acc, line) => {
@@ -470,11 +474,20 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
470
474
  return { qualifyingGift: null, nextTierGoal: null };
471
475
  }
472
476
  const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
473
- const qualifyingTier = [...giftTiers].sort((a, b) => Number(b.spend_sum_money) - Number(a.spend_sum_money)).find((tier) => subtotal >= Number(tier.spend_sum_money));
474
- const nextGoal = giftTiers.find((tier) => subtotal < Number(tier.spend_sum_money));
477
+ const currentCurrency = effectiveCart?.currency?.code || "";
478
+ console.log("currentCurrency useCalcAutoFreeGift", effectiveCart, currentCurrency);
479
+ const getThresholdAmount = (tier) => {
480
+ if (tier.spend_sum_money_multi_markets?.[currentCurrency]?.value) {
481
+ return Number(tier.spend_sum_money_multi_markets[currentCurrency].value);
482
+ }
483
+ return Number(tier.spend_sum_money || 0);
484
+ };
485
+ const qualifyingTier = [...giftTiers].sort((a, b) => getThresholdAmount(b) - getThresholdAmount(a)).find((tier) => subtotal >= getThresholdAmount(tier));
486
+ const nextGoal = giftTiers.find((tier) => subtotal < getThresholdAmount(tier));
475
487
  if (!qualifyingTier) {
476
488
  return { qualifyingGift: null, nextTierGoal: nextGoal || null };
477
489
  }
490
+ const actualThreshold = getThresholdAmount(qualifyingTier);
478
491
  const formattedGift = {
479
492
  tier: qualifyingTier,
480
493
  itemsToAdd: qualifyingTier.reward_list?.map((reward) => {
@@ -482,7 +495,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
482
495
  if (!giftProduct) return null;
483
496
  return {
484
497
  variant: {
485
- id: shopifySdk.btoaID(giftProduct.variant_id),
498
+ id: shopifyCore.btoaID(giftProduct.variant_id),
486
499
  handle: giftProduct.handle,
487
500
  sku: giftProduct.sku
488
501
  },
@@ -493,7 +506,10 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
493
506
  value: JSON.stringify({
494
507
  is_gift: true,
495
508
  rule_id: activeCampaign.rule_id,
496
- spend_sum_money: qualifyingTier.spend_sum_money
509
+ spend_sum_money: actualThreshold,
510
+ // 使用实际的门槛金额(多币种支持)
511
+ currency_code: currentCurrency
512
+ // 记录当前币种
497
513
  })
498
514
  }
499
515
  ]
@@ -501,7 +517,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
501
517
  }).filter((item) => item !== null)
502
518
  };
503
519
  return { qualifyingGift: formattedGift, nextTierGoal: nextGoal || null };
504
- }, [activeCampaign, subtotal]);
520
+ }, [activeCampaign, subtotal, effectiveCart]);
505
521
  const giftHandles = react.useMemo(() => {
506
522
  const giftVariant = autoFreeGiftConfig.map(
507
523
  (item) => item.rule_result?.spend_get_reward?.gift_product?.map(
@@ -952,6 +968,12 @@ function useApplyCartCodes(options) {
952
968
  cookieAdapter: cartCookieAdapter,
953
969
  metafieldIdentifiers
954
970
  });
971
+ const unApplicableCodes = discountCodes.filter(
972
+ (code) => updatedCart?.discountCodes?.find((item) => item.code === code && !item.applicable)
973
+ );
974
+ if (unApplicableCodes.length) {
975
+ throw new Error(`${unApplicableCodes.join(", ")} is not applicable to the cart`);
976
+ }
955
977
  if (updatedCart) {
956
978
  mutateCart(updatedCart);
957
979
  }
@@ -1308,9 +1330,12 @@ var useCalcOrderDiscount = (cart, orderDiscountConfig, customer) => {
1308
1330
  discountAmount: 0
1309
1331
  };
1310
1332
  }
1311
- const tieredDiscounts = activeCampaign.result_detail.order_discount_conf.tiered_discounts;
1312
- const qualifyingTier = [...tieredDiscounts].reverse().find((tier) => subtotal >= Number(tier.amount));
1313
- const nextGoal = tieredDiscounts.find((tier) => subtotal < Number(tier.amount));
1333
+ const currentCurrency = cart?.currency?.code || "";
1334
+ console.log("currentCurrency", cart, currentCurrency);
1335
+ const orderDiscountConf = activeCampaign.result_detail.order_discount_conf;
1336
+ const tieredDiscounts = orderDiscountConf.tiered_discounts_markets?.[currentCurrency] || orderDiscountConf.tiered_discounts;
1337
+ const qualifyingTier = [...tieredDiscounts].sort((a, b) => Number(b.amount) - Number(a.amount)).find((tier) => subtotal >= Number(tier.amount));
1338
+ const nextGoal = [...tieredDiscounts].sort((a, b) => Number(a.amount) - Number(b.amount)).find((tier) => subtotal < Number(tier.amount));
1314
1339
  if (!qualifyingTier) {
1315
1340
  return {
1316
1341
  qualifyingDiscount: null,