getUpSellProducts method

Future<List<Product>> getUpSellProducts(
  1. String sku
)

Get up-sell products

Implementation

Future<List<Product>> getUpSellProducts(String sku) async {
  try {
    final response = await _client.guestRequest<Map<String, dynamic>>(
      '/rest/V1/products/$sku/links',
      queryParameters: {'type': 'upsell'},
    );

    if (response.statusCode == 200) {
      final List<dynamic> productsData = response.data!['items'] ?? [];
      return productsData.map((json) => Product.fromJson(json)).toList();
    } else {
      throw Exception(
        'Failed to get up-sell products: ${response.statusMessage}',
      );
    }
  } on DioException catch (e) {
    throw Exception('Failed to get up-sell products: ${e.message}');
  } catch (e) {
    throw Exception('Failed to get up-sell products: $e');
  }
}