woocommerce_flutter_api 1.5.0
woocommerce_flutter_api: ^1.5.0 copied to clipboard
A Flutter package for WooCommerce integration. Manage products, orders, and customers with updated dependencies for seamless e-commerce.
example/woocommerce_flutter_api_example.dart
/// Comprehensive WooCommerce Flutter API Example
///
/// This file demonstrates all major features of the package.
/// Run different examples by uncommenting the desired function in main().
///
/// ## Setup Instructions
///
/// 1. Replace the placeholder credentials below with your actual WooCommerce store details
/// 2. Uncomment the example function you want to run in main()
/// 3. Run the example with: `dart run example/woocommerce_flutter_api_example.dart`
///
/// ## Important Notes
///
/// - For development, use `useFaker: true` to get fake data without a real store
/// - For production, set `useFaker: false` and provide real credentials
/// - Always handle errors appropriately in production code
/// - Consider implementing proper state management for larger applications
import 'package:dio/dio.dart';
import 'package:woocommerce_flutter_api/woocommerce_flutter_api.dart';
// ============================================================================
// CONFIGURATION
// ============================================================================
/// Your WooCommerce store configuration
/// TODO: Replace with your actual store details
const String storeUrl = 'https://yourstore.com';
const String consumerKey = 'ck_your_consumer_key_here';
const String consumerSecret = 'cs_your_consumer_secret_here';
// ============================================================================
// SETUP EXAMPLES
// ============================================================================
/// Basic setup example for production use
Future<void> setupExample() async {
print('π Setting up WooCommerce client for production...');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
isDebug: true, // Enable debug logging
useFaker: false, // Use real data
);
print('β
WooCommerce client initialized successfully');
print('Base URL: ${woocommerce.baseUrl}');
print('Debug mode: ${woocommerce.isDebug}');
}
/// Setup example for development with fake data
Future<void> setupWithFakeData() async {
print('π§ͺ Setting up WooCommerce client for development...');
final woocommerce = WooCommerce(
baseUrl: 'https://example.com', // Not used when useFaker is true
username: 'dev_key', // Not used when useFaker is true
password: 'dev_secret', // Not used when useFaker is true
isDebug: true,
useFaker: true, // This will return fake data for all API calls
);
print('β
Development client initialized with fake data');
// Test with fake data
try {
final products = await woocommerce.getProducts(perPage: 3);
print('π¦ Fetched ${products.length} fake products:');
for (final product in products) {
print(' - ${product.name} (ID: ${product.id})');
}
} catch (e) {
print('β Error: $e');
}
}
// ============================================================================
// AUTHENTICATION EXAMPLES
// ============================================================================
/// User login example
Future<void> authenticationExample() async {
print('π Authentication Example');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true, // Use fake data for this example
);
try {
// Login user
await woocommerce.login('user@example.com', 'password123');
print('β
User logged in successfully');
// Get user ID from storage
final userId = await LocalStorageHelper.getSecurityUserId();
print('π€ User ID: $userId');
// Logout user
await woocommerce.logout();
print('π User logged out successfully');
} catch (e) {
print('β Authentication error: $e');
}
}
/// User registration example
Future<void> registrationExample() async {
print('π Registration Example');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true,
);
try {
// Create new customer
final newCustomer = WooCustomer(
email: 'newuser@example.com',
firstName: 'John',
lastName: 'Doe',
);
await woocommerce.register(newCustomer);
print('β
Customer registered successfully');
} catch (e) {
print('β Registration error: $e');
}
}
// ============================================================================
// PRODUCT EXAMPLES
// ============================================================================
/// Fetch products with various filters
Future<void> fetchProductsExample() async {
print('π¦ Fetch Products Example');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true,
);
try {
// Get first 5 published products
final products = await woocommerce.getProducts(
perPage: 5,
status: WooFilterStatus.publish,
orderBy: WooSortOrderBy.title,
order: WooSortOrder.asc,
);
print('β
Fetched ${products.length} products:');
for (final product in products) {
print(' π¦ ${product.name}');
print(' ID: ${product.id}');
print(' Price: \$${product.price}');
print(' Status: ${product.status}');
print(' Featured: ${product.featured}');
print(' Stock: ${product.stockStatus}');
print(' ---');
}
} catch (e) {
print('β Error fetching products: $e');
}
}
/// Search products example
Future<void> searchProductsExample() async {
print('π Search Products Example');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true,
);
try {
// Search for products
final searchResults = await woocommerce.getProducts(
search: 'laptop',
perPage: 10,
status: WooFilterStatus.publish,
);
print('π Search results for "laptop":');
for (final product in searchResults) {
print(' π¦ ${product.name} - \$${product.price}');
}
// Get featured products
final featuredProducts = await woocommerce.getProducts(
featured: true,
perPage: 5,
);
print('\nβ Featured products:');
for (final product in featuredProducts) {
print(' β ${product.name}');
}
} catch (e) {
print('β Search error: $e');
}
}
/// Create a new product
Future<void> createProductExample() async {
print('β Create Product Example');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true,
);
try {
// Create a new product
final newProduct = WooProduct(
name: 'Amazing Widget',
type: WooProductType.simple,
status: WooProductStatus.publish,
description: 'This is an amazing widget that does amazing things!',
shortDescription: 'Amazing widget for amazing people',
price: 29.99,
regularPrice: 39.99,
salePrice: 29.99,
sku: 'WIDGET-001',
manageStock: true,
stockQuantity: 100,
stockStatus: WooProductStockStatus.instock,
featured: true,
virtual: false,
downloadable: false,
);
final createdProduct = await woocommerce.createProduct(newProduct);
print('β
Product created successfully!');
print('π¦ Name: ${createdProduct.name}');
print('π ID: ${createdProduct.id}');
print('π° Price: \$${createdProduct.price}');
} catch (e) {
print('β Error creating product: $e');
}
}
/// Update an existing product
Future<void> updateProductExample() async {
print('βοΈ Update Product Example');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true,
);
try {
// First, get a product to update
final products = await woocommerce.getProducts(perPage: 1);
if (products.isEmpty) {
print('β No products found to update');
return;
}
final product = products.first;
print('π¦ Original product: ${product.name} - \$${product.price}');
// Update the product
final updatedProduct = product.copyWith(
name: '${product.name} (Updated)',
price: (product.price ?? 0) + 10.0,
onSale: true,
);
final result = await woocommerce.updateProduct(product.id!, updatedProduct);
print('β
Product updated successfully!');
print('π¦ New name: ${result.name}');
print('π° New price: \$${result.price}');
print('π·οΈ On sale: ${result.onSale}');
} catch (e) {
print('β Error updating product: $e');
}
}
// ============================================================================
// ORDER EXAMPLES
// ============================================================================
/// Create a new order
Future<void> createOrderExample() async {
print('π Create Order Example');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true,
);
try {
// Create billing address
final billing = WooBilling(
firstName: 'John',
lastName: 'Doe',
email: 'john.doe@example.com',
phone: '+1234567890',
address1: '123 Main St',
city: 'New York',
state: 'NY',
postcode: '10001',
country: 'US',
);
// Create shipping address
final shipping = WooShipping(
firstName: 'John',
lastName: 'Doe',
address1: '123 Main St',
city: 'New York',
state: 'NY',
postcode: '10001',
country: 'US',
);
// Create line items
final lineItems = [
WooLineItem(
productId: 1,
quantity: 2,
name: 'Sample Product',
price: 25.99,
),
];
// Create the order
final order = WooOrder(
id: 0, // Will be assigned by WooCommerce
status: WooOrderStatus.pending,
currency: WooOrderCurrency.USD,
customerId: 1,
billing: billing,
shipping: shipping,
lineItems: lineItems,
total: 51.98,
paymentMethod: 'credit_card',
paymentMethodTitle: 'Credit Card',
);
final createdOrder = await woocommerce.createOrder(order);
print('β
Order created successfully!');
print('π Order ID: ${createdOrder.id}');
print('π§ Customer: ${createdOrder.billing?.email}');
print('π° Total: \$${createdOrder.total}');
print('π¦ Items: ${createdOrder.lineItems?.length}');
} catch (e) {
print('β Error creating order: $e');
}
}
/// Fetch orders with filters
Future<void> fetchOrdersExample() async {
print('π Fetch Orders Example');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true,
);
try {
// Get recent orders
final orders = await woocommerce.getOrders(
perPage: 5,
status: [WooOrderStatus.processing],
orderBy: WooOrderOrderBy.date,
order: WooSortOrder.desc,
);
print('β
Fetched ${orders.length} orders:');
for (final order in orders) {
print('π Order #${order.number}');
print(' Status: ${order.status}');
print(' Total: \$${order.total}');
print(' Customer: ${order.billing?.email}');
print(' Date: ${order.dateCreated}');
print(' ---');
}
} catch (e) {
print('β Error fetching orders: $e');
}
}
// ============================================================================
// CART EXAMPLES
// ============================================================================
/// Get user's cart
Future<void> cartExample() async {
print('π Cart Example');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true,
);
try {
// Get user's cart
final cart = await woocommerce.getCart();
print('β
Cart retrieved successfully!');
print('π Total items: ${cart.items?.length ?? 0}');
print('π° Total price: \$${cart.totalPrice}');
if (cart.items != null && cart.items!.isNotEmpty) {
for (final item in cart.items!) {
print(' π¦ ${item.name} x${item.quantity} - \$${item.price}');
}
} else {
print('π Cart is empty');
}
} catch (e) {
print('β Error fetching cart: $e');
}
}
// ============================================================================
// ADVANCED EXAMPLES
// ============================================================================
/// Pagination example
Future<void> paginationExample() async {
print('π Pagination Example');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true,
);
try {
const int itemsPerPage = 3;
int currentPage = 1;
bool hasMorePages = true;
while (hasMorePages) {
print('π Fetching page $currentPage...');
final products = await woocommerce.getProducts(
page: currentPage,
perPage: itemsPerPage,
);
if (products.isEmpty) {
hasMorePages = false;
print('π No more products found');
break;
}
print('π¦ Page $currentPage - ${products.length} products:');
for (final product in products) {
print(' π¦ ${product.name} (ID: ${product.id})');
}
// Check if we have more pages
hasMorePages = products.length == itemsPerPage;
currentPage++;
// Safety check to prevent infinite loops
if (currentPage > 5) {
print('β οΈ Stopping pagination after 5 pages for demo purposes');
break;
}
}
} catch (e) {
print('β Pagination error: $e');
}
}
/// Error handling example
Future<void> errorHandlingExample() async {
print('β οΈ Error Handling Example');
final woocommerce = WooCommerce(
baseUrl: 'https://invalid-store.com', // Invalid URL
username: 'invalid_key',
password: 'invalid_secret',
useFaker: false, // Try real API call to demonstrate error handling
);
try {
print('π Attempting to fetch products from invalid store...');
await woocommerce.getProducts();
} on DioException catch (e) {
print('π Network error: ${e.message}');
print('π Status code: ${e.response?.statusCode}');
print('π Response data: ${e.response?.data}');
} catch (e) {
print('β Unexpected error: $e');
}
// Demonstrate proper error handling pattern
print('\nβ
Proper error handling pattern:');
try {
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true,
);
final products = await woocommerce.getProducts(perPage: 1);
print(
'β
Successfully handled API call - found ${products.length} products');
} catch (e) {
print('β Error occurred: $e');
// In a real app, you would:
// 1. Log the error
// 2. Show user-friendly message
// 3. Implement retry logic if appropriate
// 4. Report to crash analytics
}
}
// ============================================================================
// COMPREHENSIVE E-COMMERCE FLOW EXAMPLE
// ============================================================================
/// Complete e-commerce flow example
Future<void> completeEcommerceFlow() async {
print('ποΈ Complete E-commerce Flow Example');
final woocommerce = WooCommerce(
baseUrl: storeUrl,
username: consumerKey,
password: consumerSecret,
useFaker: true,
);
try {
// Step 1: User authentication
print('π Step 1: User Authentication');
await woocommerce.login('customer@example.com', 'password123');
print('β
User logged in');
// Step 2: Browse products
print('\nπ¦ Step 2: Browse Products');
final products = await woocommerce.getProducts(
perPage: 3,
status: WooFilterStatus.publish,
);
print('β
Found ${products.length} products');
// Display first few products
for (int i = 0; i < products.length && i < 2; i++) {
print(' π¦ ${products[i].name} - \$${products[i].price}');
}
// Step 3: Search for specific products
print('\nπ Step 3: Search Products');
final searchResults = await woocommerce.getProducts(
search: 'widget',
perPage: 2,
);
print('β
Found ${searchResults.length} matching products');
// Step 4: Get product details
if (products.isNotEmpty) {
print('\nπ Step 4: Product Details');
final product = products.first;
final productDetails = await woocommerce.getProduct(product.id!);
print('β
Product details: ${productDetails.name}');
print('π° Price: \$${productDetails.price}');
print('π¦ Stock: ${productDetails.stockStatus}');
}
// Step 5: Get user's cart
print('\nπ Step 5: Shopping Cart');
final cart = await woocommerce.getCart();
print('β
Cart retrieved');
print('π Items in cart: ${cart.items?.length ?? 0}');
// Step 6: Create an order
print('\nπ Step 6: Create Order');
final order = WooOrder(
id: 0,
status: WooOrderStatus.pending,
currency: WooOrderCurrency.USD,
customerId: 1,
billing: WooBilling(
firstName: 'John',
lastName: 'Doe',
email: 'john.doe@example.com',
address1: '123 Main St',
city: 'New York',
state: 'NY',
postcode: '10001',
country: 'US',
),
lineItems: [
WooLineItem(
productId: 1,
quantity: 1,
name: 'Sample Product',
price: 25.99,
),
],
total: 25.99,
);
final createdOrder = await woocommerce.createOrder(order);
print('β
Order created: #${createdOrder.number}');
print('π° Total: \$${createdOrder.total}');
// Step 7: Logout
print('\nπ Step 7: Logout');
await woocommerce.logout();
print('β
User logged out');
print('\nπ Complete e-commerce flow finished successfully!');
} catch (e) {
print('β Error in e-commerce flow: $e');
}
}
// ============================================================================
// MAIN FUNCTION
// ============================================================================
void main() async {
print('π WooCommerce Flutter API Examples');
print('=====================================\n');
// Uncomment the example you want to run:
// Setup examples
// await setupExample();
// await setupWithFakeData();
// Authentication examples
// await authenticationExample();
// await registrationExample();
// Product examples
// await fetchProductsExample();
// await searchProductsExample();
// await createProductExample();
// await updateProductExample();
// Order examples
// await createOrderExample();
// await fetchOrdersExample();
// Cart examples
// await cartExample();
// Advanced examples
// await paginationExample();
// await errorHandlingExample();
// Complete flow
await completeEcommerceFlow();
print('\n⨠All examples completed!');
print('π‘ Uncomment different examples in main() to try them out');
}