@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,9 +1,10 @@
1
1
  import { createContext, useMemo, useRef, useState, useEffect, useCallback, useContext } from 'react';
2
2
  import useSWRMutation from 'swr/mutation';
3
- import { atobID, btoaID, getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, updateCartAttributes, getLocalStorage, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage, updateCartDeliveryOptions } from '@anker-in/shopify-sdk';
3
+ import { getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, updateCartAttributes, getLocalStorage, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage, updateCartDeliveryOptions } from '@anker-in/shopify-sdk';
4
4
  import Cookies5 from 'js-cookie';
5
5
  import { jsx } from 'react/jsx-runtime';
6
6
  import Decimal2 from 'decimal.js';
7
+ import { atobID, btoaID } from '@anker-in/shopify-core';
7
8
  import useSWR from 'swr';
8
9
  import { useRequest } from 'ahooks';
9
10
 
@@ -68,9 +69,10 @@ function normalizeAddToCartLines(lines) {
68
69
  const variant = line.variant;
69
70
  const product = variant.product;
70
71
  const quantity = line.quantity || 1;
71
- const price = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
72
- const subtotalAmount = price * quantity;
73
- const totalAmount = subtotalAmount;
72
+ const originalPrice = variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
73
+ const finalPrice = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : originalPrice;
74
+ const subtotalAmount = originalPrice * quantity;
75
+ const totalAmount = finalPrice * quantity;
74
76
  return {
75
77
  id: `temp-line-${index}-${variant.id}`,
76
78
  // Temporary ID for pre-cart lines
@@ -84,7 +86,7 @@ function normalizeAddToCartLines(lines) {
84
86
  customAttributes: line.attributes || [],
85
87
  variant: {
86
88
  id: variant.id,
87
- price,
89
+ price: variant.price?.amount ? Number(variant.price.amount) : 0,
88
90
  listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
89
91
  sku: variant.sku || "",
90
92
  name: variant.title || "",
@@ -115,12 +117,14 @@ function createMockCartFromLines(lines, existingCart) {
115
117
  const normalizedLines = normalizeAddToCartLines(lines);
116
118
  const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
117
119
  const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
120
+ console.log("lines createMockCartFromLines", lines);
121
+ const currency = lines[0]?.variant?.price?.currencyCode;
118
122
  return {
119
123
  id: existingCart?.id || "temp-cart-id",
120
124
  customerId: existingCart?.customerId,
121
125
  email: existingCart?.email,
122
126
  createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
123
- currency: existingCart?.currency || { code: "USD" },
127
+ currency: existingCart?.currency?.code || { code: currency },
124
128
  taxesIncluded: existingCart?.taxesIncluded,
125
129
  lineItems: normalizedLines,
126
130
  totalLineItemsDiscount: 0,
@@ -390,11 +394,20 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
390
394
  return { qualifyingGift: null, nextTierGoal: null };
391
395
  }
392
396
  const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
393
- const qualifyingTier = [...giftTiers].sort((a, b) => Number(b.spend_sum_money) - Number(a.spend_sum_money)).find((tier) => subtotal >= Number(tier.spend_sum_money));
394
- const nextGoal = giftTiers.find((tier) => subtotal < Number(tier.spend_sum_money));
397
+ const currentCurrency = effectiveCart?.currency?.code || "";
398
+ console.log("currentCurrency useCalcAutoFreeGift", effectiveCart, currentCurrency);
399
+ const getThresholdAmount = (tier) => {
400
+ if (tier.spend_sum_money_multi_markets?.[currentCurrency]?.value) {
401
+ return Number(tier.spend_sum_money_multi_markets[currentCurrency].value);
402
+ }
403
+ return Number(tier.spend_sum_money || 0);
404
+ };
405
+ const qualifyingTier = [...giftTiers].sort((a, b) => getThresholdAmount(b) - getThresholdAmount(a)).find((tier) => subtotal >= getThresholdAmount(tier));
406
+ const nextGoal = giftTiers.find((tier) => subtotal < getThresholdAmount(tier));
395
407
  if (!qualifyingTier) {
396
408
  return { qualifyingGift: null, nextTierGoal: nextGoal || null };
397
409
  }
410
+ const actualThreshold = getThresholdAmount(qualifyingTier);
398
411
  const formattedGift = {
399
412
  tier: qualifyingTier,
400
413
  itemsToAdd: qualifyingTier.reward_list?.map((reward) => {
@@ -413,7 +426,10 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
413
426
  value: JSON.stringify({
414
427
  is_gift: true,
415
428
  rule_id: activeCampaign.rule_id,
416
- spend_sum_money: qualifyingTier.spend_sum_money
429
+ spend_sum_money: actualThreshold,
430
+ // 使用实际的门槛金额(多币种支持)
431
+ currency_code: currentCurrency
432
+ // 记录当前币种
417
433
  })
418
434
  }
419
435
  ]
@@ -421,7 +437,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
421
437
  }).filter((item) => item !== null)
422
438
  };
423
439
  return { qualifyingGift: formattedGift, nextTierGoal: nextGoal || null };
424
- }, [activeCampaign, subtotal]);
440
+ }, [activeCampaign, subtotal, effectiveCart]);
425
441
  const giftHandles = useMemo(() => {
426
442
  const giftVariant = autoFreeGiftConfig.map(
427
443
  (item) => item.rule_result?.spend_get_reward?.gift_product?.map(
@@ -847,6 +863,12 @@ function useApplyCartCodes(options) {
847
863
  cookieAdapter: cartCookieAdapter,
848
864
  metafieldIdentifiers
849
865
  });
866
+ const unApplicableCodes = discountCodes.filter(
867
+ (code) => updatedCart?.discountCodes?.find((item) => item.code === code && !item.applicable)
868
+ );
869
+ if (unApplicableCodes.length) {
870
+ throw new Error(`${unApplicableCodes.join(", ")} is not applicable to the cart`);
871
+ }
850
872
  if (updatedCart) {
851
873
  mutateCart(updatedCart);
852
874
  }
@@ -1203,9 +1225,12 @@ var useCalcOrderDiscount = (cart, orderDiscountConfig, customer) => {
1203
1225
  discountAmount: 0
1204
1226
  };
1205
1227
  }
1206
- const tieredDiscounts = activeCampaign.result_detail.order_discount_conf.tiered_discounts;
1207
- const qualifyingTier = [...tieredDiscounts].reverse().find((tier) => subtotal >= Number(tier.amount));
1208
- const nextGoal = tieredDiscounts.find((tier) => subtotal < Number(tier.amount));
1228
+ const currentCurrency = cart?.currency?.code || "";
1229
+ console.log("currentCurrency", cart, currentCurrency);
1230
+ const orderDiscountConf = activeCampaign.result_detail.order_discount_conf;
1231
+ const tieredDiscounts = orderDiscountConf.tiered_discounts_markets?.[currentCurrency] || orderDiscountConf.tiered_discounts;
1232
+ const qualifyingTier = [...tieredDiscounts].sort((a, b) => Number(b.amount) - Number(a.amount)).find((tier) => subtotal >= Number(tier.amount));
1233
+ const nextGoal = [...tieredDiscounts].sort((a, b) => Number(a.amount) - Number(b.amount)).find((tier) => subtotal < Number(tier.amount));
1209
1234
  if (!qualifyingTier) {
1210
1235
  return {
1211
1236
  qualifyingDiscount: null,