@anker-in/shopify-react 1.2.3 → 1.2.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.
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 } 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
@@ -2455,6 +2475,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2455
2475
  const { trigger: applyCartCodes } = useApplyCartCodes();
2456
2476
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
2457
2477
  const { trigger: addCartLines2 } = useAddCartLines();
2478
+ const { trigger: updateCartLines3 } = useUpdateCartLines();
2458
2479
  const { trigger: createCart4 } = useCreateCart({
2459
2480
  updateCookie: true
2460
2481
  });
@@ -2507,10 +2528,36 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2507
2528
  // 初次加购时,就把所有 cart attributes 带上
2508
2529
  });
2509
2530
  } else {
2510
- resultCart = await addCartLines2({
2511
- cartId,
2512
- lines
2531
+ const linesToUpdate = [];
2532
+ const linesToAddNew = lines.filter((lineToAdd) => {
2533
+ const existingLine = cart?.lineItems?.find(
2534
+ (item) => item.variant?.id === lineToAdd.merchandiseId
2535
+ );
2536
+ if (existingLine) {
2537
+ linesToUpdate.push({
2538
+ id: existingLine.id,
2539
+ quantity: existingLine.quantity + (lineToAdd.quantity || 1),
2540
+ attributes: lineToAdd.attributes || void 0
2541
+ });
2542
+ return false;
2543
+ }
2544
+ return true;
2513
2545
  });
2546
+ if (linesToUpdate.length > 0) {
2547
+ resultCart = await updateCartLines3({
2548
+ cartId,
2549
+ lines: linesToUpdate
2550
+ });
2551
+ }
2552
+ if (linesToAddNew.length > 0) {
2553
+ resultCart = await addCartLines2({
2554
+ cartId: resultCart?.id || cartId,
2555
+ lines: linesToAddNew
2556
+ });
2557
+ }
2558
+ if (!resultCart) {
2559
+ resultCart = cart;
2560
+ }
2514
2561
  console.log("npm addCartLines resultCart", resultCart);
2515
2562
  if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
2516
2563
  const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
@@ -2571,6 +2618,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2571
2618
  performanceAdapter,
2572
2619
  createCart4,
2573
2620
  addCartLines2,
2621
+ updateCartLines3,
2574
2622
  applyCartCodes,
2575
2623
  removeInvalidCodes,
2576
2624
  addCustomAttributes,
@@ -2580,26 +2628,6 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
2580
2628
  );
2581
2629
  return useSWRMutation("add-to-cart", addToCart, swrOptions);
2582
2630
  }
2583
- function useUpdateCartLines(options) {
2584
- const { client, locale, cartCookieAdapter } = useShopify();
2585
- const { mutateCart, metafieldIdentifiers } = useCartContext();
2586
- const updateLines = useCallback(
2587
- async (_key, { arg }) => {
2588
- const updatedCart = await updateCartLines(client, {
2589
- ...arg,
2590
- metafieldIdentifiers,
2591
- cookieAdapter: cartCookieAdapter
2592
- });
2593
- if (updatedCart) {
2594
- mutateCart(updatedCart);
2595
- }
2596
- console.log("use-update-cart-lines updatedCart", metafieldIdentifiers, updatedCart);
2597
- return updatedCart;
2598
- },
2599
- [client, locale, cartCookieAdapter, mutateCart, metafieldIdentifiers]
2600
- );
2601
- return useSWRMutation("update-cart-lines", updateLines, options);
2602
- }
2603
2631
  function useUpdateCartAttributes({
2604
2632
  mutate,
2605
2633
  metafieldIdentifiers,