-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproducts.go
379 lines (325 loc) · 12.7 KB
/
products.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//********************************************************************************************************************//
//
// Copyright (C) 2018 - 2021 J&J Ideenschmiede GmbH <[email protected]>
//
// This file is part of gowoocommerce.
// All code may be used. Feel free and maybe code something better.
//
// Author: Jonas Kwiedor (aka gowizzard)
//
//********************************************************************************************************************//
package gowoocommerce
import (
"encoding/json"
"fmt"
)
// ProductsBody is to structure the request data
type ProductsBody struct {
Name string `json:"name"`
Slug string `json:"slug"`
Type string `json:"type"`
Status string `json:"status"`
Featured bool `json:"featured"`
CatalogVisibility string `json:"catalog_visibility"`
Description string `json:"description"`
ShortDescription string `json:"short_description"`
Sku string `json:"sku"`
Price string `json:"price"`
RegularPrice string `json:"regular_price"`
SalePrice string `json:"sale_price"`
DateOnSaleFrom interface{} `json:"date_on_sale_from"`
DateOnSaleFromGmt interface{} `json:"date_on_sale_from_gmt"`
DateOnSaleTo interface{} `json:"date_on_sale_to"`
DateOnSaleToGmt interface{} `json:"date_on_sale_to_gmt"`
OnSale bool `json:"on_sale"`
Purchasable bool `json:"purchasable"`
TotalSales int `json:"total_sales"`
Virtual bool `json:"virtual"`
Downloadable bool `json:"downloadable"`
Downloads []interface{} `json:"downloads"`
DownloadLimit int `json:"download_limit"`
DownloadExpiry int `json:"download_expiry"`
ExternalUrl string `json:"external_url"`
ButtonText string `json:"button_text"`
TaxStatus string `json:"tax_status"`
TaxClass string `json:"tax_class"`
ManageStock bool `json:"manage_stock"`
StockQuantity interface{} `json:"stock_quantity"`
Backorders string `json:"backorders"`
BackordersAllowed bool `json:"backorders_allowed"`
Backordered bool `json:"backordered"`
LowStockAmount interface{} `json:"low_stock_amount"`
SoldIndividually bool `json:"sold_individually"`
Weight string `json:"weight"`
Dimensions ProductsBodyDimensions `json:"dimensions,omitempty"`
ShippingRequired bool `json:"shipping_required"`
ShippingTaxable bool `json:"shipping_taxable"`
ShippingClass string `json:"shipping_class"`
ShippingClassId int `json:"shipping_class_id"`
ReviewsAllowed bool `json:"reviews_allowed"`
AverageRating string `json:"average_rating"`
RatingCount int `json:"rating_count"`
UpsellIds []interface{} `json:"upsell_ids"`
CrossSellIds []interface{} `json:"cross_sell_ids"`
ParentId int `json:"parent_id"`
PurchaseNote string `json:"purchase_note"`
Categories []ProductsBodyCategories `json:"categories,omitempty"`
Tags []ProductsBodyTags `json:"tags,omitempty"`
Images []ProductsBodyImages `json:"images,omitempty"`
Attributes []ProductsBodyAttributes `json:"attributes,omitempty"`
DefaultAttributes []interface{} `json:"default_attributes"`
Variations []interface{} `json:"variations"`
GroupedProducts []interface{} `json:"grouped_products"`
MenuOrder int `json:"menu_order"`
RelatedIds []interface{} `json:"related_ids"`
MetaData []interface{} `json:"meta_data"`
}
type ProductsBodyDimensions struct {
Length string `json:"length,omitempty"`
Width string `json:"width,omitempty"`
Height string `json:"height,omitempty"`
}
type ProductsBodyCategories struct {
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
}
type ProductsBodyTags struct {
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
}
type ProductsBodyImages struct {
Src string `json:"src,omitempty"`
Name string `json:"name,omitempty"`
Alt string `json:"alt,omitempty"`
}
type ProductsBodyAttributes struct {
Name string `json:"name"`
Position int `json:"position"`
Visible bool `json:"visible"`
Variation bool `json:"variation"`
Options []string `json:"options"`
}
// ProductsReturn is to decode the product return
type ProductsReturn struct {
Id int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Permalink string `json:"permalink"`
DateCreated string `json:"date_created"`
DateCreatedGmt string `json:"date_created_gmt"`
DateModified string `json:"date_modified"`
DateModifiedGmt string `json:"date_modified_gmt"`
Type string `json:"type"`
Status string `json:"status"`
Featured bool `json:"featured"`
CatalogVisibility string `json:"catalog_visibility"`
Description string `json:"description"`
ShortDescription string `json:"short_description"`
Sku string `json:"sku"`
Price string `json:"price"`
RegularPrice string `json:"regular_price"`
SalePrice string `json:"sale_price"`
DateOnSaleFrom interface{} `json:"date_on_sale_from"`
DateOnSaleFromGmt interface{} `json:"date_on_sale_from_gmt"`
DateOnSaleTo interface{} `json:"date_on_sale_to"`
DateOnSaleToGmt interface{} `json:"date_on_sale_to_gmt"`
OnSale bool `json:"on_sale"`
Purchasable bool `json:"purchasable"`
TotalSales int `json:"total_sales"`
Virtual bool `json:"virtual"`
Downloadable bool `json:"downloadable"`
Downloads []interface{} `json:"downloads"`
DownloadLimit int `json:"download_limit"`
DownloadExpiry int `json:"download_expiry"`
ExternalUrl string `json:"external_url"`
ButtonText string `json:"button_text"`
TaxStatus string `json:"tax_status"`
TaxClass string `json:"tax_class"`
ManageStock bool `json:"manage_stock"`
StockQuantity interface{} `json:"stock_quantity"`
Backorders string `json:"backorders"`
BackordersAllowed bool `json:"backorders_allowed"`
Backordered bool `json:"backordered"`
LowStockAmount interface{} `json:"low_stock_amount"`
SoldIndividually bool `json:"sold_individually"`
Weight string `json:"weight"`
Dimensions struct {
Length string `json:"length"`
Width string `json:"width"`
Height string `json:"height"`
} `json:"dimensions"`
ShippingRequired bool `json:"shipping_required"`
ShippingTaxable bool `json:"shipping_taxable"`
ShippingClass string `json:"shipping_class"`
ShippingClassId int `json:"shipping_class_id"`
ReviewsAllowed bool `json:"reviews_allowed"`
AverageRating string `json:"average_rating"`
RatingCount int `json:"rating_count"`
UpsellIds []interface{} `json:"upsell_ids"`
CrossSellIds []interface{} `json:"cross_sell_ids"`
ParentId int `json:"parent_id"`
PurchaseNote string `json:"purchase_note"`
Categories []struct {
Id int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"categories"`
Tags []interface{} `json:"tags"`
Images []interface{} `json:"images"`
Attributes []interface{} `json:"attributes"`
DefaultAttributes []interface{} `json:"default_attributes"`
Variations []interface{} `json:"variations"`
GroupedProducts []interface{} `json:"grouped_products"`
MenuOrder int `json:"menu_order"`
PriceHtml string `json:"price_html"`
RelatedIds []interface{} `json:"related_ids"`
MetaData []interface{} `json:"meta_data"`
StockStatus string `json:"stock_status"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
Collection []struct {
Href string `json:"href"`
} `json:"collection"`
} `json:"_links"`
Code string `json:"code"`
Message string `json:"message"`
Data struct {
Status int `json:"status"`
Params struct {
Backorders string `json:"backorders"`
} `json:"params"`
Details struct {
Backorders struct {
Code string `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
} `json:"backorders"`
} `json:"details"`
} `json:"data"`
}
// Products are to get a list of all products per page
func Products(page int, r Request) ([]ProductsReturn, error) {
// Set config for new request
c := Config{fmt.Sprintf("/wp-json/wc/v3/products?page=%d&per_page=100", page), "GET", nil}
// Send request
response, err := c.Send(r)
if err != nil {
return nil, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode []ProductsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return nil, err
}
// Return data
return decode, err
}
// CreateProduct is to create a new product
func CreateProduct(body ProductsBody, r Request) (ProductsReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return ProductsReturn{}, err
}
// Set config for new request
c := Config{"/wp-json/wc/v3/products", "POST", convert}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductsReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductsReturn{}, err
}
// Return data
return decode, err
}
// UpdateProduct is to update a product
func UpdateProduct(id int, body ProductsBody, r Request) (ProductsReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return ProductsReturn{}, err
}
// Set config for new request
c := Config{fmt.Sprintf("/wp-json/wc/v3/products/%d", id), "PUT", convert}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductsReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductsReturn{}, err
}
// Return data
return decode, err
}
// UpdateProductStock is to update a stock
func UpdateProductStock(id, stock int, r Request) (ProductsReturn, error) {
// Define request body & set stock to body struct
type RequestBody struct {
StockQuantity int `json:"stock_quantity"`
}
body := RequestBody{
StockQuantity: stock,
}
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return ProductsReturn{}, err
}
// Set config for new request
c := Config{fmt.Sprintf("/wp-json/wc/v3/products/%d", id), "PUT", convert}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductsReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductsReturn{}, err
}
// Return data
return decode, err
}
// DeleteProduct is to remove a product from woocommerce
func DeleteProduct(id int, force bool, r Request) (ProductsReturn, error) {
// Set config for new request
c := Config{fmt.Sprintf("/wp-json/wc/v3/products/%d?force=%t", id, force), "DELETE", nil}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductsReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductsReturn{}, err
}
// Return data
return decode, err
}