@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.
@@ -994,6 +994,26 @@ function useRemoveCartCodes(options) {
994
994
  );
995
995
  return useSWRMutation__default.default("remove-codes", removeCodes, options);
996
996
  }
997
+ function useUpdateCartLines(options) {
998
+ const { client, locale, cartCookieAdapter } = useShopify();
999
+ const { mutateCart, metafieldIdentifiers } = useCartContext();
1000
+ const updateLines = react.useCallback(
1001
+ async (_key, { arg }) => {
1002
+ const updatedCart = await shopifySdk.updateCartLines(client, {
1003
+ ...arg,
1004
+ metafieldIdentifiers,
1005
+ cookieAdapter: cartCookieAdapter
1006
+ });
1007
+ if (updatedCart) {
1008
+ mutateCart(updatedCart);
1009
+ }
1010
+ console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
1011
+ return updatedCart;
1012
+ },
1013
+ [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
1014
+ );
1015
+ return useSWRMutation__default.default("update-cart-lines", updateLines, options);
1016
+ }
997
1017
  var initSameLinesAttributes = ({
998
1018
  cart,
999
1019
  line
@@ -2294,6 +2314,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2294
2314
  const { trigger: applyCartCodes } = useApplyCartCodes();
2295
2315
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
2296
2316
  const { trigger: addCartLines2 } = useAddCartLines();
2317
+ const { trigger: updateCartLines3 } = useUpdateCartLines();
2297
2318
  const { trigger: createCart4 } = useCreateCart({
2298
2319
  updateCookie: true
2299
2320
  });
@@ -2346,10 +2367,36 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2346
2367
  // 初次加购时,就把所有 cart attributes 带上
2347
2368
  });
2348
2369
  } else {
2349
- resultCart = await addCartLines2({
2350
- cartId,
2351
- lines
2370
+ const linesToUpdate = [];
2371
+ const linesToAddNew = lines.filter((lineToAdd) => {
2372
+ const existingLine = cart?.lineItems?.find(
2373
+ (item) => item.variant?.id === lineToAdd.merchandiseId
2374
+ );
2375
+ if (existingLine) {
2376
+ linesToUpdate.push({
2377
+ id: existingLine.id,
2378
+ quantity: existingLine.quantity + (lineToAdd.quantity || 1),
2379
+ attributes: lineToAdd.attributes || void 0
2380
+ });
2381
+ return false;
2382
+ }
2383
+ return true;
2352
2384
  });
2385
+ if (linesToUpdate.length > 0) {
2386
+ resultCart = await updateCartLines3({
2387
+ cartId,
2388
+ lines: linesToUpdate
2389
+ });
2390
+ }
2391
+ if (linesToAddNew.length > 0) {
2392
+ resultCart = await addCartLines2({
2393
+ cartId: resultCart?.id || cartId,
2394
+ lines: linesToAddNew
2395
+ });
2396
+ }
2397
+ if (!resultCart) {
2398
+ resultCart = cart;
2399
+ }
2353
2400
  console.log("npm addCartLines resultCart", resultCart);
2354
2401
  if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
2355
2402
  const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
@@ -2410,6 +2457,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2410
2457
  performanceAdapter,
2411
2458
  createCart4,
2412
2459
  addCartLines2,
2460
+ updateCartLines3,
2413
2461
  applyCartCodes,
2414
2462
  removeInvalidCodes,
2415
2463
  addCustomAttributes,
@@ -2419,26 +2467,6 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2419
2467
  );
2420
2468
  return useSWRMutation__default.default("add-to-cart", addToCart, swrOptions);
2421
2469
  }
2422
- function useUpdateCartLines(options) {
2423
- const { client, locale, cartCookieAdapter } = useShopify();
2424
- const { mutateCart, metafieldIdentifiers } = useCartContext();
2425
- const updateLines = react.useCallback(
2426
- async (_key, { arg }) => {
2427
- const updatedCart = await shopifySdk.updateCartLines(client, {
2428
- ...arg,
2429
- metafieldIdentifiers,
2430
- cookieAdapter: cartCookieAdapter
2431
- });
2432
- if (updatedCart) {
2433
- mutateCart(updatedCart);
2434
- }
2435
- console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
2436
- return updatedCart;
2437
- },
2438
- [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
2439
- );
2440
- return useSWRMutation__default.default("update-cart-lines", updateLines, options);
2441
- }
2442
2470
  function useUpdateCartAttributes({
2443
2471
  mutate,
2444
2472
  metafieldIdentifiers,