@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.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createContext, useMemo, useContext, useRef, useState, useEffect, useCallback } from 'react';
2
- import { createShopifyClient, getProductsByHandles, createCart, updateCartCodes, addCartLines, removeCartLines, getLocalStorage, updateCartLines, updateCartAttributes, updateBuyerIdentity, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, getCart, setLocalStorage } from '@anker-in/shopify-sdk';
2
+ import { createShopifyClient, getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, getLocalStorage, updateCartAttributes, updateBuyerIdentity, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, getCart, setLocalStorage } from '@anker-in/shopify-sdk';
3
3
  export * from '@anker-in/shopify-sdk';
4
4
  import Cookies5 from 'js-cookie';
5
5
  import { jsx, jsxs } from 'react/jsx-runtime';
@@ -1155,6 +1155,26 @@ function useRemoveCartCodes(options) {
1155
1155
  );
1156
1156
  return useSWRMutation("remove-codes", removeCodes, options);
1157
1157
  }
1158
+ function useUpdateCartLines(options) {
1159
+ const { client, locale, cartCookieAdapter } = useShopify();
1160
+ const { mutateCart, metafieldIdentifiers } = useCartContext();
1161
+ const updateLines = useCallback(
1162
+ async (_key, { arg }) => {
1163
+ const updatedCart = await updateCartLines(client, {
1164
+ ...arg,
1165
+ metafieldIdentifiers,
1166
+ cookieAdapter: cartCookieAdapter
1167
+ });
1168
+ if (updatedCart) {
1169
+ mutateCart(updatedCart);
1170
+ }
1171
+ console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
1172
+ return updatedCart;
1173
+ },
1174
+ [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
1175
+ );
1176
+ return useSWRMutation("update-cart-lines", updateLines, options);
1177
+ }
1158
1178
  var initSameLinesAttributes = ({
1159
1179
  cart,
1160
1180
  line
@@ -2487,6 +2507,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2487
2507
  const { trigger: applyCartCodes } = useApplyCartCodes();
2488
2508
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
2489
2509
  const { trigger: addCartLines2 } = useAddCartLines();
2510
+ const { trigger: updateCartLines3 } = useUpdateCartLines();
2490
2511
  const { trigger: createCart4 } = useCreateCart({
2491
2512
  updateCookie: true
2492
2513
  });
@@ -2539,10 +2560,36 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2539
2560
  // 初次加购时,就把所有 cart attributes 带上
2540
2561
  });
2541
2562
  } else {
2542
- resultCart = await addCartLines2({
2543
- cartId,
2544
- lines
2563
+ const linesToUpdate = [];
2564
+ const linesToAddNew = lines.filter((lineToAdd) => {
2565
+ const existingLine = cart?.lineItems?.find(
2566
+ (item) => item.variant?.id === lineToAdd.merchandiseId
2567
+ );
2568
+ if (existingLine) {
2569
+ linesToUpdate.push({
2570
+ id: existingLine.id,
2571
+ quantity: existingLine.quantity + (lineToAdd.quantity || 1),
2572
+ attributes: lineToAdd.attributes || void 0
2573
+ });
2574
+ return false;
2575
+ }
2576
+ return true;
2545
2577
  });
2578
+ if (linesToUpdate.length > 0) {
2579
+ resultCart = await updateCartLines3({
2580
+ cartId,
2581
+ lines: linesToUpdate
2582
+ });
2583
+ }
2584
+ if (linesToAddNew.length > 0) {
2585
+ resultCart = await addCartLines2({
2586
+ cartId: resultCart?.id || cartId,
2587
+ lines: linesToAddNew
2588
+ });
2589
+ }
2590
+ if (!resultCart) {
2591
+ resultCart = cart;
2592
+ }
2546
2593
  console.log("npm addCartLines resultCart", resultCart);
2547
2594
  if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
2548
2595
  const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
@@ -2603,6 +2650,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2603
2650
  performanceAdapter,
2604
2651
  createCart4,
2605
2652
  addCartLines2,
2653
+ updateCartLines3,
2606
2654
  applyCartCodes,
2607
2655
  removeInvalidCodes,
2608
2656
  addCustomAttributes,
@@ -2612,26 +2660,6 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2612
2660
  );
2613
2661
  return useSWRMutation("add-to-cart", addToCart, swrOptions);
2614
2662
  }
2615
- function useUpdateCartLines(options) {
2616
- const { client, locale, cartCookieAdapter } = useShopify();
2617
- const { mutateCart, metafieldIdentifiers } = useCartContext();
2618
- const updateLines = useCallback(
2619
- async (_key, { arg }) => {
2620
- const updatedCart = await updateCartLines(client, {
2621
- ...arg,
2622
- metafieldIdentifiers,
2623
- cookieAdapter: cartCookieAdapter
2624
- });
2625
- if (updatedCart) {
2626
- mutateCart(updatedCart);
2627
- }
2628
- console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
2629
- return updatedCart;
2630
- },
2631
- [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
2632
- );
2633
- return useSWRMutation("update-cart-lines", updateLines, options);
2634
- }
2635
2663
  function useUpdateCartAttributes({
2636
2664
  mutate,
2637
2665
  metafieldIdentifiers,