@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.
@@ -1,8 +1,9 @@
1
1
  import { createContext, useMemo, useContext, useState, useCallback, useEffect, useRef } from 'react';
2
- import { createShopifyClient, getCart, updateCartAttributes, updateCartLines, btoaID, getProductsByHandles, getLocalStorage, atobID } from '@anker-in/shopify-sdk';
2
+ import { createShopifyClient, getCart, updateCartAttributes, updateCartLines, getProductsByHandles, getLocalStorage } from '@anker-in/shopify-sdk';
3
3
  import Cookies5 from 'js-cookie';
4
4
  import { jsx } from 'react/jsx-runtime';
5
5
  import Decimal2 from 'decimal.js';
6
+ import { btoaID, atobID } from '@anker-in/shopify-core';
6
7
  import useSWR from 'swr';
7
8
  import useSWRMutation8 from 'swr/mutation';
8
9
  import { useRequest } from 'ahooks';
@@ -101,9 +102,10 @@ function normalizeAddToCartLines(lines) {
101
102
  const variant = line.variant;
102
103
  const product = variant.product;
103
104
  const quantity = line.quantity || 1;
104
- const price = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
105
- const subtotalAmount = price * quantity;
106
- const totalAmount = subtotalAmount;
105
+ const originalPrice = variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
106
+ const finalPrice = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : originalPrice;
107
+ const subtotalAmount = originalPrice * quantity;
108
+ const totalAmount = finalPrice * quantity;
107
109
  return {
108
110
  id: `temp-line-${index}-${variant.id}`,
109
111
  // Temporary ID for pre-cart lines
@@ -117,7 +119,7 @@ function normalizeAddToCartLines(lines) {
117
119
  customAttributes: line.attributes || [],
118
120
  variant: {
119
121
  id: variant.id,
120
- price,
122
+ price: variant.price?.amount ? Number(variant.price.amount) : 0,
121
123
  listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
122
124
  sku: variant.sku || "",
123
125
  name: variant.title || "",
@@ -148,12 +150,14 @@ function createMockCartFromLines(lines, existingCart) {
148
150
  const normalizedLines = normalizeAddToCartLines(lines);
149
151
  const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
150
152
  const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
153
+ console.log("lines createMockCartFromLines", lines);
154
+ const currency = lines[0]?.variant?.price?.currencyCode;
151
155
  return {
152
156
  id: existingCart?.id || "temp-cart-id",
153
157
  customerId: existingCart?.customerId,
154
158
  email: existingCart?.email,
155
159
  createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
156
- currency: existingCart?.currency || { code: "USD" },
160
+ currency: existingCart?.currency?.code || { code: currency },
157
161
  taxesIncluded: existingCart?.taxesIncluded,
158
162
  lineItems: normalizedLines,
159
163
  totalLineItemsDiscount: 0,
@@ -420,11 +424,20 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
420
424
  return { qualifyingGift: null, nextTierGoal: null };
421
425
  }
422
426
  const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
423
- const qualifyingTier = [...giftTiers].sort((a, b) => Number(b.spend_sum_money) - Number(a.spend_sum_money)).find((tier) => subtotal >= Number(tier.spend_sum_money));
424
- const nextGoal = giftTiers.find((tier) => subtotal < Number(tier.spend_sum_money));
427
+ const currentCurrency = effectiveCart?.currency?.code || "";
428
+ console.log("currentCurrency useCalcAutoFreeGift", effectiveCart, currentCurrency);
429
+ const getThresholdAmount = (tier) => {
430
+ if (tier.spend_sum_money_multi_markets?.[currentCurrency]?.value) {
431
+ return Number(tier.spend_sum_money_multi_markets[currentCurrency].value);
432
+ }
433
+ return Number(tier.spend_sum_money || 0);
434
+ };
435
+ const qualifyingTier = [...giftTiers].sort((a, b) => getThresholdAmount(b) - getThresholdAmount(a)).find((tier) => subtotal >= getThresholdAmount(tier));
436
+ const nextGoal = giftTiers.find((tier) => subtotal < getThresholdAmount(tier));
425
437
  if (!qualifyingTier) {
426
438
  return { qualifyingGift: null, nextTierGoal: nextGoal || null };
427
439
  }
440
+ const actualThreshold = getThresholdAmount(qualifyingTier);
428
441
  const formattedGift = {
429
442
  tier: qualifyingTier,
430
443
  itemsToAdd: qualifyingTier.reward_list?.map((reward) => {
@@ -443,7 +456,10 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
443
456
  value: JSON.stringify({
444
457
  is_gift: true,
445
458
  rule_id: activeCampaign.rule_id,
446
- spend_sum_money: qualifyingTier.spend_sum_money
459
+ spend_sum_money: actualThreshold,
460
+ // 使用实际的门槛金额(多币种支持)
461
+ currency_code: currentCurrency
462
+ // 记录当前币种
447
463
  })
448
464
  }
449
465
  ]
@@ -451,7 +467,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
451
467
  }).filter((item) => item !== null)
452
468
  };
453
469
  return { qualifyingGift: formattedGift, nextTierGoal: nextGoal || null };
454
- }, [activeCampaign, subtotal]);
470
+ }, [activeCampaign, subtotal, effectiveCart]);
455
471
  const giftHandles = useMemo(() => {
456
472
  const giftVariant = autoFreeGiftConfig.map(
457
473
  (item) => item.rule_result?.spend_get_reward?.gift_product?.map(