createCoupon method

Future<WooCoupon> createCoupon({
  1. String? code,
  2. String? discountType,
  3. String? amount,
  4. bool? individualUse,
  5. bool? excludeSaleItems,
  6. String? minimumAmount,
})

Creates an coupon and returns the WooCoupon object.

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

Implementation

Future<WooCoupon> createCoupon({
  String? code,
  String? discountType,
  String? amount,
  bool? individualUse,
  bool? excludeSaleItems,
  String? minimumAmount,
}) async {
  Map<String, dynamic> payload = {};

  ({
    'code': code,
    'discount_type': discountType,
    'amount': amount,
    'individual_use': individualUse,
    'exclude_sale_items': excludeSaleItems,
    'minimum_amount': minimumAmount,
  }).forEach((k, v) {
    if (v != null) payload[k] = v.toString();
  });
  WooCoupon coupon;
  _setApiResourceUrl(
    path: 'coupons',
  );
  final response = await post(queryUri.toString(), payload);
  _printToLog('response gotten : ' + response.toString());
  coupon = WooCoupon.fromJson(response);
  return coupon;
}