addToMyCart method

Future<WooCartItem> addToMyCart({
  1. required String itemId,
  2. required String quantity,
  3. List<WooProductVariation>? variations,
})

Accepts an int id of a product or product variation, int quantity, and an array of chosen variation attribute objects Related endpoint : wc/store/cart

Implementation

Future<WooCartItem> addToMyCart(
    {required String itemId,
    required String quantity,
    List<WooProductVariation>? variations}) async {
  Map<String, dynamic> data = {
    'id': itemId,
    'quantity': quantity,
  };
  if (variations != null) data['variations'] = variations;
  await getAuthTokenFromDb();
  _urlHeader['Authorization'] = 'Bearer ' + _authToken!;
  final response = await http.post(
      Uri.parse(this.baseUrl + URL_STORE_API_PATH + 'cart/items'),
      headers: _urlHeader,
      body: data);

  if (response.statusCode >= 200 && response.statusCode < 300) {
    final jsonStr = json.decode(response.body);

    _printToLog('added to my cart : ' + jsonStr.toString());
    return WooCartItem.fromJson(jsonStr);
  } else {
    WooCommerceError err =
        WooCommerceError.fromJson(json.decode(response.body));
    throw err;
  }
}