@anker-in/shopify-react 0.1.1-beta.38 → 0.1.1-beta.39

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.
@@ -990,6 +990,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
990
990
  const { trigger: applyCartCodes } = useApplyCartCodes();
991
991
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
992
992
  const { trigger: addCartLines2 } = useAddCartLines();
993
+ const { trigger: createCart3 } = useCreateCart();
993
994
  const addToCart = useCallback(
994
995
  async (_key, { arg }) => {
995
996
  const {
@@ -1021,40 +1022,45 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1021
1022
  if (lines.length === 0) {
1022
1023
  return;
1023
1024
  }
1024
- const cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
1025
- let resultCart = await addCartLines2({
1026
- cartId,
1027
- lines,
1028
- buyerIdentity
1029
- });
1030
- if (!resultCart) {
1031
- performanceAdapter?.addToCartEnd();
1032
- return void 0;
1033
- }
1034
- console.log("npm addCartLines resultCart", resultCart);
1035
- if (resultCart.discountCodes && resultCart.discountCodes.length > 0) {
1036
- const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
1037
- if (unapplicableCodes.length > 0) {
1038
- if (onCodesInvalid) {
1039
- const handledCart = await onCodesInvalid(resultCart, unapplicableCodes);
1040
- if (handledCart) {
1041
- resultCart = handledCart;
1025
+ let cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
1026
+ let resultCart = null;
1027
+ if (!cartId) {
1028
+ resultCart = await createCart3({
1029
+ lines,
1030
+ buyerIdentity,
1031
+ discountCodes,
1032
+ customAttributes
1033
+ });
1034
+ } else {
1035
+ resultCart = await addCartLines2({
1036
+ cartId,
1037
+ lines
1038
+ });
1039
+ console.log("npm addCartLines resultCart", resultCart);
1040
+ if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
1041
+ const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
1042
+ if (unapplicableCodes.length > 0) {
1043
+ if (onCodesInvalid) {
1044
+ const handledCart = await onCodesInvalid(resultCart, unapplicableCodes);
1045
+ if (handledCart) {
1046
+ resultCart = handledCart;
1047
+ }
1048
+ } else {
1049
+ await removeInvalidCodes({
1050
+ discountCodes: unapplicableCodes
1051
+ });
1042
1052
  }
1043
- } else {
1044
- await removeInvalidCodes({
1045
- discountCodes: unapplicableCodes
1046
- });
1047
1053
  }
1048
1054
  }
1049
- }
1050
- if (discountCodes && discountCodes.length > 0) {
1051
- applyCartCodes({
1052
- replaceExistingCodes,
1053
- discountCodes
1054
- });
1055
- }
1056
- if (customAttributes && customAttributes.length > 0) {
1057
- addCustomAttributes(customAttributes);
1055
+ if (resultCart && discountCodes && discountCodes.length > 0) {
1056
+ applyCartCodes({
1057
+ replaceExistingCodes,
1058
+ discountCodes
1059
+ });
1060
+ }
1061
+ if (customAttributes && customAttributes.length > 0) {
1062
+ addCustomAttributes(customAttributes);
1063
+ }
1058
1064
  }
1059
1065
  if (withTrack) {
1060
1066
  trackAddToCartGA({
@@ -1066,7 +1072,21 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1066
1072
  performanceAdapter?.addToCartEnd();
1067
1073
  return resultCart;
1068
1074
  },
1069
- [client, locale, cartCookieAdapter, userAdapter, cart, withTrack, performanceAdapter]
1075
+ [
1076
+ client,
1077
+ locale,
1078
+ cartCookieAdapter,
1079
+ userAdapter,
1080
+ cart,
1081
+ withTrack,
1082
+ performanceAdapter,
1083
+ createCart3,
1084
+ addCartLines2,
1085
+ applyCartCodes,
1086
+ removeInvalidCodes,
1087
+ addCustomAttributes,
1088
+ config
1089
+ ]
1070
1090
  );
1071
1091
  return useSWRMutation("add-to-cart", addToCart, swrOptions);
1072
1092
  }