@anker-in/shopify-react 0.1.1-beta.2 → 0.1.1-beta.4

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.
@@ -95,6 +95,81 @@ var CODE_AMOUNT_KEY = "_sku_code_money";
95
95
  var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
96
96
  var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
97
97
 
98
+ // src/hooks/cart/utils/normalize-add-to-cart-lines.ts
99
+ function normalizeAddToCartLines(lines) {
100
+ return lines.filter((line) => line.variant?.id).map((line, index) => {
101
+ const variant = line.variant;
102
+ const product = variant.product;
103
+ 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;
107
+ return {
108
+ id: `temp-line-${index}-${variant.id}`,
109
+ // Temporary ID for pre-cart lines
110
+ name: product?.title || variant.title || "",
111
+ quantity,
112
+ variantId: variant.id,
113
+ productId: product?.id || variant.id.split("/").slice(0, -2).join("/"),
114
+ totalAmount,
115
+ subtotalAmount,
116
+ discountAllocations: [],
117
+ customAttributes: line.attributes || [],
118
+ variant: {
119
+ id: variant.id,
120
+ price,
121
+ listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
122
+ sku: variant.sku || "",
123
+ name: variant.title || "",
124
+ image: variant.image ? {
125
+ url: variant.image.url,
126
+ altText: variant.image.altText || void 0
127
+ } : void 0,
128
+ requiresShipping: false,
129
+ // Default value, not available in NormalizedProductVariant
130
+ availableForSale: variant.availableForSale ?? true,
131
+ quantityAvailable: variant.quantityAvailable ?? 0,
132
+ currentlyNotInStock: false,
133
+ // Default value, will be updated when added to cart
134
+ weight: variant.weight,
135
+ metafields: variant.metafields
136
+ },
137
+ product,
138
+ path: product?.handle ? `/products/${product.handle}` : "",
139
+ discounts: [],
140
+ options: variant.selectedOptions?.map((opt) => ({
141
+ name: opt.name,
142
+ value: opt.value
143
+ }))
144
+ };
145
+ });
146
+ }
147
+ function createMockCartFromLines(lines, existingCart) {
148
+ const normalizedLines = normalizeAddToCartLines(lines);
149
+ const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
150
+ const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
151
+ return {
152
+ id: existingCart?.id || "temp-cart-id",
153
+ customerId: existingCart?.customerId,
154
+ email: existingCart?.email,
155
+ createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
156
+ currency: existingCart?.currency || { code: "USD" },
157
+ taxesIncluded: existingCart?.taxesIncluded,
158
+ lineItems: normalizedLines,
159
+ totallineItemsDiscount: 0,
160
+ orderDiscounts: 0,
161
+ lineItemsSubtotalPrice: subtotalPrice,
162
+ subtotalPrice,
163
+ totalPrice,
164
+ totalTaxAmount: 0,
165
+ discountCodes: existingCart?.discountCodes || [],
166
+ discountAllocations: [],
167
+ url: existingCart?.url || "",
168
+ ready: true,
169
+ customAttributes: existingCart?.customAttributes
170
+ };
171
+ }
172
+
98
173
  // src/hooks/cart/utils/index.ts
99
174
  var getQuery = () => {
100
175
  const url = typeof window !== "undefined" ? window.location.search : "";
@@ -319,12 +394,15 @@ var formatFunctionAutoFreeGift = ({
319
394
  };
320
395
  return result;
321
396
  };
322
- var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
397
+ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
323
398
  const tags = useMemo(() => customer?.tags || [], [customer?.tags]);
324
399
  const isCustomerLoading = useMemo(() => !customer ? true : false, [customer]);
325
400
  const dealsType = "";
326
401
  const { client, locale } = useShopify();
327
402
  const giftProductsCache = useRef(null);
403
+ const effectiveCart = useMemo(() => {
404
+ return cart;
405
+ }, [lines, cart]);
328
406
  const { activeCampaign, subtotal } = useMemo(() => {
329
407
  for (const campaign of autoFreeGiftConfig) {
330
408
  const { rule_conditions = [], rule_result } = campaign;
@@ -332,7 +410,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
332
410
  const isPreCheckPassed = preCheck(rule_conditions, tags, []);
333
411
  if (isPreCheckPassed && spend_get_reward) {
334
412
  const matchedSubtotal = getMatchedMainProductSubTotal(
335
- cart,
413
+ effectiveCart,
336
414
  spend_get_reward.main_product?.variant_list?.map((v) => v.variant_id) || [],
337
415
  {
338
416
  spend_money_type: spend_get_reward.main_product?.spend_money_type || 1,
@@ -346,13 +424,13 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
346
424
  }
347
425
  }
348
426
  return { activeCampaign: null, subtotal: 0 };
349
- }, [autoFreeGiftConfig, cart, tags, dealsType]);
427
+ }, [autoFreeGiftConfig, effectiveCart, tags, dealsType]);
350
428
  const { qualifyingGift, nextTierGoal } = useMemo(() => {
351
429
  if (!activeCampaign || !activeCampaign.rule_result?.spend_get_reward?.gift_product) {
352
430
  return { qualifyingGift: null, nextTierGoal: null };
353
431
  }
354
432
  const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
355
- const qualifyingTier = [...giftTiers].reverse().find((tier) => subtotal >= Number(tier.spend_sum_money));
433
+ const qualifyingTier = [...giftTiers].sort((a, b) => Number(b.spend_sum_money) - Number(a.spend_sum_money)).find((tier) => subtotal >= Number(tier.spend_sum_money));
356
434
  const nextGoal = giftTiers.find((tier) => subtotal < Number(tier.spend_sum_money));
357
435
  if (!qualifyingTier) {
358
436
  return { qualifyingGift: null, nextTierGoal: nextGoal || null };
@@ -429,12 +507,19 @@ var useScriptAutoFreeGift = ({
429
507
  campaign,
430
508
  _giveaway,
431
509
  cart,
432
- locale: providedLocale
510
+ locale: providedLocale,
511
+ lines
433
512
  }) => {
434
513
  const { client, locale: contextLocale } = useShopify();
435
514
  const locale = providedLocale || contextLocale;
436
515
  const [points_subscribe, set_points_subscribe] = useState(false);
437
516
  const giftProductsCache = useRef(null);
517
+ const effectiveCart = useMemo(() => {
518
+ if (lines && lines.length > 0) {
519
+ return createMockCartFromLines(lines, cart);
520
+ }
521
+ return cart;
522
+ }, [lines, cart]);
438
523
  useEffect(() => {
439
524
  if (locale === "au") {
440
525
  const isPointsSubscribe = Cookies5.get("points_subscribe");
@@ -456,14 +541,14 @@ var useScriptAutoFreeGift = ({
456
541
  upgrade_multiple2 = 1.2;
457
542
  upgrade_value2 = 40;
458
543
  }
459
- cart?.lineItems?.forEach(({ customAttributes }) => {
544
+ effectiveCart?.lineItems?.forEach(({ customAttributes }) => {
460
545
  customAttributes?.forEach(({ key, value }) => {
461
546
  if (key === "_amount_upgrade_multiple") upgrade_multiple2 = Number(value) || 1;
462
547
  if (key === "_amount_upgrade_value") upgrade_value2 = Number(value) || 0;
463
548
  });
464
549
  });
465
550
  return [upgrade_multiple2, upgrade_value2];
466
- }, [cart?.lineItems, points_subscribe]);
551
+ }, [effectiveCart?.lineItems, points_subscribe]);
467
552
  const breakpoints = useMemo(() => {
468
553
  if (!isActivityAvailable) return [];
469
554
  return (campaign?.breakpoints || []).map((item) => ({
@@ -491,7 +576,7 @@ var useScriptAutoFreeGift = ({
491
576
  }, [giftHandles]);
492
577
  const involvedLines = useMemo(() => {
493
578
  if (!isActivityAvailable) return [];
494
- return (cart?.lineItems || []).filter((line) => {
579
+ return (effectiveCart?.lineItems || []).filter((line) => {
495
580
  const isNotGift = line?.totalAmount && Number(line.totalAmount) > 0 && line.customAttributes?.every(
496
581
  (item) => item.key !== _giveaway
497
582
  );
@@ -500,7 +585,7 @@ var useScriptAutoFreeGift = ({
500
585
  );
501
586
  return isNotGift && hasCampaignTag;
502
587
  });
503
- }, [cart?.lineItems, isActivityAvailable, _giveaway]);
588
+ }, [effectiveCart?.lineItems, isActivityAvailable, _giveaway]);
504
589
  const involvedSubTotal = useMemo(() => {
505
590
  if (!isActivityAvailable) return new Decimal2(0);
506
591
  return involvedLines.reduce((prev, item) => {
@@ -1083,6 +1168,7 @@ function CartProvider({
1083
1168
  isCodeChanging,
1084
1169
  setIsCodeChanging,
1085
1170
  autoFreeGiftConfig,
1171
+ gradientGiftsConfig,
1086
1172
  setLoadingState,
1087
1173
  loadingState,
1088
1174
  // function满赠
@@ -1106,6 +1192,7 @@ function CartProvider({
1106
1192
  locale,
1107
1193
  isCodeChanging,
1108
1194
  autoFreeGiftConfig,
1195
+ gradientGiftsConfig,
1109
1196
  loadingState,
1110
1197
  // function满赠
1111
1198
  functionAutoFreeGift,