-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfigurable_products.go
81 lines (64 loc) · 2.6 KB
/
configurable_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
package magento2
import (
"fmt"
)
type MConfigurableProduct struct {
Route string
Options *[]Option
APIClient *Client
}
func SetOptionForExistingConfigurableProduct(sku string, o *ConfigurableProductOption, apiClient *Client) (*MConfigurableProduct, error) {
mConfigurableProduct := &MConfigurableProduct{
Route: configurableProducts + "/" + sku,
Options: &[]Option{},
APIClient: apiClient,
}
endpoint := mConfigurableProduct.Route + "/" + configurableProductsOptionsRelative
httpClient := apiClient.HTTPClient
payLoad := createConfigurableProductByOptionPayload{
Option: *o,
}
resp, err := httpClient.R().SetBody(payLoad).Post(endpoint)
err = mayReturnErrorForHTTPResponse(err, resp, "create configurable product")
if err != nil {
return mConfigurableProduct, err
}
err = mConfigurableProduct.UpdateOptionsFromRemote()
return mConfigurableProduct, err
}
func (mConfigurableProduct *MConfigurableProduct) UpdateOptionsFromRemote() error {
httpClient := mConfigurableProduct.APIClient.HTTPClient
resp, err := httpClient.R().SetResult(mConfigurableProduct.Options).Get(mConfigurableProduct.Route + "/" + configurableProductsOptionsAllRelative)
return mayReturnErrorForHTTPResponse(err, resp, "get options for configurable product from remote")
}
func (mConfigurableProduct *MConfigurableProduct) AddChildBySKU(sku string) error {
httpClient := mConfigurableProduct.APIClient.HTTPClient
payLoad := addChildSKUPayload{
Sku: sku,
}
endpoint := fmt.Sprintf("%s/%s", mConfigurableProduct.Route, configurableProductsChildRelative)
resp, err := httpClient.R().SetBody(payLoad).Post(endpoint)
return mayReturnErrorForHTTPResponse(err, resp, "add child by sku to configurable product")
}
func GetConfigurableProductBySKU(sku string, apiClient *Client) (*MConfigurableProduct, error) {
mConfigurableProduct := &MConfigurableProduct{
Route: configurableProducts + "/" + sku,
Options: &[]Option{},
APIClient: apiClient,
}
err := mConfigurableProduct.UpdateOptionsFromRemote()
return mConfigurableProduct, err
}
func (mConfigurableProduct *MConfigurableProduct) UpdateOptionByID(o *ConfigurableProductOption) error {
httpClient := mConfigurableProduct.APIClient.HTTPClient
endpoint := fmt.Sprintf("%s/%s/%d", mConfigurableProduct.Route, configurableProductsOptionsRelative, o.ID)
payLoad := createConfigurableProductByOptionPayload{
Option: *o,
}
resp, err := httpClient.R().SetBody(payLoad).Put(endpoint)
err = mayReturnErrorForHTTPResponse(err, resp, "update option for configurable product")
if err != nil {
return err
}
return mConfigurableProduct.UpdateOptionsFromRemote()
}