WooCoupon class
WooCommerce Coupon Model
This class represents a discount coupon in WooCommerce with comprehensive rules, restrictions, and usage tracking capabilities. Coupons can provide percentage or fixed amount discounts with various conditions and limitations.
Key Features
- Flexible Discount Types: Percentage, fixed cart, or fixed product discounts
- Usage Controls: Limit total usage, per-user usage, and individual use restrictions
- Product/Category Targeting: Apply to specific products or categories
- Amount Restrictions: Set minimum and maximum order amounts
- Email Restrictions: Limit usage to specific customer emails
- Expiration Support: Set coupon expiration dates
- Free Shipping: Enable free shipping when coupon is applied
Usage Examples
Creating a Coupon
final coupon = WooCoupon(
code: 'SAVE20',
discountType: 'percent',
amount: '20',
description: '20% off your order',
usageLimit: 100,
);
Working with Coupon Data
// Check if coupon is expired
if (coupon.dateExpires != null &&
coupon.dateExpires!.isBefore(DateTime.now())) {
print('Coupon has expired');
}
// Check usage limits
if (coupon.usageLimit != null &&
coupon.usageCount! >= coupon.usageLimit!) {
print('Coupon usage limit reached');
}
JSON Serialization
The class supports full JSON serialization for API communication:
// Convert to JSON for API requests
final json = coupon.toJson();
// Create from JSON response
final coupon = WooCoupon.fromJson(jsonData);
Constructors
-
WooCoupon({int? id, String? code, String? amount, DateTime? dateCreated, DateTime? dateCreatedGmt, DateTime? dateModified, DateTime? dateModifiedGmt, String? discountType, String? description, DateTime? dateExpires, DateTime? dateExpiresGmt, int? usageCount, bool? individualUse, List<
int> ? productIds, List<int> ? excludedProductIds, int? usageLimit, int? usageLimitPerUser, int? limitUsageToXItems, bool? freeShipping, List<int> ? productCategories, List<int> ? excludedProductCategories, bool? excludeSaleItems, String? minimumAmount, String? maximumAmount, List<String> ? emailRestrictions, List<String> ? usedBy, List<WooMetaData> ? metaData}) - Creates a new WooCoupon instance
- WooCoupon.fake([int? id])
-
Creates a fake WooCoupon instance for testing purposes
factory
-
WooCoupon.fromJson(Map<
String, dynamic> json) -
Creates a WooCoupon instance from JSON data
factory
Properties
- amount → String?
-
Discount amount
final
- code → String?
-
Coupon code
final
- dateCreated → DateTime?
-
Date and time when the coupon was created (local time)
final
- dateCreatedGmt → DateTime?
-
Date and time when the coupon was created (GMT)
final
- dateExpires → DateTime?
-
Date and time when the coupon expires (local time)
final
- dateExpiresGmt → DateTime?
-
Date and time when the coupon expires (GMT)
final
- dateModified → DateTime?
-
Date and time when the coupon was last modified (local time)
final
- dateModifiedGmt → DateTime?
-
Date and time when the coupon was last modified (GMT)
final
- description → String?
-
Coupon description
final
- discountType → String?
-
Type of discount
final
-
emailRestrictions
→ List<
String> ? -
List of email addresses that can use this coupon
final
-
excludedProductCategories
→ List<
int> ? -
List of category IDs the coupon does not apply to
final
-
excludedProductIds
→ List<
int> ? -
List of product IDs the coupon cannot be used on
final
- excludeSaleItems → bool?
-
Whether the coupon excludes items on sale
final
- freeShipping → bool?
-
Whether this coupon enables free shipping
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- id → int?
-
Unique identifier for the coupon
final
- individualUse → bool?
-
Whether the coupon can only be used individually
final
- limitUsageToXItems → int?
-
Maximum number of items the coupon can be applied to
final
- maximumAmount → String?
-
Maximum order amount allowed when using the coupon
final
-
metaData
→ List<
WooMetaData> ? -
Additional metadata related to the coupon
final
- minimumAmount → String?
-
Minimum order amount required to apply the coupon
final
-
productCategories
→ List<
int> ? -
List of category IDs the coupon applies to
final
-
productIds
→ List<
int> ? -
List of product IDs the coupon can be used on
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- usageCount → int?
-
Number of times the coupon has been used
final
- usageLimit → int?
-
Maximum number of times the coupon can be used in total
final
- usageLimitPerUser → int?
-
Maximum number of times the coupon can be used per customer
final
-
usedBy
→ List<
String> ? -
List of users who have used this coupon
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toJson(
) → Map< String, dynamic> - Converts the WooCoupon instance to JSON format
-
toString(
) → String -
Returns a string representation of the WooCoupon instance
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited