add product to cocart woocomerce using rest api,and authenticatrion

Viewed 188

is it possible to add product to cart with cocart using consumer key and secret as authentication in restapi. as of now im using customer password and email as authentication to add item to cart but i cant store email and password in local storage. here is the link to documentation https://docs.cocart.xyz/#add-to-cart-clear-cart-properties any one knows please help

https://docs.cocart.xyz/#add-to-cart-clear-cart-properties

Future<List<CartItem>> AddToCart(
{required String? prodductid,
String? quantity = '1',
required String userid,
required String username,
required String password}) async {
 print("Adding to Cart");
 List<CartItem> cartItems = [];
// String username = 'basiltalks85@gmail.com';
// String password = 'basil51951';
//lets encode creds
 String basicAuth =
    'Basic ' + 
 base64Encode(utf8.encode('$consumerKey:$consumerSecret'));
print(basicAuth);

try {
  var response = await Dio().post(
    'https://beta.nilgiris.ae/wp-json/cocart/v2/cart/add-item? 
  cart_key=$userid',
    options: Options(
      headers: <String, String>{'authorization': basicAuth},
    ),
    data: {
      "id": prodductid,
      "quantity": quantity,
    },
  );
  if (response.statusCode == 200) {
    print('on success add to cart');
    print(response);
    final res = response.data as Map<dynamic, dynamic>;
    final items = res['items'] as List;
    if (items.isEmpty) {
      return cartItems;
    }
    if (items.isNotEmpty) {
      for (var i = 0; i < items.length; i++) {
        cartItems.add(CartItem.fromJson(items[i]));
      }
      return cartItems;
      }
    }
  } on DioError catch (e) {
    toast('Something Wrong');
  
    throw e;
  } catch (e) {
 
  throw e;
}
return cartItems;

}

0 Answers
Related