@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.
@@ -1,6 +1,6 @@
1
1
  import { createContext, useMemo, useRef, useState, useEffect, useCallback, useContext } from 'react';
2
2
  import useSWRMutation from 'swr/mutation';
3
- import { getProductsByHandles, createCart, updateCartCodes, addCartLines, removeCartLines, getLocalStorage, updateCartLines, updateCartAttributes, updateBuyerIdentity, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage } from '@anker-in/shopify-sdk';
3
+ import { getProductsByHandles, createCart, updateCartCodes, addCartLines, updateCartLines, removeCartLines, getLocalStorage, updateCartAttributes, updateBuyerIdentity, getProduct, getAllProducts, getCollection, getAllCollections, getCollections, getBlog, getAllBlogs, getArticle, getArticles, getArticlesInBlog, setLocalStorage } from '@anker-in/shopify-sdk';
4
4
  import Cookies5 from 'js-cookie';
5
5
  import { jsx } from 'react/jsx-runtime';
6
6
  import Decimal3 from 'decimal.js';
@@ -985,6 +985,26 @@ function useRemoveCartCodes(options) {
985
985
  );
986
986
  return useSWRMutation("remove-codes", removeCodes, options);
987
987
  }
988
+ function useUpdateCartLines(options) {
989
+ const { client, locale, cartCookieAdapter } = useShopify();
990
+ const { mutateCart, metafieldIdentifiers } = useCartContext();
991
+ const updateLines = useCallback(
992
+ async (_key, { arg }) => {
993
+ const updatedCart = await updateCartLines(client, {
994
+ ...arg,
995
+ metafieldIdentifiers,
996
+ cookieAdapter: cartCookieAdapter
997
+ });
998
+ if (updatedCart) {
999
+ mutateCart(updatedCart);
1000
+ }
1001
+ console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
1002
+ return updatedCart;
1003
+ },
1004
+ [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
1005
+ );
1006
+ return useSWRMutation("update-cart-lines", updateLines, options);
1007
+ }
988
1008
  var initSameLinesAttributes = ({
989
1009
  cart,
990
1010
  line
@@ -2285,6 +2305,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2285
2305
  const { trigger: applyCartCodes } = useApplyCartCodes();
2286
2306
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
2287
2307
  const { trigger: addCartLines2 } = useAddCartLines();
2308
+ const { trigger: updateCartLines3 } = useUpdateCartLines();
2288
2309
  const { trigger: createCart4 } = useCreateCart({
2289
2310
  updateCookie: true
2290
2311
  });
@@ -2337,10 +2358,36 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2337
2358
  // 初次加购时,就把所有 cart attributes 带上
2338
2359
  });
2339
2360
  } else {
2340
- resultCart = await addCartLines2({
2341
- cartId,
2342
- lines
2361
+ const linesToUpdate = [];
2362
+ const linesToAddNew = lines.filter((lineToAdd) => {
2363
+ const existingLine = cart?.lineItems?.find(
2364
+ (item) => item.variant?.id === lineToAdd.merchandiseId
2365
+ );
2366
+ if (existingLine) {
2367
+ linesToUpdate.push({
2368
+ id: existingLine.id,
2369
+ quantity: existingLine.quantity + (lineToAdd.quantity || 1),
2370
+ attributes: lineToAdd.attributes || void 0
2371
+ });
2372
+ return false;
2373
+ }
2374
+ return true;
2343
2375
  });
2376
+ if (linesToUpdate.length > 0) {
2377
+ resultCart = await updateCartLines3({
2378
+ cartId,
2379
+ lines: linesToUpdate
2380
+ });
2381
+ }
2382
+ if (linesToAddNew.length > 0) {
2383
+ resultCart = await addCartLines2({
2384
+ cartId: resultCart?.id || cartId,
2385
+ lines: linesToAddNew
2386
+ });
2387
+ }
2388
+ if (!resultCart) {
2389
+ resultCart = cart;
2390
+ }
2344
2391
  console.log("npm addCartLines resultCart", resultCart);
2345
2392
  if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
2346
2393
  const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
@@ -2401,6 +2448,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2401
2448
  performanceAdapter,
2402
2449
  createCart4,
2403
2450
  addCartLines2,
2451
+ updateCartLines3,
2404
2452
  applyCartCodes,
2405
2453
  removeInvalidCodes,
2406
2454
  addCustomAttributes,
@@ -2410,26 +2458,6 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2410
2458
  );
2411
2459
  return useSWRMutation("add-to-cart", addToCart, swrOptions);
2412
2460
  }
2413
- function useUpdateCartLines(options) {
2414
- const { client, locale, cartCookieAdapter } = useShopify();
2415
- const { mutateCart, metafieldIdentifiers } = useCartContext();
2416
- const updateLines = useCallback(
2417
- async (_key, { arg }) => {
2418
- const updatedCart = await updateCartLines(client, {
2419
- ...arg,
2420
- metafieldIdentifiers,
2421
- cookieAdapter: cartCookieAdapter
2422
- });
2423
- if (updatedCart) {
2424
- mutateCart(updatedCart);
2425
- }
2426
- console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
2427
- return updatedCart;
2428
- },
2429
- [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
2430
- );
2431
- return useSWRMutation("update-cart-lines", updateLines, options);
2432
- }
2433
2461
  function useUpdateCartAttributes({
2434
2462
  mutate,
2435
2463
  metafieldIdentifiers,