@anker-in/shopify-react 0.1.1-beta.35 → 0.1.1-beta.36

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.
@@ -430,54 +430,28 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
430
430
  }
431
431
  return { activeCampaign: null, subtotal: 0 };
432
432
  }, [autoFreeGiftConfig, effectiveCart, tags, dealsType]);
433
- const { qualifyingGift, nextTierGoal } = react.useMemo(() => {
433
+ const { qualifyingTier, nextTierGoal, actualThreshold, currentCurrency } = react.useMemo(() => {
434
434
  if (!activeCampaign || !activeCampaign.rule_result?.spend_get_reward?.gift_product) {
435
- return { qualifyingGift: null, nextTierGoal: null };
435
+ return { qualifyingTier: null, nextTierGoal: null, actualThreshold: 0, currentCurrency: "" };
436
436
  }
437
437
  const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
438
- const currentCurrency = effectiveCart?.currency?.code || "";
439
- console.log("currentCurrency useCalcAutoFreeGift", effectiveCart, currentCurrency);
438
+ const currentCurrency2 = effectiveCart?.currency?.code || "";
439
+ console.log("currentCurrency useCalcAutoFreeGift", effectiveCart, currentCurrency2);
440
440
  const getThresholdAmount = (tier) => {
441
- if (tier.spend_sum_money_multi_markets?.[currentCurrency]?.value) {
442
- return Number(tier.spend_sum_money_multi_markets[currentCurrency].value);
441
+ if (tier.spend_sum_money_multi_markets?.[currentCurrency2]?.value) {
442
+ return Number(tier.spend_sum_money_multi_markets[currentCurrency2].value);
443
443
  }
444
444
  return Number(tier.spend_sum_money || 0);
445
445
  };
446
- const qualifyingTier = [...giftTiers].sort((a, b) => getThresholdAmount(b) - getThresholdAmount(a)).find((tier) => subtotal >= getThresholdAmount(tier));
446
+ const qualifyingTier2 = [...giftTiers].sort((a, b) => getThresholdAmount(b) - getThresholdAmount(a)).find((tier) => subtotal >= getThresholdAmount(tier));
447
447
  const nextGoal = giftTiers.find((tier) => subtotal < getThresholdAmount(tier));
448
- if (!qualifyingTier) {
449
- return { qualifyingGift: null, nextTierGoal: nextGoal || null };
450
- }
451
- const actualThreshold = getThresholdAmount(qualifyingTier);
452
- const formattedGift = {
453
- tier: qualifyingTier,
454
- itemsToAdd: qualifyingTier.reward_list?.map((reward) => {
455
- const giftProduct = reward?.variant_list?.[0];
456
- if (!giftProduct) return null;
457
- return {
458
- variant: {
459
- id: shopifyCore.btoaID(giftProduct.variant_id),
460
- handle: giftProduct.handle,
461
- sku: giftProduct.sku
462
- },
463
- quantity: reward?.get_unit || 1,
464
- attributes: [
465
- {
466
- key: CUSTOMER_ATTRIBUTE_KEY,
467
- value: JSON.stringify({
468
- is_gift: true,
469
- rule_id: activeCampaign.rule_id,
470
- spend_sum_money: actualThreshold,
471
- // 使用实际的门槛金额(多币种支持)
472
- currency_code: currentCurrency
473
- // 记录当前币种
474
- })
475
- }
476
- ]
477
- };
478
- }).filter((item) => item !== null)
448
+ const actualThreshold2 = qualifyingTier2 ? getThresholdAmount(qualifyingTier2) : 0;
449
+ return {
450
+ qualifyingTier: qualifyingTier2,
451
+ nextTierGoal: nextGoal || null,
452
+ actualThreshold: actualThreshold2,
453
+ currentCurrency: currentCurrency2
479
454
  };
480
- return { qualifyingGift: formattedGift, nextTierGoal: nextGoal || null };
481
455
  }, [activeCampaign, subtotal, effectiveCart]);
482
456
  const giftHandles = react.useMemo(() => {
483
457
  const giftVariant = autoFreeGiftConfig.map(
@@ -518,6 +492,58 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
518
492
  }
519
493
  return giftProductsResult;
520
494
  }, [giftProductsResult, shouldFetch]);
495
+ const qualifyingGift = react.useMemo(() => {
496
+ if (!qualifyingTier || !activeCampaign) {
497
+ return null;
498
+ }
499
+ const itemsToAdd = qualifyingTier.reward_list?.map((reward) => {
500
+ if (!reward.variant_list || reward.variant_list.length === 0) {
501
+ return null;
502
+ }
503
+ let selectedGiftProduct = null;
504
+ for (const giftVariant of reward.variant_list) {
505
+ const productInfo = finalGiftProductsResult?.find(
506
+ (p) => p.handle === giftVariant.handle
507
+ );
508
+ if (productInfo) {
509
+ const variantInfo = productInfo.variants?.find((v) => v.sku === giftVariant.sku);
510
+ if (variantInfo?.availableForSale) {
511
+ selectedGiftProduct = giftVariant;
512
+ break;
513
+ }
514
+ }
515
+ }
516
+ if (!selectedGiftProduct) {
517
+ selectedGiftProduct = reward.variant_list[0];
518
+ }
519
+ return {
520
+ variant: {
521
+ id: shopifyCore.btoaID(selectedGiftProduct.variant_id),
522
+ handle: selectedGiftProduct.handle,
523
+ sku: selectedGiftProduct.sku
524
+ },
525
+ quantity: reward?.get_unit || 1,
526
+ attributes: [
527
+ {
528
+ key: CUSTOMER_ATTRIBUTE_KEY,
529
+ value: JSON.stringify({
530
+ is_gift: true,
531
+ rule_id: activeCampaign.rule_id,
532
+ spend_sum_money: actualThreshold,
533
+ // 使用实际的门槛金额(多币种支持)
534
+ currency_code: currentCurrency
535
+ // 记录当前币种
536
+ })
537
+ }
538
+ ]
539
+ };
540
+ }).filter((item) => item !== null);
541
+ const formattedGift = {
542
+ tier: qualifyingTier,
543
+ itemsToAdd
544
+ };
545
+ return formattedGift;
546
+ }, [qualifyingTier, activeCampaign, finalGiftProductsResult, actualThreshold, currentCurrency]);
521
547
  return {
522
548
  qualifyingGift,
523
549
  nextTierGoal,