@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.
@@ -96,6 +96,8 @@ interface CartContextValue {
96
96
  giftNeedAddToCartLines: any[];
97
97
  /** Auto free gift config */
98
98
  autoFreeGiftConfig?: any;
99
+ /** Gradient gifts config */
100
+ gradientGiftsConfig?: any;
99
101
  /** Metafield identifiers */
100
102
  metafieldIdentifiers?: {
101
103
  variant: HasMetafieldsIdentifier[];
@@ -96,6 +96,8 @@ interface CartContextValue {
96
96
  giftNeedAddToCartLines: any[];
97
97
  /** Auto free gift config */
98
98
  autoFreeGiftConfig?: any;
99
+ /** Gradient gifts config */
100
+ gradientGiftsConfig?: any;
99
101
  /** Metafield identifiers */
100
102
  metafieldIdentifiers?: {
101
103
  variant: HasMetafieldsIdentifier[];
@@ -104,6 +104,81 @@ var CODE_AMOUNT_KEY = "_sku_code_money";
104
104
  var SCRIPT_CODE_AMOUNT_KEY = "_code_money";
105
105
  var MAIN_PRODUCT_CODE = ["WS24", "WSTD", "WS7D", "WSCP", "WSPE", "WSPD"];
106
106
 
107
+ // src/hooks/cart/utils/normalize-add-to-cart-lines.ts
108
+ function normalizeAddToCartLines(lines) {
109
+ return lines.filter((line) => line.variant?.id).map((line, index) => {
110
+ const variant = line.variant;
111
+ const product = variant.product;
112
+ const quantity = line.quantity || 1;
113
+ const price = variant.finalPrice?.amount ? Number(variant.finalPrice.amount) : variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : variant.price?.amount ? Number(variant.price.amount) : 0;
114
+ const subtotalAmount = price * quantity;
115
+ const totalAmount = subtotalAmount;
116
+ return {
117
+ id: `temp-line-${index}-${variant.id}`,
118
+ // Temporary ID for pre-cart lines
119
+ name: product?.title || variant.title || "",
120
+ quantity,
121
+ variantId: variant.id,
122
+ productId: product?.id || variant.id.split("/").slice(0, -2).join("/"),
123
+ totalAmount,
124
+ subtotalAmount,
125
+ discountAllocations: [],
126
+ customAttributes: line.attributes || [],
127
+ variant: {
128
+ id: variant.id,
129
+ price,
130
+ listPrice: variant.compareAtPrice?.amount ? Number(variant.compareAtPrice.amount) : 0,
131
+ sku: variant.sku || "",
132
+ name: variant.title || "",
133
+ image: variant.image ? {
134
+ url: variant.image.url,
135
+ altText: variant.image.altText || void 0
136
+ } : void 0,
137
+ requiresShipping: false,
138
+ // Default value, not available in NormalizedProductVariant
139
+ availableForSale: variant.availableForSale ?? true,
140
+ quantityAvailable: variant.quantityAvailable ?? 0,
141
+ currentlyNotInStock: false,
142
+ // Default value, will be updated when added to cart
143
+ weight: variant.weight,
144
+ metafields: variant.metafields
145
+ },
146
+ product,
147
+ path: product?.handle ? `/products/${product.handle}` : "",
148
+ discounts: [],
149
+ options: variant.selectedOptions?.map((opt) => ({
150
+ name: opt.name,
151
+ value: opt.value
152
+ }))
153
+ };
154
+ });
155
+ }
156
+ function createMockCartFromLines(lines, existingCart) {
157
+ const normalizedLines = normalizeAddToCartLines(lines);
158
+ const subtotalPrice = normalizedLines.reduce((sum, line) => sum + line.subtotalAmount, 0);
159
+ const totalPrice = normalizedLines.reduce((sum, line) => sum + line.totalAmount, 0);
160
+ return {
161
+ id: existingCart?.id || "temp-cart-id",
162
+ customerId: existingCart?.customerId,
163
+ email: existingCart?.email,
164
+ createdAt: existingCart?.createdAt || (/* @__PURE__ */ new Date()).toISOString(),
165
+ currency: existingCart?.currency || { code: "USD" },
166
+ taxesIncluded: existingCart?.taxesIncluded,
167
+ lineItems: normalizedLines,
168
+ totallineItemsDiscount: 0,
169
+ orderDiscounts: 0,
170
+ lineItemsSubtotalPrice: subtotalPrice,
171
+ subtotalPrice,
172
+ totalPrice,
173
+ totalTaxAmount: 0,
174
+ discountCodes: existingCart?.discountCodes || [],
175
+ discountAllocations: [],
176
+ url: existingCart?.url || "",
177
+ ready: true,
178
+ customAttributes: existingCart?.customAttributes
179
+ };
180
+ }
181
+
107
182
  // src/hooks/cart/utils/index.ts
108
183
  var getQuery = () => {
109
184
  const url = typeof window !== "undefined" ? window.location.search : "";
@@ -328,12 +403,15 @@ var formatFunctionAutoFreeGift = ({
328
403
  };
329
404
  return result;
330
405
  };
331
- var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
406
+ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer, lines) => {
332
407
  const tags = react.useMemo(() => customer?.tags || [], [customer?.tags]);
333
408
  const isCustomerLoading = react.useMemo(() => !customer ? true : false, [customer]);
334
409
  const dealsType = "";
335
410
  const { client, locale } = useShopify();
336
411
  const giftProductsCache = react.useRef(null);
412
+ const effectiveCart = react.useMemo(() => {
413
+ return cart;
414
+ }, [lines, cart]);
337
415
  const { activeCampaign, subtotal } = react.useMemo(() => {
338
416
  for (const campaign of autoFreeGiftConfig) {
339
417
  const { rule_conditions = [], rule_result } = campaign;
@@ -341,7 +419,7 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
341
419
  const isPreCheckPassed = preCheck(rule_conditions, tags, []);
342
420
  if (isPreCheckPassed && spend_get_reward) {
343
421
  const matchedSubtotal = getMatchedMainProductSubTotal(
344
- cart,
422
+ effectiveCart,
345
423
  spend_get_reward.main_product?.variant_list?.map((v) => v.variant_id) || [],
346
424
  {
347
425
  spend_money_type: spend_get_reward.main_product?.spend_money_type || 1,
@@ -355,13 +433,13 @@ var useCalcAutoFreeGift = (cart, autoFreeGiftConfig, customer) => {
355
433
  }
356
434
  }
357
435
  return { activeCampaign: null, subtotal: 0 };
358
- }, [autoFreeGiftConfig, cart, tags, dealsType]);
436
+ }, [autoFreeGiftConfig, effectiveCart, tags, dealsType]);
359
437
  const { qualifyingGift, nextTierGoal } = react.useMemo(() => {
360
438
  if (!activeCampaign || !activeCampaign.rule_result?.spend_get_reward?.gift_product) {
361
439
  return { qualifyingGift: null, nextTierGoal: null };
362
440
  }
363
441
  const giftTiers = activeCampaign.rule_result.spend_get_reward.gift_product;
364
- const qualifyingTier = [...giftTiers].reverse().find((tier) => subtotal >= Number(tier.spend_sum_money));
442
+ const qualifyingTier = [...giftTiers].sort((a, b) => Number(b.spend_sum_money) - Number(a.spend_sum_money)).find((tier) => subtotal >= Number(tier.spend_sum_money));
365
443
  const nextGoal = giftTiers.find((tier) => subtotal < Number(tier.spend_sum_money));
366
444
  if (!qualifyingTier) {
367
445
  return { qualifyingGift: null, nextTierGoal: nextGoal || null };
@@ -438,12 +516,19 @@ var useScriptAutoFreeGift = ({
438
516
  campaign,
439
517
  _giveaway,
440
518
  cart,
441
- locale: providedLocale
519
+ locale: providedLocale,
520
+ lines
442
521
  }) => {
443
522
  const { client, locale: contextLocale } = useShopify();
444
523
  const locale = providedLocale || contextLocale;
445
524
  const [points_subscribe, set_points_subscribe] = react.useState(false);
446
525
  const giftProductsCache = react.useRef(null);
526
+ const effectiveCart = react.useMemo(() => {
527
+ if (lines && lines.length > 0) {
528
+ return createMockCartFromLines(lines, cart);
529
+ }
530
+ return cart;
531
+ }, [lines, cart]);
447
532
  react.useEffect(() => {
448
533
  if (locale === "au") {
449
534
  const isPointsSubscribe = Cookies5__default.default.get("points_subscribe");
@@ -465,14 +550,14 @@ var useScriptAutoFreeGift = ({
465
550
  upgrade_multiple2 = 1.2;
466
551
  upgrade_value2 = 40;
467
552
  }
468
- cart?.lineItems?.forEach(({ customAttributes }) => {
553
+ effectiveCart?.lineItems?.forEach(({ customAttributes }) => {
469
554
  customAttributes?.forEach(({ key, value }) => {
470
555
  if (key === "_amount_upgrade_multiple") upgrade_multiple2 = Number(value) || 1;
471
556
  if (key === "_amount_upgrade_value") upgrade_value2 = Number(value) || 0;
472
557
  });
473
558
  });
474
559
  return [upgrade_multiple2, upgrade_value2];
475
- }, [cart?.lineItems, points_subscribe]);
560
+ }, [effectiveCart?.lineItems, points_subscribe]);
476
561
  const breakpoints = react.useMemo(() => {
477
562
  if (!isActivityAvailable) return [];
478
563
  return (campaign?.breakpoints || []).map((item) => ({
@@ -500,7 +585,7 @@ var useScriptAutoFreeGift = ({
500
585
  }, [giftHandles]);
501
586
  const involvedLines = react.useMemo(() => {
502
587
  if (!isActivityAvailable) return [];
503
- return (cart?.lineItems || []).filter((line) => {
588
+ return (effectiveCart?.lineItems || []).filter((line) => {
504
589
  const isNotGift = line?.totalAmount && Number(line.totalAmount) > 0 && line.customAttributes?.every(
505
590
  (item) => item.key !== _giveaway
506
591
  );
@@ -509,7 +594,7 @@ var useScriptAutoFreeGift = ({
509
594
  );
510
595
  return isNotGift && hasCampaignTag;
511
596
  });
512
- }, [cart?.lineItems, isActivityAvailable, _giveaway]);
597
+ }, [effectiveCart?.lineItems, isActivityAvailable, _giveaway]);
513
598
  const involvedSubTotal = react.useMemo(() => {
514
599
  if (!isActivityAvailable) return new Decimal2__default.default(0);
515
600
  return involvedLines.reduce((prev, item) => {
@@ -1092,6 +1177,7 @@ function CartProvider({
1092
1177
  isCodeChanging,
1093
1178
  setIsCodeChanging,
1094
1179
  autoFreeGiftConfig,
1180
+ gradientGiftsConfig,
1095
1181
  setLoadingState,
1096
1182
  loadingState,
1097
1183
  // function满赠
@@ -1115,6 +1201,7 @@ function CartProvider({
1115
1201
  locale,
1116
1202
  isCodeChanging,
1117
1203
  autoFreeGiftConfig,
1204
+ gradientGiftsConfig,
1118
1205
  loadingState,
1119
1206
  // function满赠
1120
1207
  functionAutoFreeGift,