createCart method

Future<Cart> createCart()

Create a new guest cart.

Creates a new shopping cart for guest users. Guest carts are temporary and can be merged with customer carts after authentication.

Returns a Cart object representing the newly created cart. Throws an exception if cart creation fails.

Implementation

Future<Cart> createCart() async {
  try {
    final response = await _client.guestRequest<Map<String, dynamic>>(
      '/rest/V1/guest-carts',
      options: Options(method: 'POST'),
    );

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