-
Notifications
You must be signed in to change notification settings - Fork 1
/
product_attribute_terms.go
167 lines (129 loc) · 4.17 KB
/
product_attribute_terms.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
//********************************************************************************************************************//
//
// 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"
)
// ProductAttributeTermBody is to structure the request data
type ProductAttributeTermBody struct {
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
Description string `json:"description,omitempty"`
MenuOrder int `json:"menu_order,omitempty"`
}
// ProductAttributeTermsReturn is to decode the data
type ProductAttributeTermsReturn struct {
Id int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description"`
MenuOrder int `json:"menu_order"`
Count int `json:"count"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
Collection []struct {
Href string `json:"href"`
} `json:"collection"`
} `json:"_links"`
}
// ProductAttributeTerms is to get all terms
func ProductAttributeTerms(attributeId int, r Request) ([]ProductAttributeTermsReturn, error) {
// Set config for new request
c := Config{fmt.Sprintf("/wp-json/wc/v3/products/attributes/%d/terms", attributeId), "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 []ProductAttributeTermsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return nil, err
}
// Return data
return decode, err
}
// CreateProductAttributeTerms is to create a new term
func CreateProductAttributeTerms(attributeId int, body ProductAttributeTermBody, r Request) (ProductAttributeTermsReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return ProductAttributeTermsReturn{}, err
}
// Set config for new request
c := Config{fmt.Sprintf("/wp-json/wc/v3/products/attributes/%d/terms", attributeId), "POST", convert}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductAttributeTermsReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductAttributeTermsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductAttributeTermsReturn{}, err
}
// Return data
return decode, err
}
// UpdateProductAttributeTerms is to update a term
func UpdateProductAttributeTerms(attributeId, termId int, body ProductAttributeTermBody, r Request) (ProductAttributeTermsReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return ProductAttributeTermsReturn{}, err
}
// Set config for new request
c := Config{fmt.Sprintf("/wp-json/wc/v3/products/attributes/%d/terms/%d", attributeId, termId), "PUT", convert}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductAttributeTermsReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductAttributeTermsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductAttributeTermsReturn{}, err
}
// Return data
return decode, err
}
// DeleteProductAttributeTerms is to delete a term
func DeleteProductAttributeTerms(attributeId, termId int, force bool, r Request) (ProductAttributeTermsReturn, error) {
// Set config for new request
c := Config{fmt.Sprintf("/wp-json/wc/v3/products/attributes/%d/terms/%d?force=%t", attributeId, termId, force), "DELETE", nil}
// Send request
response, err := c.Send(r)
if err != nil {
return ProductAttributeTermsReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ProductAttributeTermsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductAttributeTermsReturn{}, err
}
// Return data
return decode, err
}