getProductCategories method

Future<List<WooProductCategory>> getProductCategories({
  1. int? page,
  2. int? perPage,
  3. String? search,
  4. String? order,
  5. String? orderBy,
  6. bool? hideEmpty,
  7. int? parent,
  8. int? product,
  9. String? slug,
})

Returns a list of all WooProductCategory, with filter options.

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

Implementation

Future<List<WooProductCategory>> getProductCategories(
    {int? page,
    int? perPage,
    String? search,
    //List<int> exclude,
    //List<int> include,
    String? order,
    String? orderBy,
    bool? hideEmpty,
    int? parent,
    int? product,
    String? slug}) async {
  Map<String, dynamic> payload = {};

  ({
    'page': page, 'per_page': perPage, 'search': search,
    //'exclude': exclude, 'include': include,
    'order': order, 'orderby': orderBy, 'hide_empty': hideEmpty,
    'parent': parent,
    'product': product, 'slug': slug,
  }).forEach((k, v) {
    if (v != null) payload[k] = v.toString();
  });

  List<WooProductCategory> productCategories = [];
  _printToLog('payload : ' + payload.toString());
  _setApiResourceUrl(path: 'products/categories', queryParameters: payload);
  _printToLog('this is the path : ' + this.apiPath);
  final response = await get(queryUri.toString());
  for (var c in response) {
    var cat = WooProductCategory.fromJson(c);
    _printToLog('category gotten here : ' + cat.toString());
    productCategories.add(cat);
  }
  return productCategories;
}