@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.mjs CHANGED
@@ -1,9 +1,10 @@
1
1
  import { createContext, useMemo, useContext, useRef, useState, useEffect, useCallback } from 'react';
2
- import { createShopifyClient, atobID, btoaID, getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, updateCartAttributes, getLocalStorage, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, getCart, setLocalStorage, updateCartDeliveryOptions } from '@anker-in/shopify-sdk';
2
+ import { createShopifyClient, getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, updateCartAttributes, getLocalStorage, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, getCart, setLocalStorage, updateCartDeliveryOptions } from '@anker-in/shopify-sdk';
3
3
  export * 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 useSWRMutation from 'swr/mutation';
9
10
  import { useRequest } from 'ahooks';
@@ -140,9 +141,10 @@ function normalizeAddToCartLines(lines) {
140
141
  const variant = line.variant;
141
142
  const product = variant.product;
142
143
  const quantity = line.quantity || 1;
143
- const price = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
144
- const subtotalAmount = price * quantity;
145
- const totalAmount = subtotalAmount;
144
+ const originalPrice = variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
145
+ const finalPrice = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : originalPrice;
146
+ const subtotalAmount = originalPrice * quantity;
147
+ const totalAmount = finalPrice * quantity;
146
148
  return {
147
149
  id: `temp-line-${index}-${variant.id}`,
148
150
  // Temporary ID for pre-cart lines
@@ -156,7 +158,7 @@ function normalizeAddToCartLines(lines) {
156
158
  customAttributes: line.attributes || [],
157
159
  variant: {
158
160
  id: variant.id,
159
- price,
161
+ price: variant.price?.amount ? Number(variant.price.amount) : 0,
160
162
  listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
161
163
  sku: variant.sku || "",
162
164
  name: variant.title || "",
@@ -187,12 +189,14 @@ function createMockCartFromLines(lines, existingCart) {
187
189
  const normalizedLines = normalizeAddToCartLines(lines);
188
190
  const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
189
191
  const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
192
+ console.log("lines createMockCartFromLines", lines);
193
+ const currency = lines[0]?.variant?.price?.currencyCode;
190
194
  return {
191
195
  id: existingCart?.id || "temp-cart-id",
192
196
  customerId: existingCart?.customerId,
193
197
  email: existingCart?.email,
194
198
  createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
195
- currency: existingCart?.currency || { code: "USD" },
199
+ currency: existingCart?.currency?.code || { code: currency },
196
200
  taxesIncluded: existingCart?.taxesIncluded,
197
201
  lineItems: normalizedLines,
198
202
  totalLineItemsDiscount: 0,
@@ -462,11 +466,20 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
462
466
  return { qualifyingGift: null, nextTierGoal: null };
463
467
  }
464
468
  const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
465
- const qualifyingTier = [...giftTiers].sort((a, b) => Number(b.spend_sum_money) - Number(a.spend_sum_money)).find((tier) => subtotal >= Number(tier.spend_sum_money));
466
- const nextGoal = giftTiers.find((tier) => subtotal < Number(tier.spend_sum_money));
469
+ const currentCurrency = effectiveCart?.currency?.code || "";
470
+ console.log("currentCurrency useCalcAutoFreeGift", effectiveCart, currentCurrency);
471
+ const getThresholdAmount = (tier) => {
472
+ if (tier.spend_sum_money_multi_markets?.[currentCurrency]?.value) {
473
+ return Number(tier.spend_sum_money_multi_markets[currentCurrency].value);
474
+ }
475
+ return Number(tier.spend_sum_money || 0);
476
+ };
477
+ const qualifyingTier = [...giftTiers].sort((a, b) => getThresholdAmount(b) - getThresholdAmount(a)).find((tier) => subtotal >= getThresholdAmount(tier));
478
+ const nextGoal = giftTiers.find((tier) => subtotal < getThresholdAmount(tier));
467
479
  if (!qualifyingTier) {
468
480
  return { qualifyingGift: null, nextTierGoal: nextGoal || null };
469
481
  }
482
+ const actualThreshold = getThresholdAmount(qualifyingTier);
470
483
  const formattedGift = {
471
484
  tier: qualifyingTier,
472
485
  itemsToAdd: qualifyingTier.reward_list?.map((reward) => {
@@ -485,7 +498,10 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
485
498
  value: JSON.stringify({
486
499
  is_gift: true,
487
500
  rule_id: activeCampaign.rule_id,
488
- spend_sum_money: qualifyingTier.spend_sum_money
501
+ spend_sum_money: actualThreshold,
502
+ // 使用实际的门槛金额(多币种支持)
503
+ currency_code: currentCurrency
504
+ // 记录当前币种
489
505
  })
490
506
  }
491
507
  ]
@@ -493,7 +509,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
493
509
  }).filter((item) => item !== null)
494
510
  };
495
511
  return { qualifyingGift: formattedGift, nextTierGoal: nextGoal || null };
496
- }, [activeCampaign, subtotal]);
512
+ }, [activeCampaign, subtotal, effectiveCart]);
497
513
  const giftHandles = useMemo(() => {
498
514
  const giftVariant = autoFreeGiftConfig.map(
499
515
  (item) => item.rule_result?.spend_get_reward?.gift_product?.map(
@@ -944,6 +960,12 @@ function useApplyCartCodes(options) {
944
960
  cookieAdapter: cartCookieAdapter,
945
961
  metafieldIdentifiers
946
962
  });
963
+ const unApplicableCodes = discountCodes.filter(
964
+ (code) => updatedCart?.discountCodes?.find((item) => item.code === code && !item.applicable)
965
+ );
966
+ if (unApplicableCodes.length) {
967
+ throw new Error(`${unApplicableCodes.join(", ")} is not applicable to the cart`);
968
+ }
947
969
  if (updatedCart) {
948
970
  mutateCart(updatedCart);
949
971
  }
@@ -1300,9 +1322,12 @@ var useCalcOrderDiscount = (cart, orderDiscountConfig, customer) => {
1300
1322
  discountAmount: 0
1301
1323
  };
1302
1324
  }
1303
- const tieredDiscounts = activeCampaign.result_detail.order_discount_conf.tiered_discounts;
1304
- const qualifyingTier = [...tieredDiscounts].reverse().find((tier) => subtotal >= Number(tier.amount));
1305
- const nextGoal = tieredDiscounts.find((tier) => subtotal < Number(tier.amount));
1325
+ const currentCurrency = cart?.currency?.code || "";
1326
+ console.log("currentCurrency", cart, currentCurrency);
1327
+ const orderDiscountConf = activeCampaign.result_detail.order_discount_conf;
1328
+ const tieredDiscounts = orderDiscountConf.tiered_discounts_markets?.[currentCurrency] || orderDiscountConf.tiered_discounts;
1329
+ const qualifyingTier = [...tieredDiscounts].sort((a, b) => Number(b.amount) - Number(a.amount)).find((tier) => subtotal >= Number(tier.amount));
1330
+ const nextGoal = [...tieredDiscounts].sort((a, b) => Number(a.amount) - Number(b.amount)).find((tier) => subtotal < Number(tier.amount));
1306
1331
  if (!qualifyingTier) {
1307
1332
  return {
1308
1333
  qualifyingDiscount: null,