getProductVariations method

Future<List<WooProductVariation>> getProductVariations({
  1. required int productId,
  2. int? page,
  3. int? perPage,
  4. String? search,
  5. String? after,
  6. String? before,
  7. List<int>? exclude,
  8. List<int>? include,
  9. int? offset,
  10. String? order,
  11. String? orderBy,
  12. List<int>? parent,
  13. List<int>? parentExclude,
  14. String? slug,
  15. String? status,
  16. String? sku,
  17. String? taxClass,
  18. bool? onSale,
  19. String? minPrice,
  20. String? maxPrice,
  21. String? stockStatus,
})

Returns a list of all WooProductVariation, with filter options.

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

Implementation

Future<List<WooProductVariation>> getProductVariations(
    {required int productId,
    int? page,
    int? perPage,
    String? search,
    String? after,
    String? before,
    List<int>? exclude,
    List<int>? include,
    int? offset,
    String? order,
    String? orderBy,
    List<int>? parent,
    List<int>? parentExclude,
    String? slug,
    String? status,
    String? sku,
    String? taxClass,
    bool? onSale,
    String? minPrice,
    String? maxPrice,
    String? stockStatus}) 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,
    'sku': sku,
    '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();
  });
  List<WooProductVariation> productVariations = [];
  _setApiResourceUrl(
      path: 'products/' + productId.toString() + '/variations',
      queryParameters: payload);
  _printToLog('this is the curent path : ' + this.apiPath);
  final response = await get(queryUri.toString());
  for (var v in response) {
    var prodv = WooProductVariation.fromJson(v);
    _printToLog('prod gotten here : ' + prodv.toString());
    productVariations.add(prodv);
  }
  return productVariations;
}