forked from hiscaler/woocommerce-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.go
203 lines (180 loc) · 9.07 KB
/
product.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package woocommerce
import (
"fmt"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/hiscaler/woocommerce-go/entity"
jsoniter "github.com/json-iterator/go"
)
type productService service
// Products
type ProductsQueryParams struct {
queryParams
Search string `url:"search,omitempty"`
After string `url:"after,omitempty"`
Before string `url:"before,omitempty"`
Exclude []int `url:"exclude,omitempty"`
Include []int `url:"include,omitempty"`
Parent []int `url:"parent,omitempty"`
ParentExclude []int `url:"parent_exclude,omitempty"`
Slug string `url:"slug,omitempty"`
Status string `url:"status,omitempty"`
Type string `url:"type,omitempty"`
SKU string `url:"sku,omitempty"`
Featured bool `url:"featured,omitempty"`
Category string `url:"category,omitempty"`
Tag string `url:"tag,omitempty"`
ShippingClass string `url:"shipping_class,omitempty"`
Attribute string `url:"attribute,omitempty"`
AttributeTerm string `url:"attribute_term,omitempty"`
TaxClass string `url:"tax_class,omitempty"`
OnSale bool `url:"on_sale,omitempty"`
MinPrice float64 `url:"min_price,string,omitempty"`
MaxPrice float64 `url:"max_price,string,omitempty"`
StockStatus string `url:"stock_status,omitempty"`
}
func (m ProductsQueryParams) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.Before, validation.When(m.Before != "", validation.By(func(value interface{}) error {
dateStr, _ := value.(string)
return IsValidateTime(dateStr)
}))),
validation.Field(&m.After, validation.When(m.After != "", validation.By(func(value interface{}) error {
dateStr, _ := value.(string)
return IsValidateTime(dateStr)
}))),
validation.Field(&m.OrderBy, validation.When(m.OrderBy != "", validation.In("id", "include", "title", "slug", "price", "popularity", "rating").Error("无效的排序字段"))),
validation.Field(&m.Status, validation.When(m.Status != "", validation.In("any", "draft", "pending", "private", "publish").Error("无效的状态"))),
validation.Field(&m.Type, validation.When(m.Type != "", validation.In("simple", "grouped", "external", "variable").Error("无效的类型"))),
)
}
// All List all products
func (s productService) All(params ProductsQueryParams) (items []entity.Product, total, totalPages int, isLastPage bool, err error) {
if err = params.Validate(); err != nil {
return
}
params.TidyVars()
params.After = ToISOTimeString(params.After, false, true)
params.Before = ToISOTimeString(params.Before, true, false)
resp, err := s.httpClient.R().SetQueryParamsFromValues(toValues(params)).Get("/products")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &items)
total, totalPages, isLastPage = parseResponseTotal(params.Page, resp)
}
return
}
// One Retrieve a product
func (s productService) One(id int) (item entity.Product, err error) {
var res entity.Product
resp, err := s.httpClient.R().Get(fmt.Sprintf("/products/%d", id))
if err != nil {
return
}
if resp.IsSuccess() {
if err = jsoniter.Unmarshal(resp.Body(), &res); err == nil {
item = res
}
}
return
}
// Create
type CreateProductRequest struct {
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
Type string `json:"type,omitempty"`
Status string `json:"status,omitempty"`
Featured bool `json:"featured,omitempty"`
CatalogVisibility string `json:"catalog_visibility,omitempty"`
Description string `json:"description,omitempty"`
ShortDescription string `json:"short_description,omitempty"`
SKU string `json:"sku,omitempty"`
RegularPrice float64 `json:"regular_price,string,omitempty"`
SalePrice float64 `json:"sale_price,string,omitempty"`
DateOnSaleFrom string `json:"date_on_sale_from,omitempty"`
DateOnSaleFromGMT string `json:"date_on_sale_from_gmt,omitempty"`
DateOnSaleTo string `json:"date_on_sale_to,omitempty"`
DateOnSaleToGMT string `json:"date_on_sale_to_gmt,omitempty"`
Virtual bool `json:"virtual,omitempty"`
Downloadable bool `json:"downloadable,omitempty"`
Downloads []entity.ProductDownload `json:"downloads,omitempty"`
DownloadLimit int `json:"download_limit,omitempty"`
DownloadExpiry int `json:"download_expiry,omitempty"`
ExternalUrl string `json:"external_url,omitempty"`
ButtonText string `json:"button_text,omitempty"`
TaxStatus string `json:"tax_status,omitempty"`
TaxClass string `json:"tax_class,omitempty"`
ManageStock bool `json:"manage_stock,omitempty"`
StockQuantity int `json:"stock_quantity,omitempty"`
StockStatus string `json:"stock_status,omitempty"`
Backorders string `json:"backorders,omitempty"`
SoldIndividually bool `json:"sold_individually,omitempty"`
Weight string `json:"weight,omitempty"`
Dimensions *entity.ProductDimension `json:"dimensions,omitempty"`
ShippingClass string `json:"shipping_class,omitempty"`
ReviewsAllowed bool `json:"reviews_allowed,omitempty"`
UpsellIds []int `json:"upsell_ids,omitempty"`
CrossSellIds []int `json:"cross_sell_ids,omitempty"`
ParentId int `json:"parent_id,omitempty"`
PurchaseNote string `json:"purchase_note,omitempty"`
Categories []entity.ProductCategory `json:"categories,omitempty"`
Tags []entity.ProductTag `json:"tags,omitempty"`
Images []entity.ProductImage `json:"images,omitempty"`
Attributes []entity.ProductAttribute `json:"attributes,omitempty"`
DefaultAttributes []entity.ProductDefaultAttribute `json:"default_attributes,omitempty"`
GroupedProducts []int `json:"grouped_products,omitempty"`
MenuOrder int `json:"menu_order,omitempty"`
MetaData []entity.Meta `json:"meta_data,omitempty"`
}
func (m CreateProductRequest) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.Name, validation.Required.Error("商品名称不能为空")),
validation.Field(&m.Type, validation.When(m.Type != "", validation.In("simple", "grouped", "external ", "variable").Error("无效的类型"))),
validation.Field(&m.Status, validation.When(m.Status != "", validation.In("draft", "pending", "private", "publish").Error("无效的状态"))),
validation.Field(&m.CatalogVisibility, validation.When(m.CatalogVisibility != "", validation.In("visible", "catalog", "search", "hidden").Error("无效的目录可见性"))),
validation.Field(&m.TaxStatus, validation.When(m.TaxStatus != "", validation.In("taxable", "shipping ", "none").Error("无效的税务状态"))),
validation.Field(&m.StockStatus, validation.When(m.StockStatus != "", validation.In("instock", "outofstock ", "onbackorder").Error("无效的库存状态"))),
validation.Field(&m.Backorders, validation.When(m.Backorders != "", validation.In("yes", "no ", "notify").Error("无效的缺货订单状态"))),
)
}
// Create create a product
func (s productService) Create(req CreateProductRequest) (item entity.Product, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().SetBody(req).Post("/products")
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
// Update
type UpdateProductRequest = CreateProductRequest
func (s productService) Update(id int, req UpdateProductRequest) (item entity.Product, err error) {
if err = req.Validate(); err != nil {
return
}
resp, err := s.httpClient.R().SetBody(req).Put(fmt.Sprintf("/products/%d", id))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}
// Delete delete a product
func (s productService) Delete(id int, force bool) (item entity.Product, err error) {
resp, err := s.httpClient.R().SetBody(map[string]bool{"force": force}).Delete(fmt.Sprintf("/products/%d", id))
if err != nil {
return
}
if resp.IsSuccess() {
err = jsoniter.Unmarshal(resp.Body(), &item)
}
return
}