woocommerce_flutter_api 1.5.0 copy "woocommerce_flutter_api: ^1.5.0" to clipboard
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');
}
18
likes
150
points
362
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package for WooCommerce integration. Manage products, orders, and customers with updated dependencies for seamless e-commerce.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

dio, faker, flutter_secure_storage, pretty_dio_logger

More

Packages that depend on woocommerce_flutter_api