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

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.
@@ -60,15 +60,16 @@ interface CreateCartInput {
60
60
  declare function useCreateCart(options?: SWRMutationConfiguration<NormalizedCart | undefined, Error, 'create-cart', CreateCartInput>): swr_mutation.SWRMutationResponse<NormalizedCart | undefined, Error, "create-cart", CreateCartInput>;
61
61
 
62
62
  interface AddCartLinesInput {
63
- /** Cart ID (required) */
64
- cartId: string;
63
+ /** Cart ID (optional, will create new cart if not provided) */
64
+ cartId?: string;
65
65
  /** Lines to add */
66
66
  lines: CartLineInput[];
67
67
  }
68
68
  /**
69
69
  * Hook for adding lines to cart
70
70
  *
71
- * Automatically removes invalid discount codes after adding items
71
+ * - Automatically creates a new cart if no cart ID exists
72
+ * - Automatically removes invalid discount codes after adding items
72
73
  *
73
74
  * @param options - SWR mutation configuration
74
75
  * @returns SWR mutation with trigger function
@@ -77,12 +78,27 @@ interface AddCartLinesInput {
77
78
  * ```tsx
78
79
  * const { trigger, isMutating } = useAddCartLines()
79
80
  *
81
+ * // Add to existing cart
80
82
  * await trigger({
83
+ * cartId: 'gid://shopify/Cart/xxx',
81
84
  * lines: [{
82
85
  * merchandiseId: 'gid://shopify/ProductVariant/123',
83
86
  * quantity: 1
84
87
  * }]
85
88
  * })
89
+ *
90
+ * // Create new cart (no cartId provided)
91
+ * await trigger({
92
+ * lines: [{
93
+ * merchandiseId: 'gid://shopify/ProductVariant/123',
94
+ * quantity: 1
95
+ * }],
96
+ * buyerIdentity: {
97
+ * email: 'customer@example.com',
98
+ * countryCode: 'US'
99
+ * },
100
+ * discountCodes: ['SUMMER2024']
101
+ * })
86
102
  * ```
87
103
  */
88
104
  declare function useAddCartLines(options?: SWRMutationConfiguration<NormalizedCart | undefined, Error, 'add-cart-lines', AddCartLinesInput>): swr_mutation.SWRMutationResponse<NormalizedCart | undefined, Error, "add-cart-lines", AddCartLinesInput>;
@@ -60,15 +60,16 @@ interface CreateCartInput {
60
60
  declare function useCreateCart(options?: SWRMutationConfiguration<NormalizedCart | undefined, Error, 'create-cart', CreateCartInput>): swr_mutation.SWRMutationResponse<NormalizedCart | undefined, Error, "create-cart", CreateCartInput>;
61
61
 
62
62
  interface AddCartLinesInput {
63
- /** Cart ID (required) */
64
- cartId: string;
63
+ /** Cart ID (optional, will create new cart if not provided) */
64
+ cartId?: string;
65
65
  /** Lines to add */
66
66
  lines: CartLineInput[];
67
67
  }
68
68
  /**
69
69
  * Hook for adding lines to cart
70
70
  *
71
- * Automatically removes invalid discount codes after adding items
71
+ * - Automatically creates a new cart if no cart ID exists
72
+ * - Automatically removes invalid discount codes after adding items
72
73
  *
73
74
  * @param options - SWR mutation configuration
74
75
  * @returns SWR mutation with trigger function
@@ -77,12 +78,27 @@ interface AddCartLinesInput {
77
78
  * ```tsx
78
79
  * const { trigger, isMutating } = useAddCartLines()
79
80
  *
81
+ * // Add to existing cart
80
82
  * await trigger({
83
+ * cartId: 'gid://shopify/Cart/xxx',
81
84
  * lines: [{
82
85
  * merchandiseId: 'gid://shopify/ProductVariant/123',
83
86
  * quantity: 1
84
87
  * }]
85
88
  * })
89
+ *
90
+ * // Create new cart (no cartId provided)
91
+ * await trigger({
92
+ * lines: [{
93
+ * merchandiseId: 'gid://shopify/ProductVariant/123',
94
+ * quantity: 1
95
+ * }],
96
+ * buyerIdentity: {
97
+ * email: 'customer@example.com',
98
+ * countryCode: 'US'
99
+ * },
100
+ * discountCodes: ['SUMMER2024']
101
+ * })
86
102
  * ```
87
103
  */
88
104
  declare function useAddCartLines(options?: SWRMutationConfiguration<NormalizedCart | undefined, Error, 'add-cart-lines', AddCartLinesInput>): swr_mutation.SWRMutationResponse<NormalizedCart | undefined, Error, "add-cart-lines", AddCartLinesInput>;
@@ -723,11 +723,23 @@ function useAddCartLines(options) {
723
723
  const { mutateCart, metafieldIdentifiers } = useCartContext();
724
724
  const addLines = react.useCallback(
725
725
  async (_key, { arg }) => {
726
- let updatedCart = await shopifySdk.addCartLines(client, {
727
- ...arg,
728
- metafieldIdentifiers,
729
- cookieAdapter: cartCookieAdapter
730
- });
726
+ const { cartId, lines } = arg;
727
+ const id = cartId || cartCookieAdapter?.getCartId(locale);
728
+ let updatedCart;
729
+ if (!id) {
730
+ updatedCart = await shopifySdk.createCart(client, {
731
+ lines,
732
+ metafieldIdentifiers,
733
+ cookieAdapter: cartCookieAdapter
734
+ });
735
+ } else {
736
+ updatedCart = await shopifySdk.addCartLines(client, {
737
+ cartId: id,
738
+ lines,
739
+ metafieldIdentifiers,
740
+ cookieAdapter: cartCookieAdapter
741
+ });
742
+ }
731
743
  if (updatedCart) {
732
744
  const unApplicableCodes = updatedCart.discountCodes.filter((item) => !item.applicable).map((item) => item.code);
733
745
  if (unApplicableCodes.length > 0) {
@@ -999,7 +1011,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
999
1011
  const { trigger: applyCartCodes } = useApplyCartCodes();
1000
1012
  const { trigger: removeInvalidCodes } = useRemoveCartCodes();
1001
1013
  const { trigger: addCartLines2 } = useAddCartLines();
1002
- const { trigger: createCart3 } = useCreateCart();
1014
+ const { trigger: createCart4 } = useCreateCart();
1003
1015
  const addToCart = react.useCallback(
1004
1016
  async (_key, { arg }) => {
1005
1017
  const {
@@ -1034,7 +1046,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1034
1046
  let cartId = needCreateCart ? void 0 : providedCartId || cart?.id;
1035
1047
  let resultCart = null;
1036
1048
  if (!cartId) {
1037
- resultCart = await createCart3({
1049
+ resultCart = await createCart4({
1038
1050
  lines,
1039
1051
  buyerIdentity,
1040
1052
  discountCodes,
@@ -1089,7 +1101,7 @@ function useAddToCart({ withTrack = true } = {}, swrOptions) {
1089
1101
  cart,
1090
1102
  withTrack,
1091
1103
  performanceAdapter,
1092
- createCart3,
1104
+ createCart4,
1093
1105
  addCartLines2,
1094
1106
  applyCartCodes,
1095
1107
  removeInvalidCodes,