rust_woocommerce/models/
products.rs

1use crate::controllers::{
2    products::{ProductModify, ProductModifyBuilder},
3    Entity,
4};
5
6use super::MetaData;
7use chrono::NaiveDateTime;
8use serde::{Deserialize, Serialize};
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct Product {
11    /// Unique identifier for the resource.
12    pub id: i32,
13    /// Product name.
14    pub name: String,
15    /// Product slug.
16    pub slug: String,
17    /// Product URL.
18    pub permalink: String,
19    /// The date the product was created, in the site's timezone.
20    pub date_created: NaiveDateTime,
21    /// The date the product was created, as GMT.
22    pub date_created_gmt: NaiveDateTime,
23    /// The date the product was last modified, in the site's timezone.
24    pub date_modified: NaiveDateTime,
25    /// The date the product was last modified, as GMT.
26    pub date_modified_gmt: NaiveDateTime,
27    /// Product type, Options: simple, grouped, external and variable. Default is simple.
28    #[serde(rename = "type")]
29    pub product_type: ProductType,
30    /// Product status (post status). Options: draft, pending, private and publish. Default is publish.
31    pub status: ProductStatus,
32    /// Featured product. Default is false.
33    pub featured: bool,
34    /// Catalog visibility. Options: visible, catalog, search and hidden. Default is visible.
35    pub catalog_visibility: CatalogVisibility,
36    /// Product description.
37    pub description: String,
38    /// Product short description.
39    pub short_description: String,
40    /// Unique identifier.
41    pub sku: String,
42    /// Current product price.
43    pub price: String,
44    /// Product regular price.
45    pub regular_price: String,
46    /// Product sale price.
47    pub sale_price: String,
48    /// Start date of sale price, in the site's timezone.
49    pub date_on_sale_from: Option<NaiveDateTime>,
50    /// Start date of sale price, as GMT.
51    pub date_on_sale_from_gmt: Option<NaiveDateTime>,
52    /// End date of sale price, in the site's timezone.
53    pub date_on_sale_to: Option<NaiveDateTime>,
54    /// End date of sale price, as GMT.
55    pub date_on_sale_to_gmt: Option<NaiveDateTime>,
56    /// Price formatted in HTML.
57    pub price_html: String,
58    /// Shows if the product is on sale.
59    pub on_sale: bool,
60    /// Shows if the product can be bought.
61    pub purchasable: bool,
62    /// Amount of sales.
63    pub total_sales: i32,
64    /// If the product is virtual. Default is false.
65    #[serde(rename = "virtual")]
66    pub is_virtual: bool,
67    /// If the product is downloadable. Default is false.
68    pub downloadable: bool,
69    /// List of downloadable files. See Product - Downloads properties
70    pub downloads: Vec<Download>,
71    /// Number of times downloadable files can be downloaded after purchase. Default is -1.
72    pub download_limit: i32,
73    /// Number of days until access to downloadable files expires. Default is -1.
74    pub download_expiry: i32,
75    /// Product external URL. Only for external products.
76    pub external_url: String,
77    /// Product external button text. Only for external products.
78    pub button_text: String,
79    /// Tax status. Options: taxable, shipping and none. Default is taxable.
80    pub tax_status: TaxStatus,
81    /// Tax class.
82    pub tax_class: String,
83    /// Stock management at product level. Default is false.
84    pub manage_stock: bool,
85    /// Stock quantity.
86    pub stock_quantity: Option<i32>,
87    /// Controls the stock status of the product. Options: instock, outofstock, onbackorder. Default is instock.
88    pub stock_status: StockStatus,
89    /// If managing stock, this controls if backorders are allowed. Options: no, notify and yes. Default is no.
90    pub backorders: BackordersStatus,
91    /// Shows if backorders are allowed.
92    pub backorders_allowed: bool,
93    /// Shows if the product is on backordered.
94    pub backordered: bool,
95    /// Allow one item to be bought in a single order. Default is false.
96    pub sold_individually: bool,
97    /// Product weight.
98    pub weight: String,
99    /// Product dimensions.
100    pub dimensions: Dimensions,
101    /// Shows if the product need to be shipped.
102    pub shipping_required: bool,
103    /// Shows whether or not the product shipping is taxable.READ-ONLY
104    pub shipping_taxable: bool,
105    /// Shipping class slug.
106    pub shipping_class: String,
107    /// Shipping class ID.
108    pub shipping_class_id: i32,
109    /// Allow reviews. Default is true.
110    pub reviews_allowed: bool,
111    /// Reviews average rating.
112    pub average_rating: String,
113    /// Amount of reviews that the product have.
114    pub rating_count: i32,
115    /// List of related products IDs.
116    pub related_ids: Vec<i32>,
117    /// List of up-sell products IDs.
118    pub upsell_ids: Vec<i32>,
119    /// List of cross-sell products IDs.
120    pub cross_sell_ids: Vec<i32>,
121    /// Product parent ID.
122    pub parent_id: i32,
123    /// Optional note to send the customer after purchase.
124    pub purchase_note: String,
125    /// List of categories.
126    pub categories: Vec<ProductCategory>,
127    /// List of tags.
128    pub tags: Vec<ProductTag>,
129    /// List of images.
130    pub images: Vec<ProductImage>,
131    /// List of attributes.
132    pub attributes: Vec<ProductAttribute>,
133    /// Defaults variation attributes.
134    pub default_attributes: Vec<ProductDefaultAttribute>,
135    /// List of variations IDs.
136    pub variations: Vec<i32>,
137    /// List of grouped products ID.
138    pub grouped_products: Vec<i32>,
139    /// Menu order, used to custom sort products.
140    pub menu_order: i32,
141    /// Meta data.
142    pub meta_data: Vec<MetaData>,
143}
144impl Entity for Product {
145    fn endpoint() -> String {
146        String::from("products/")
147    }
148    fn child_endpoint(parent_id: i32) -> String {
149        let _ = parent_id;
150        String::new()
151    }
152}
153impl Product {
154    pub fn builder() -> ProductModifyBuilder {
155        ProductModify::builder()
156    }
157}
158#[derive(Debug, Clone, Serialize, Deserialize, Default)]
159#[serde(rename_all = "lowercase")]
160pub enum ProductType {
161    #[default]
162    Simple,
163    Grouped,
164    External,
165    Variable,
166}
167#[derive(Debug, Clone, Serialize, Deserialize, Default)]
168#[serde(rename_all = "lowercase")]
169pub enum ProductStatus {
170    Draft,
171    Pending,
172    Private,
173    #[default]
174    Publish,
175}
176#[derive(Debug, Clone, Serialize, Deserialize, Default)]
177#[serde(rename_all = "lowercase")]
178pub enum CatalogVisibility {
179    #[default]
180    Visible,
181    Catalog,
182    Search,
183    Hidden,
184}
185#[derive(Debug, Clone, Serialize, Deserialize, Default)]
186#[serde(rename_all = "lowercase")]
187pub enum TaxStatus {
188    #[default]
189    Taxable,
190    Shipping,
191    None,
192}
193#[derive(Debug, Clone, Serialize, Deserialize, Default)]
194#[serde(rename_all = "lowercase")]
195pub enum StockStatus {
196    #[default]
197    Instock,
198    Outofstock,
199    Onbackorder,
200}
201#[derive(Debug, Clone, Serialize, Deserialize, Default)]
202#[serde(rename_all = "lowercase")]
203pub enum BackordersStatus {
204    #[default]
205    No,
206    Notify,
207    Yes,
208}
209#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct Download {
211    /// File ID.
212    pub id: String,
213    /// File name.
214    pub name: String,
215    /// File URL.
216    pub file: String,
217}
218#[derive(Debug, Clone, Serialize, Deserialize)]
219pub struct Dimensions {
220    /// Product length.
221    pub length: String,
222    /// Product width.
223    pub width: String,
224    /// Product height.
225    pub height: String,
226}
227#[derive(Debug, Clone, Serialize, Deserialize)]
228pub struct ProductCategory {
229    /// Category ID.
230    pub id: i32,
231    /// Category name.
232    pub name: String,
233    /// Category slug.
234    pub slug: String,
235}
236#[derive(Debug, Clone, Serialize, Deserialize)]
237pub struct ProductTag {
238    /// Tag ID.
239    pub id: i32,
240    /// Tag name.
241    pub name: String,
242    /// Tag slug.
243    pub slug: String,
244}
245#[derive(Debug, Clone, Serialize, Deserialize)]
246pub struct ProductImage {
247    /// Image ID.
248    pub id: i32,
249    /// The date the image was created, in the site's timezone.
250    pub date_created: NaiveDateTime,
251    /// The date the image was created, as GMT.
252    pub date_created_gmt: NaiveDateTime,
253    /// The date the image was last modified, in the site's timezone.
254    pub date_modified: NaiveDateTime,
255    /// The date the image was last modified, as GMT.
256    pub date_modified_gmt: NaiveDateTime,
257    /// Image URL.
258    pub src: String,
259    /// Image name.
260    pub name: String,
261    /// Image alternative text.
262    pub alt: String,
263}
264#[derive(Debug, Clone, Serialize, Deserialize)]
265pub struct ProductAttribute {
266    /// Attribute ID.
267    pub id: i32,
268    /// Attribute name.
269    pub name: String,
270    /// Attribute position.
271    pub position: i32,
272    /// Define if the attribute is visible on the "Additional information" tab in the product's page. Default is false.
273    pub visible: bool,
274    /// Define if the attribute can be used as variation. Default is false.
275    pub variation: bool,
276    /// List of available term names of the attribute.
277    pub options: Vec<String>,
278}
279#[derive(Debug, Clone, Serialize, Deserialize)]
280pub struct ProductDefaultAttribute {
281    /// Attribute ID.
282    pub id: i32,
283    /// Attribute name.
284    pub name: String,
285    /// Selected attribute term name.
286    pub option: String,
287}