231 lines
6.9 KiB
Dart
231 lines
6.9 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import '../models/product.dart';
|
|
import '../models/category.dart';
|
|
import '../models/review.dart';
|
|
|
|
class WooCommerceService {
|
|
// Ersetze diese URL mit deiner WooCommerce Shop-URL
|
|
static const String baseUrl = 'https://hyggecraftery.com';
|
|
static const String consumerKey = 'ck_ae583aa69ca3f962e14ae03faadd302937b8f3e2'; // Ersetze mit deinem Consumer Key
|
|
static const String consumerSecret = 'cs_3835315e5dc3c7e87c89a733e6c3e5ec8f76b70e'; // Ersetze mit deinem Consumer Secret
|
|
|
|
// Öffentliche Zugriffe für andere Services
|
|
static String get shopUrl => baseUrl;
|
|
|
|
// Für öffentliche Produkte (ohne Authentifizierung)
|
|
// Du kannst auch die WooCommerce REST API ohne Keys verwenden, wenn die Produkte öffentlich sind
|
|
static const String apiBaseUrl = '$baseUrl/wp-json/wc/v3';
|
|
|
|
/// Baut eine authentifizierte URL für WooCommerce API-Aufrufe
|
|
Uri _buildAuthenticatedUrl(String endpoint, Map<String, String>? queryParams) {
|
|
final params = <String, String>{
|
|
'consumer_key': consumerKey,
|
|
'consumer_secret': consumerSecret,
|
|
if (queryParams != null) ...queryParams,
|
|
};
|
|
|
|
return Uri.parse('$apiBaseUrl/$endpoint').replace(
|
|
queryParameters: params,
|
|
);
|
|
}
|
|
|
|
Future<List<Product>> getProducts({
|
|
int perPage = 20,
|
|
int page = 1,
|
|
String? category,
|
|
String? search,
|
|
String? orderBy,
|
|
String? order,
|
|
double? minPrice,
|
|
double? maxPrice,
|
|
}) async {
|
|
try {
|
|
final queryParams = {
|
|
'per_page': perPage.toString(),
|
|
'page': page.toString(),
|
|
'status': 'publish',
|
|
};
|
|
|
|
if (category != null) {
|
|
queryParams['category'] = category;
|
|
}
|
|
|
|
if (search != null && search.isNotEmpty) {
|
|
queryParams['search'] = search;
|
|
}
|
|
|
|
if (orderBy != null) {
|
|
queryParams['orderby'] = orderBy;
|
|
}
|
|
|
|
if (order != null) {
|
|
queryParams['order'] = order;
|
|
}
|
|
|
|
if (minPrice != null) {
|
|
queryParams['min_price'] = minPrice.toString();
|
|
}
|
|
|
|
if (maxPrice != null) {
|
|
queryParams['max_price'] = maxPrice.toString();
|
|
}
|
|
|
|
final uri = _buildAuthenticatedUrl('products', queryParams);
|
|
|
|
final response = await http.get(uri);
|
|
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> data = json.decode(response.body);
|
|
return data.map((json) => Product.fromJson(json)).toList();
|
|
} else {
|
|
throw Exception('Fehler beim Laden der Produkte: ${response.statusCode}');
|
|
}
|
|
} catch (e) {
|
|
throw Exception('Fehler beim Abrufen der Produkte: $e');
|
|
}
|
|
}
|
|
|
|
Future<Product> getProduct(int productId) async {
|
|
try {
|
|
final uri = _buildAuthenticatedUrl('products/$productId', null);
|
|
|
|
final response = await http.get(uri);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = json.decode(response.body);
|
|
return Product.fromJson(data);
|
|
} else {
|
|
throw Exception('Fehler beim Laden des Produkts: ${response.statusCode}');
|
|
}
|
|
} catch (e) {
|
|
throw Exception('Fehler beim Abrufen des Produkts: $e');
|
|
}
|
|
}
|
|
|
|
Future<List<Product>> getFeaturedProducts({int limit = 6}) async {
|
|
try {
|
|
final uri = _buildAuthenticatedUrl('products', {
|
|
'featured': 'true',
|
|
'per_page': limit.toString(),
|
|
'status': 'publish',
|
|
});
|
|
|
|
final response = await http.get(uri);
|
|
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> data = json.decode(response.body);
|
|
return data.map((json) => Product.fromJson(json)).toList();
|
|
} else {
|
|
throw Exception('Fehler beim Laden der Featured Produkte: ${response.statusCode}');
|
|
}
|
|
} catch (e) {
|
|
throw Exception('Fehler beim Abrufen der Featured Produkte: $e');
|
|
}
|
|
}
|
|
|
|
/// Holt alle Kategorien
|
|
Future<List<Category>> getCategories({int? parent}) async {
|
|
try {
|
|
final queryParams = <String, String>{
|
|
'per_page': '100',
|
|
'hide_empty': 'false',
|
|
};
|
|
|
|
if (parent != null) {
|
|
queryParams['parent'] = parent.toString();
|
|
}
|
|
|
|
final uri = _buildAuthenticatedUrl('products/categories', queryParams);
|
|
|
|
final response = await http.get(uri);
|
|
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> data = json.decode(response.body);
|
|
return data.map((json) => Category.fromJson(json)).toList();
|
|
} else {
|
|
throw Exception('Fehler beim Laden der Kategorien: ${response.statusCode}');
|
|
}
|
|
} catch (e) {
|
|
throw Exception('Fehler beim Abrufen der Kategorien: $e');
|
|
}
|
|
}
|
|
|
|
/// Holt Bewertungen für ein Produkt
|
|
Future<List<Review>> getProductReviews(int productId, {int perPage = 10, int page = 1}) async {
|
|
try {
|
|
final uri = _buildAuthenticatedUrl('products/reviews', {
|
|
'product': productId.toString(),
|
|
'per_page': perPage.toString(),
|
|
'page': page.toString(),
|
|
'status': 'approved',
|
|
});
|
|
|
|
final response = await http.get(uri);
|
|
|
|
if (response.statusCode == 200) {
|
|
final List<dynamic> data = json.decode(response.body);
|
|
return data.map((json) => Review.fromJson(json)).toList();
|
|
} else {
|
|
throw Exception('Fehler beim Laden der Bewertungen: ${response.statusCode}');
|
|
}
|
|
} catch (e) {
|
|
throw Exception('Fehler beim Abrufen der Bewertungen: $e');
|
|
}
|
|
}
|
|
|
|
/// Holt Produktbewertungs-Statistiken
|
|
Future<ProductRating> getProductRating(int productId) async {
|
|
try {
|
|
final product = await getProduct(productId);
|
|
|
|
// WooCommerce speichert Rating-Daten im Produkt
|
|
final averageRating = double.tryParse(product.rating?.toString() ?? '0') ?? 0.0;
|
|
final ratingCount = product.ratingCount ?? 0;
|
|
|
|
return ProductRating(
|
|
averageRating: averageRating,
|
|
ratingCount: ratingCount,
|
|
ratingBreakdown: {}, // Kann aus Reviews berechnet werden
|
|
);
|
|
} catch (e) {
|
|
throw Exception('Fehler beim Abrufen der Bewertungsstatistik: $e');
|
|
}
|
|
}
|
|
|
|
/// Erstellt eine neue Bewertung
|
|
Future<Review?> createReview({
|
|
required int productId,
|
|
required String reviewer,
|
|
required String reviewerEmail,
|
|
required String review,
|
|
required int rating,
|
|
}) async {
|
|
try {
|
|
final uri = _buildAuthenticatedUrl('products/reviews', null);
|
|
|
|
final response = await http.post(
|
|
uri,
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: json.encode({
|
|
'product_id': productId,
|
|
'reviewer': reviewer,
|
|
'reviewer_email': reviewerEmail,
|
|
'review': review,
|
|
'rating': rating,
|
|
'status': 'pending', // Wird vom Admin genehmigt
|
|
}),
|
|
);
|
|
|
|
if (response.statusCode == 201) {
|
|
final data = json.decode(response.body);
|
|
return Review.fromJson(data);
|
|
} else {
|
|
throw Exception('Fehler beim Erstellen der Bewertung: ${response.statusCode}');
|
|
}
|
|
} catch (e) {
|
|
throw Exception('Fehler beim Erstellen der Bewertung: $e');
|
|
}
|
|
}
|
|
}
|