@anker-in/shopify-react 1.3.0-beta.3 → 1.3.0-beta.5

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.js CHANGED
@@ -1163,6 +1163,26 @@ function useRemoveCartCodes(options) {
1163
1163
  );
1164
1164
  return useSWRMutation__default.default("remove-codes", removeCodes, options);
1165
1165
  }
1166
+ function useUpdateCartLines(options) {
1167
+ const { client, locale, cartCookieAdapter } = useShopify();
1168
+ const { mutateCart, metafieldIdentifiers } = useCartContext();
1169
+ const updateLines = react.useCallback(
1170
+ async (_key, { arg }) => {
1171
+ const updatedCart = await shopifySdk.updateCartLines(client, {
1172
+ ...arg,
1173
+ metafieldIdentifiers,
1174
+ cookieAdapter: cartCookieAdapter
1175
+ });
1176
+ if (updatedCart) {
1177
+ mutateCart(updatedCart);
1178
+ }
1179
+ console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
1180
+ return updatedCart;
1181
+ },
1182
+ [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
1183
+ );
1184
+ return useSWRMutation__default.default("update-cart-lines", updateLines, options);
1185
+ }
1166
1186
  var initSameLinesAttributes = ({
1167
1187
  cart,
1168
1188
  line
@@ -2495,6 +2515,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2495
2515
  const { trigger: applyCartCodes } = useApplyCartCodes();
2496
2516
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
2497
2517
  const { trigger: addCartLines2 } = useAddCartLines();
2518
+ const { trigger: updateCartLines3 } = useUpdateCartLines();
2498
2519
  const { trigger: createCart4 } = useCreateCart({
2499
2520
  updateCookie: true
2500
2521
  });
@@ -2547,10 +2568,36 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2547
2568
  // 初次加购时,就把所有 cart attributes 带上
2548
2569
  });
2549
2570
  } else {
2550
- resultCart = await addCartLines2({
2551
- cartId,
2552
- lines
2571
+ const linesToUpdate = [];
2572
+ const linesToAddNew = lines.filter((lineToAdd) => {
2573
+ const existingLine = cart?.lineItems?.find(
2574
+ (item) => item.variant?.id === lineToAdd.merchandiseId
2575
+ );
2576
+ if (existingLine) {
2577
+ linesToUpdate.push({
2578
+ id: existingLine.id,
2579
+ quantity: existingLine.quantity + (lineToAdd.quantity || 1),
2580
+ attributes: lineToAdd.attributes || void 0
2581
+ });
2582
+ return false;
2583
+ }
2584
+ return true;
2553
2585
  });
2586
+ if (linesToUpdate.length > 0) {
2587
+ resultCart = await updateCartLines3({
2588
+ cartId,
2589
+ lines: linesToUpdate
2590
+ });
2591
+ }
2592
+ if (linesToAddNew.length > 0) {
2593
+ resultCart = await addCartLines2({
2594
+ cartId: resultCart?.id || cartId,
2595
+ lines: linesToAddNew
2596
+ });
2597
+ }
2598
+ if (!resultCart) {
2599
+ resultCart = cart;
2600
+ }
2554
2601
  console.log("npm addCartLines resultCart", resultCart);
2555
2602
  if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
2556
2603
  const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
@@ -2611,6 +2658,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2611
2658
  performanceAdapter,
2612
2659
  createCart4,
2613
2660
  addCartLines2,
2661
+ updateCartLines3,
2614
2662
  applyCartCodes,
2615
2663
  removeInvalidCodes,
2616
2664
  addCustomAttributes,
@@ -2620,26 +2668,6 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2620
2668
  );
2621
2669
  return useSWRMutation__default.default("add-to-cart", addToCart, swrOptions);
2622
2670
  }
2623
- function useUpdateCartLines(options) {
2624
- const { client, locale, cartCookieAdapter } = useShopify();
2625
- const { mutateCart, metafieldIdentifiers } = useCartContext();
2626
- const updateLines = react.useCallback(
2627
- async (_key, { arg }) => {
2628
- const updatedCart = await shopifySdk.updateCartLines(client, {
2629
- ...arg,
2630
- metafieldIdentifiers,
2631
- cookieAdapter: cartCookieAdapter
2632
- });
2633
- if (updatedCart) {
2634
- mutateCart(updatedCart);
2635
- }
2636
- console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
2637
- return updatedCart;
2638
- },
2639
- [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
2640
- );
2641
- return useSWRMutation__default.default("update-cart-lines", updateLines, options);
2642
- }
2643
2671
  function useUpdateCartAttributes({
2644
2672
  mutate,
2645
2673
  metafieldIdentifiers,