addLineItemsToCart method

Future<Cart> addLineItemsToCart({
  1. required String cartId,
  2. required List<CartLineUpdateInput> cartLineInputs,
})

add line item to cart

Implementation

Future<Cart> addLineItemsToCart({
  required String cartId,
  required List<CartLineUpdateInput> cartLineInputs,
}) async {
  final lineInputs = cartLineInputs.map((e) {
    final json = e.toJson();
    json.remove('id');
    return json;
  }).toList();
  final MutationOptions addLineItem = MutationOptions(
    document: gql(addLineItemToCartMutation),
    variables: {
      'cartId': cartId,
      'lines': lineInputs,
      'country': ShopifyLocalization.countryCode
    },
  );
  QueryResult result = await _graphQLClient!.mutate(addLineItem);
  checkForError(result, key: 'cartLinesAdd', errorKey: 'userErrors');

  return Cart.fromJson(
      ((result.data!['cartLinesAdd'] ?? const {})['cart'] ?? const {}));
}