orderHistory method

dynamic orderHistory({
  1. int? orderID,
  2. String? context,
  3. int? page,
  4. int? perPage,
  5. String? search,
  6. String? after,
  7. String? before,
  8. String? modifiedAfter,
  9. String? modifiedBefore,
  10. bool? datesAreGmt,
  11. List<int>? exclude,
  12. List<int>? include,
  13. int? offset,
  14. Order? order,
  15. String? orderBy,
  16. List<int>? parent,
  17. List<int>? parentExclude,
  18. List<String>? status,
  19. int? woo_customer_id,
  20. int? product,
  21. int? dp,
})

Implementation

orderHistory({
  int? orderID,
  String? context,
  int? page,
  int? perPage,
  String? search,
  String? after,
  String? before,
  String? modifiedAfter,
  String? modifiedBefore,
  bool? datesAreGmt,
  List<int>? exclude,
  List<int>? include,
  int? offset,
  Order? order,
  String? orderBy,
  List<int>? parent,
  List<int>? parentExclude,
  List<String>? status,
  int? woo_customer_id,
  int? product,
  int? dp,
}) async {
  var orderHistory_request_api = 'orders';

  // If orderID is provided, fetch a single order
  if (orderID != null) {
    orderHistory_request_api = 'orders/$orderID';
  } else {
    // If orderID is not provided, fetch all orders with pagination and other parameters
    if (perPage != null) {
      orderHistory_request_api += '?per_page=$perPage';
    } else {
      orderHistory_request_api += '?per_page=10'; // Default perPage
    }

    if (context != null) {
      orderHistory_request_api += '&context=$context';
    }
    if (page != null) {
      orderHistory_request_api += '&page=$page';
    }
    if (search != null) {
      orderHistory_request_api += '&search=$search';
    }
    if (after != null) {
      orderHistory_request_api += '&after=$after';
    }
    if (before != null) {
      orderHistory_request_api += '&before=$before';
    }
    if (modifiedAfter != null) {
      orderHistory_request_api += '&modified_after=$modifiedAfter';
    }
    if (modifiedBefore != null) {
      orderHistory_request_api += '&modified_before=$modifiedBefore';
    }
    if (datesAreGmt != null) {
      orderHistory_request_api += '&dates_are_gmt=$datesAreGmt';
    }
    if (exclude != null && exclude.isNotEmpty) {
      orderHistory_request_api += '&exclude=${exclude.join(',')}';
    }
    if (include != null && include.isNotEmpty) {
      orderHistory_request_api += '&include=${include.join(',')}';
    }
    if (offset != null) orderHistory_request_api += '&offset=$offset';
    if (order != null) {
      String orderType = 'asc';
      if (order == Order.ascending) orderType = 'asc';
      if (order == Order.descending) orderType = 'desc';
      orderHistory_request_api += '&order=$orderType';
    }
    if (orderBy != null) orderHistory_request_api += '&orderby=$orderBy';
    if (parent != null && parent.isNotEmpty) {
      orderHistory_request_api += '&parent=${parent.join(',')}';
    }
    if (parentExclude != null && parentExclude.isNotEmpty) {
      orderHistory_request_api +=
          '&parent_exclude=${parentExclude.join(',')}';
    }
    if (status != null && status.isNotEmpty) {
      orderHistory_request_api += '&status=${status.join(',')}';
    }
    if (woo_customer_id != null) {
      orderHistory_request_api += '&customer=$woo_customer_id';
    }
    if (product != null) {
      orderHistory_request_api += '&product=$product';
    }
    if (dp != null) {
      orderHistory_request_api += '&dp=$dp';
    }
  }

  Response res = await ApiServices().getRequest(
      orderHistory_request_api, baseUrl, consumerKey, consumerSecret);
  var decodedData = json.decode(res.body);

  // If orderID is provided, return a single OrderModel object
  if (orderID != null) {
    return OrderModel.fromJson(decodedData);
  } else {
    // If orderID is not provided, return a list of OrderModel objects
    List<OrderModel> order_parsed_list = [];
    for (var i = 0; i < decodedData.length; i++) {
      OrderModel parseData = OrderModel.fromJson(decodedData[i]);
      order_parsed_list.add(parseData);
    }
    return order_parsed_list;
  }
}