-
Notifications
You must be signed in to change notification settings - Fork 1
/
product_attributes.go
168 lines (130 loc) · 3.92 KB
/
product_attributes.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
//********************************************************************************************************************//
//
// Copyright (C) 2018 - 2021 J&J Ideenschmiede GmbH <info@jj-ideenschmiede.de>
//
// 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"
)
// ProductAttributesBody is to structure the data
type ProductAttributesBody struct {
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
Type string `json:"type,omitempty"`
OrderBy string `json:"order_by,omitempty"`
HasArchives bool `json:"has_archives,omitempty"`
}
// ProductAttributesReturn is to decode the data
type ProductAttributesReturn struct {
Id int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Type string `json:"type"`
OrderBy string `json:"order_by"`
HasArchives bool `json:"has_archives"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
Collection []struct {
Href string `json:"href"`
} `json:"collection"`
} `json:"_links"`
}
// ProductAttributes is to get a list of all product attributes
func ProductAttributes(r Request) ([]ProductAttributesReturn, error) {
// Set config for new request
c := Config{"/wp-json/wc/v3/products/attributes", "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 []ProductAttributesReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return nil, err
}
// Return data
return decode, err
}
// CreateProductAttributes is to create a new attribute
func CreateProductAttributes(body ProductAttributesBody, r Request) (ProductAttributesReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return ProductAttributesReturn{}, err
}
// Set config for new request
c := Config{"/wp-json/wc/v3/products/attributes/", "POST", convert}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductAttributesReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductAttributesReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductAttributesReturn{}, err
}
// Return data
return decode, err
}
// UpdateProductAttributes is to update a attribute
func UpdateProductAttributes(id int, body ProductAttributesBody, r Request) (ProductAttributesReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return ProductAttributesReturn{}, err
}
// Set config for new request
c := Config{fmt.Sprintf("/wp-json/wc/v3/products/attributes/%d", id), "PUT", convert}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductAttributesReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductAttributesReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductAttributesReturn{}, err
}
// Return data
return decode, err
}
// DeleteProductAttributes is to delete a product attribute
func DeleteProductAttributes(id int, r Request) (ProductAttributesReturn, error) {
// Set config for new request
c := Config{fmt.Sprintf("/wp-json/wc/v3/products/attributes/%d", id), "DELETE", nil}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductAttributesReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductAttributesReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductAttributesReturn{}, err
}
// Return data
return decode, err
}