addToCustomerCart method

Future<Cart> addToCustomerCart({
  1. required String sku,
  2. required int quantity,
  3. Map<String, dynamic>? productOptions,
})

Add item to customer cart

Implementation

Future<Cart> addToCustomerCart({
  required String sku,
  required int quantity,
  Map<String, dynamic>? productOptions,
}) async {
  try {
    final data = {
      'cartItem': {'sku': sku, 'qty': quantity},
    };

    if (productOptions != null) {
      data['cartItem']!['product_option'] = productOptions;
    }

    final response = await _client.authenticatedRequest<Map<String, dynamic>>(
      '/rest/V1/carts/mine/items',
      options: Options(method: 'POST'),
      data: data,
    );

    if (response.statusCode == 200) {
      return Cart.fromJson(response.data!);
    } else {
      throw Exception(
        'Failed to add item to customer cart: ${response.statusMessage}',
      );
    }
  } on DioException catch (e) {
    throw Exception('Failed to add item to customer cart: ${e.message}');
  } catch (e) {
    throw Exception('Failed to add item to customer cart: $e');
  }
}