@anker-in/shopify-react 1.2.3 → 1.2.4-beta.0

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
@@ -2463,6 +2483,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2463
2483
  const { trigger: applyCartCodes } = useApplyCartCodes();
2464
2484
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
2465
2485
  const { trigger: addCartLines2 } = useAddCartLines();
2486
+ const { trigger: updateCartLines3 } = useUpdateCartLines();
2466
2487
  const { trigger: createCart4 } = useCreateCart({
2467
2488
  updateCookie: true
2468
2489
  });
@@ -2515,10 +2536,36 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2515
2536
  // 初次加购时,就把所有 cart attributes 带上
2516
2537
  });
2517
2538
  } else {
2518
- resultCart = await addCartLines2({
2519
- cartId,
2520
- lines
2539
+ const linesToUpdate = [];
2540
+ const linesToAddNew = lines.filter((lineToAdd) => {
2541
+ const existingLine = cart?.lineItems?.find(
2542
+ (item) => item.variant?.id === lineToAdd.merchandiseId
2543
+ );
2544
+ if (existingLine) {
2545
+ linesToUpdate.push({
2546
+ id: existingLine.id,
2547
+ quantity: existingLine.quantity + (lineToAdd.quantity || 1),
2548
+ attributes: lineToAdd.attributes || void 0
2549
+ });
2550
+ return false;
2551
+ }
2552
+ return true;
2521
2553
  });
2554
+ if (linesToUpdate.length > 0) {
2555
+ resultCart = await updateCartLines3({
2556
+ cartId,
2557
+ lines: linesToUpdate
2558
+ });
2559
+ }
2560
+ if (linesToAddNew.length > 0) {
2561
+ resultCart = await addCartLines2({
2562
+ cartId: resultCart?.id || cartId,
2563
+ lines: linesToAddNew
2564
+ });
2565
+ }
2566
+ if (!resultCart) {
2567
+ resultCart = cart;
2568
+ }
2522
2569
  console.log("npm addCartLines resultCart", resultCart);
2523
2570
  if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
2524
2571
  const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
@@ -2579,6 +2626,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2579
2626
  performanceAdapter,
2580
2627
  createCart4,
2581
2628
  addCartLines2,
2629
+ updateCartLines3,
2582
2630
  applyCartCodes,
2583
2631
  removeInvalidCodes,
2584
2632
  addCustomAttributes,
@@ -2588,26 +2636,6 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2588
2636
  );
2589
2637
  return useSWRMutation__default.default("add-to-cart", addToCart, swrOptions);
2590
2638
  }
2591
- function useUpdateCartLines(options) {
2592
- const { client, locale, cartCookieAdapter } = useShopify();
2593
- const { mutateCart, metafieldIdentifiers } = useCartContext();
2594
- const updateLines = react.useCallback(
2595
- async (_key, { arg }) => {
2596
- const updatedCart = await shopifySdk.updateCartLines(client, {
2597
- ...arg,
2598
- metafieldIdentifiers,
2599
- cookieAdapter: cartCookieAdapter
2600
- });
2601
- if (updatedCart) {
2602
- mutateCart(updatedCart);
2603
- }
2604
- console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
2605
- return updatedCart;
2606
- },
2607
- [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
2608
- );
2609
- return useSWRMutation__default.default("update-cart-lines", updateLines, options);
2610
- }
2611
2639
  function useUpdateCartAttributes({
2612
2640
  mutate,
2613
2641
  metafieldIdentifiers,