getCoupons method

Future<List<WooCoupon>?> getCoupons({
  1. int? page,
  2. int? perPage,
  3. String? search,
  4. String? after,
  5. String? before,
  6. int? offset,
  7. String? order,
  8. String? orderBy,
  9. String? code,
})

Returns a list of all WooCoupon, with filter options.

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

Implementation

Future<List<WooCoupon>?> getCoupons({
  int? page,
  int? perPage,
  String? search,
  String? after,
  String? before,
  //List<int> exclude,
  //List<int> include,
  int? offset,
  String? order,
  String? orderBy,
  String? code,
}) 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, 'code': code,
  }).forEach((k, v) {
    if (v != null) payload[k] = v.toString();
  });
  List<WooCoupon>? coupons;
  _printToLog('Getting Coupons With Payload : ' + payload.toString());
  _setApiResourceUrl(path: 'coupons', queryParameters: payload);
  final response = await get(queryUri.toString());
  for (var c in response) {
    var coupon = WooCoupon.fromJson(c);
    _printToLog('prod gotten here : ' + order.toString());
    coupons!.add(coupon);
  }
  return coupons;
}