@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.
package/dist/index.mjs CHANGED
@@ -1104,6 +1104,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1104
1104
  const { trigger: applyCartCodes } = useApplyCartCodes();
1105
1105
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
1106
1106
  const { trigger: addCartLines2 } = useAddCartLines();
1107
+ const { trigger: createCart3 } = useCreateCart();
1107
1108
  const addToCart = useCallback(
1108
1109
  async (_key, { arg }) => {
1109
1110
  const {
@@ -1135,40 +1136,45 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1135
1136
  if (lines.length === 0) {
1136
1137
  return;
1137
1138
  }
1138
- const cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
1139
- let resultCart = await addCartLines2({
1140
- cartId,
1141
- lines,
1142
- buyerIdentity
1143
- });
1144
- if (!resultCart) {
1145
- performanceAdapter?.addToCartEnd();
1146
- return void 0;
1147
- }
1148
- console.log("npm addCartLines resultCart", resultCart);
1149
- if (resultCart.discountCodes && resultCart.discountCodes.length > 0) {
1150
- const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
1151
- if (unapplicableCodes.length > 0) {
1152
- if (onCodesInvalid) {
1153
- const handledCart = await onCodesInvalid(resultCart, unapplicableCodes);
1154
- if (handledCart) {
1155
- resultCart = handledCart;
1139
+ let cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
1140
+ let resultCart = null;
1141
+ if (!cartId) {
1142
+ resultCart = await createCart3({
1143
+ lines,
1144
+ buyerIdentity,
1145
+ discountCodes,
1146
+ customAttributes
1147
+ });
1148
+ } else {
1149
+ resultCart = await addCartLines2({
1150
+ cartId,
1151
+ lines
1152
+ });
1153
+ console.log("npm addCartLines resultCart", resultCart);
1154
+ if (resultCart && resultCart.discountCodes && resultCart.discountCodes.length > 0) {
1155
+ const unapplicableCodes = resultCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
1156
+ if (unapplicableCodes.length > 0) {
1157
+ if (onCodesInvalid) {
1158
+ const handledCart = await onCodesInvalid(resultCart, unapplicableCodes);
1159
+ if (handledCart) {
1160
+ resultCart = handledCart;
1161
+ }
1162
+ } else {
1163
+ await removeInvalidCodes({
1164
+ discountCodes: unapplicableCodes
1165
+ });
1156
1166
  }
1157
- } else {
1158
- await removeInvalidCodes({
1159
- discountCodes: unapplicableCodes
1160
- });
1161
1167
  }
1162
1168
  }
1163
- }
1164
- if (discountCodes && discountCodes.length > 0) {
1165
- applyCartCodes({
1166
- replaceExistingCodes,
1167
- discountCodes
1168
- });
1169
- }
1170
- if (customAttributes && customAttributes.length > 0) {
1171
- addCustomAttributes(customAttributes);
1169
+ if (resultCart && discountCodes && discountCodes.length > 0) {
1170
+ applyCartCodes({
1171
+ replaceExistingCodes,
1172
+ discountCodes
1173
+ });
1174
+ }
1175
+ if (customAttributes && customAttributes.length > 0) {
1176
+ addCustomAttributes(customAttributes);
1177
+ }
1172
1178
  }
1173
1179
  if (withTrack) {
1174
1180
  trackAddToCartGA({
@@ -1180,7 +1186,21 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1180
1186
  performanceAdapter?.addToCartEnd();
1181
1187
  return resultCart;
1182
1188
  },
1183
- [client, locale, cartCookieAdapter, userAdapter, cart, withTrack, performanceAdapter]
1189
+ [
1190
+ client,
1191
+ locale,
1192
+ cartCookieAdapter,
1193
+ userAdapter,
1194
+ cart,
1195
+ withTrack,
1196
+ performanceAdapter,
1197
+ createCart3,
1198
+ addCartLines2,
1199
+ applyCartCodes,
1200
+ removeInvalidCodes,
1201
+ addCustomAttributes,
1202
+ config
1203
+ ]
1184
1204
  );
1185
1205
  return useSWRMutation("add-to-cart", addToCart, swrOptions);
1186
1206
  }