@anker-in/shopify-react 1.3.0-beta.3 → 1.3.0-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.
@@ -1165,6 +1165,26 @@ function useRemoveCartCodes(options) {
1165
1165
  );
1166
1166
  return useSWRMutation__default.default("remove-codes", removeCodes, options);
1167
1167
  }
1168
+ function useUpdateCartLines(options) {
1169
+ const { client, locale, cartCookieAdapter } = useShopify();
1170
+ const { mutateCart, metafieldIdentifiers } = useCartContext();
1171
+ const updateLines = react.useCallback(
1172
+ async (_key, { arg }) => {
1173
+ const updatedCart = await shopifySdk.updateCartLines(client, {
1174
+ ...arg,
1175
+ metafieldIdentifiers,
1176
+ cookieAdapter: cartCookieAdapter
1177
+ });
1178
+ if (updatedCart) {
1179
+ mutateCart(updatedCart);
1180
+ }
1181
+ console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
1182
+ return updatedCart;
1183
+ },
1184
+ [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
1185
+ );
1186
+ return useSWRMutation__default.default("update-cart-lines", updateLines, options);
1187
+ }
1168
1188
  var initSameLinesAttributes = ({
1169
1189
  cart,
1170
1190
  line
@@ -2326,6 +2346,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2326
2346
  const { trigger: applyCartCodes } = useApplyCartCodes();
2327
2347
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
2328
2348
  const { trigger: addCartLines2 } = useAddCartLines();
2349
+ const { trigger: updateCartLines3 } = useUpdateCartLines();
2329
2350
  const { trigger: createCart4 } = useCreateCart({
2330
2351
  updateCookie: true
2331
2352
  });
@@ -2378,10 +2399,36 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2378
2399
  // 初次加购时,就把所有 cart attributes 带上
2379
2400
  });
2380
2401
  } else {
2381
- resultCart = await addCartLines2({
2382
- cartId,
2383
- lines
2402
+ const linesToUpdate = [];
2403
+ const linesToAddNew = lines.filter((lineToAdd) => {
2404
+ const existingLine = cart?.lineItems?.find(
2405
+ (item) => item.variant?.id === lineToAdd.merchandiseId
2406
+ );
2407
+ if (existingLine) {
2408
+ linesToUpdate.push({
2409
+ id: existingLine.id,
2410
+ quantity: existingLine.quantity + (lineToAdd.quantity || 1),
2411
+ attributes: lineToAdd.attributes || void 0
2412
+ });
2413
+ return false;
2414
+ }
2415
+ return true;
2384
2416
  });
2417
+ if (linesToUpdate.length > 0) {
2418
+ resultCart = await updateCartLines3({
2419
+ cartId,
2420
+ lines: linesToUpdate
2421
+ });
2422
+ }
2423
+ if (linesToAddNew.length > 0) {
2424
+ resultCart = await addCartLines2({
2425
+ cartId: resultCart?.id || cartId,
2426
+ lines: linesToAddNew
2427
+ });
2428
+ }
2429
+ if (!resultCart) {
2430
+ resultCart = cart;
2431
+ }
2385
2432
  console.log("npm addCartLines resultCart", resultCart);
2386
2433
  if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
2387
2434
  const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
@@ -2442,6 +2489,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2442
2489
  performanceAdapter,
2443
2490
  createCart4,
2444
2491
  addCartLines2,
2492
+ updateCartLines3,
2445
2493
  applyCartCodes,
2446
2494
  removeInvalidCodes,
2447
2495
  addCustomAttributes,
@@ -2451,26 +2499,6 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2451
2499
  );
2452
2500
  return useSWRMutation__default.default("add-to-cart", addToCart, swrOptions);
2453
2501
  }
2454
- function useUpdateCartLines(options) {
2455
- const { client, locale, cartCookieAdapter } = useShopify();
2456
- const { mutateCart, metafieldIdentifiers } = useCartContext();
2457
- const updateLines = react.useCallback(
2458
- async (_key, { arg }) => {
2459
- const updatedCart = await shopifySdk.updateCartLines(client, {
2460
- ...arg,
2461
- metafieldIdentifiers,
2462
- cookieAdapter: cartCookieAdapter
2463
- });
2464
- if (updatedCart) {
2465
- mutateCart(updatedCart);
2466
- }
2467
- console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
2468
- return updatedCart;
2469
- },
2470
- [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
2471
- );
2472
- return useSWRMutation__default.default("update-cart-lines", updateLines, options);
2473
- }
2474
2502
  function useUpdateCartAttributes({
2475
2503
  mutate,
2476
2504
  metafieldIdentifiers,