rust_woocommerce/models/coupons.rs
1use chrono::NaiveDateTime;
2use serde::{Deserialize, Serialize};
3
4use crate::controllers::coupons::{
5 CreateCouponBuilder, NoAmount, NoCode, NoDiscountType, UpdateCouponBuilder,
6};
7use crate::controllers::Entity;
8
9use super::MetaData;
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct Coupon {
13 /// Unique identifier for the object.
14 pub id: i32,
15 /// Coupon code.
16 pub code: String,
17 /// The amount of discount. Should always be numeric, even if setting a percentage.
18 pub amount: String,
19 /// The date the coupon was created, in the site's timezone.
20 pub date_created: NaiveDateTime,
21 /// The date the coupon was created, as GMT.
22 pub date_created_gmt: NaiveDateTime,
23 /// The date the coupon was last modified, in the site's timezone.
24 pub date_modified: NaiveDateTime,
25 /// The date the coupon was last modified, as GMT.
26 pub date_modified_gmt: NaiveDateTime,
27 /// Determines the type of discount that will be applied. Options: percent, fixed_cart and fixed_product. Default is fixed_cart.
28 pub discount_type: DiscountType,
29 /// Coupon description.
30 pub description: String,
31 /// The date the coupon expires, in the site's timezone.
32 pub date_expires: Option<String>,
33 /// The date the coupon expires, as GMT.
34 pub date_expires_gmt: Option<String>,
35 /// Number of times the coupon has been used already.
36 pub usage_count: i32,
37 /// If true, the coupon can only be used individually. Other applied coupons will be removed from the cart. Default is false.
38 pub individual_use: bool,
39 /// List of product IDs the coupon can be used on.
40 pub product_ids: Vec<i32>,
41 /// List of product IDs the coupon cannot be used on.
42 pub excluded_product_ids: Vec<i32>,
43 /// How many times the coupon can be used in total.
44 pub usage_limit: Option<i32>,
45 /// How many times the coupon can be used per customer.
46 pub usage_limit_per_user: Option<i32>,
47 /// Max number of items in the cart the coupon can be applied to.
48 pub limit_usage_to_x_items: Option<i32>,
49 /// If true and if the free shipping method requires a coupon, this coupon will enable free shipping. Default is false.
50 pub free_shipping: bool,
51 /// List of category IDs the coupon applies to.
52 pub product_categories: Vec<i32>,
53 /// List of category IDs the coupon does not apply to.
54 pub excluded_product_categories: Vec<i32>,
55 /// If true, this coupon will not be applied to items that have sale prices. Default is false.
56 pub exclude_sale_items: bool,
57 /// Minimum order amount that needs to be in the cart before coupon applies.
58 pub minimum_amount: String,
59 /// Maximum order amount allowed when using the coupon.
60 pub maximum_amount: String,
61 /// List of email addresses that can use this coupon.
62 pub email_restrictions: Vec<String>,
63 /// List of user IDs (or guest email addresses) that have used the coupon.
64 pub used_by: Vec<String>,
65 /// Meta data.
66 pub meta_data: Vec<MetaData>,
67}
68impl Entity for Coupon {
69 fn endpoint() -> String {
70 String::from("coupons/")
71 }
72 fn child_endpoint(parent_id: i32) -> String {
73 let _ = parent_id;
74 String::new()
75 }
76}
77impl Coupon {
78 pub fn create() -> CreateCouponBuilder<NoCode, NoDiscountType, NoAmount> {
79 CreateCouponBuilder::default()
80 }
81 pub fn update() -> UpdateCouponBuilder {
82 UpdateCouponBuilder::default()
83 }
84}
85/// Determines the type of discount that will be applied.
86#[derive(Debug, Clone, Serialize, Deserialize, Default)]
87#[serde(rename_all = "snake_case")]
88pub enum DiscountType {
89 #[default]
90 FixedCart,
91 FixedProduct,
92 Percent,
93}