getProducts method

Future<List<WooProduct>> getProducts({
  1. int? page,
  2. int? perPage,
  3. String? search,
  4. String? after,
  5. String? before,
  6. String? order,
  7. String? orderBy,
  8. String? slug,
  9. String? status,
  10. String? type,
  11. String? sku,
  12. String? category,
  13. String? tag,
  14. String? shippingClass,
  15. String? attribute,
  16. String? attributeTerm,
  17. String? taxClass,
  18. String? minPrice,
  19. String? maxPrice,
  20. String? stockStatus,
  21. List<int>? exclude,
  22. List<int>? parentExclude,
  23. List<int>? include,
  24. List<int>? parent,
  25. int? offset,
  26. bool? featured,
  27. bool? onSale,
})

Returns a list of all WooProduct, with filter options.

Related endpoint: https://woocommerce.github.io/woocommerce-rest-api-docs/#products.

Implementation

Future<List<WooProduct>> getProducts(
    {int? page,
    int? perPage,
    String? search,
    String? after,
    String? before,
    String? order,
    String? orderBy,
    String? slug,
    String? status,
    String? type,
    String? sku,
    String? category,
    String? tag,
    String? shippingClass,
    String? attribute,
    String? attributeTerm,
    String? taxClass,
    String? minPrice,
    String? maxPrice,
    String? stockStatus,
    List<int>? exclude,
    List<int>? parentExclude,
    List<int>? include,
    List<int>? parent,
    int? offset,
    bool? featured,
    bool? onSale}) async {
  Map<String, dynamic> payload = {};

  ({
    'page': page,
    'per_page': perPage,
    'search': search,
    'after': after,
    'before': before,
    'exclude': exclude,
    'include': include,
    'offset': offset,
    'order': order,
    'orderby': orderBy,
    'parent': parent,
    'parent_exclude': parentExclude,
    'slug': slug,
    'status': status,
    'type': type,
    'sku': sku,
    'featured': featured,
    'category': category,
    'tag': tag,
    'shipping_class': shippingClass,
    'attribute': attribute,
    'attribute_term': attributeTerm,
    'tax_class': taxClass,
    'on_sale': onSale,
    'min_price': minPrice,
    'max_price': maxPrice,
    'stock_status': stockStatus,
  }).forEach((k, v) {
    if (v != null) payload[k] = v.toString();
  });

  _printToLog("Parameters: " + payload.toString());
  List<WooProduct> products = [];
  _setApiResourceUrl(path: 'products', queryParameters: payload);
  final response = await get(queryUri.toString());
  _printToLog('response gotten : ' + response.toString());
  _printToLog('this is the queri uri : ' + queryUri.toString());
  for (var p in response) {
    var prod = WooProduct.fromJson(p);
    _printToLog('product gotten here : ' + prod.name.toString());
    products.add(prod);
  }
  return products;
}