forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariant.go
113 lines (99 loc) · 4.62 KB
/
variant.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
package goshopify
import (
"fmt"
"time"
"github.com/shopspring/decimal"
)
const variantsBasePath = "variants"
// VariantService is an interface for interacting with the variant endpoints
// of the Shopify API.
// See https://help.shopify.com/api/reference/product_variant
type VariantService interface {
List(int64, interface{}) ([]Variant, error)
Count(int64, interface{}) (int, error)
Get(int64, interface{}) (*Variant, error)
Create(int64, Variant) (*Variant, error)
Update(Variant) (*Variant, error)
Delete(int64, int64) error
}
// VariantServiceOp handles communication with the variant related methods of
// the Shopify API.
type VariantServiceOp struct {
client *Client
}
// Variant represents a Shopify variant
type Variant struct {
ID int64 `json:"id,omitempty"`
ProductID int64 `json:"product_id,omitempty"`
Title string `json:"title,omitempty"`
Sku string `json:"sku,omitempty"`
Position int `json:"position,omitempty"`
Grams int `json:"grams,omitempty"`
InventoryPolicy string `json:"inventory_policy,omitempty"`
Price *decimal.Decimal `json:"price,omitempty"`
CompareAtPrice *decimal.Decimal `json:"compare_at_price,omitempty"`
FulfillmentService string `json:"fulfillment_service,omitempty"`
InventoryManagement string `json:"inventory_management,omitempty"`
InventoryItemId int64 `json:"inventory_item_id,omitempty"`
Option1 string `json:"option1,omitempty"`
Option2 string `json:"option2,omitempty"`
Option3 string `json:"option3,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
Taxable bool `json:"taxable,omitempty"`
Barcode string `json:"barcode,omitempty"`
ImageID int64 `json:"image_id,omitempty"`
InventoryQuantity int `json:"inventory_quantity,omitempty"`
Weight *decimal.Decimal `json:"weight,omitempty"`
WeightUnit string `json:"weight_unit,omitempty"`
OldInventoryQuantity int `json:"old_inventory_quantity,omitempty"`
RequireShipping bool `json:"requires_shipping,omitempty"`
AdminGraphqlAPIID string `json:"admin_graphql_api_id,omitempty"`
}
// VariantResource represents the result from the variants/X.json endpoint
type VariantResource struct {
Variant *Variant `json:"variant"`
}
// VariantsResource represents the result from the products/X/variants.json endpoint
type VariantsResource struct {
Variants []Variant `json:"variants"`
}
// List variants
func (s *VariantServiceOp) List(productID int64, options interface{}) ([]Variant, error) {
path := fmt.Sprintf("%s/%s/%d/variants.json", globalApiPathPrefix, productsBasePath, productID)
resource := new(VariantsResource)
err := s.client.Get(path, resource, options)
return resource.Variants, err
}
// Count variants
func (s *VariantServiceOp) Count(productID int64, options interface{}) (int, error) {
path := fmt.Sprintf("%s/%s/%d/variants/count.json", globalApiPathPrefix, productsBasePath, productID)
return s.client.Count(path, options)
}
// Get individual variant
func (s *VariantServiceOp) Get(variantID int64, options interface{}) (*Variant, error) {
path := fmt.Sprintf("%s/%s/%d.json", globalApiPathPrefix, variantsBasePath, variantID)
resource := new(VariantResource)
err := s.client.Get(path, resource, options)
return resource.Variant, err
}
// Create a new variant
func (s *VariantServiceOp) Create(productID int64, variant Variant) (*Variant, error) {
path := fmt.Sprintf("%s/%s/%d/variants.json", globalApiPathPrefix, productsBasePath, productID)
wrappedData := VariantResource{Variant: &variant}
resource := new(VariantResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Variant, err
}
// Update existing variant
func (s *VariantServiceOp) Update(variant Variant) (*Variant, error) {
path := fmt.Sprintf("%s/%s/%d.json", globalApiPathPrefix, variantsBasePath, variant.ID)
wrappedData := VariantResource{Variant: &variant}
resource := new(VariantResource)
err := s.client.Put(path, wrappedData, resource)
return resource.Variant, err
}
// Delete an existing product
func (s *VariantServiceOp) Delete(productID int64, variantID int64) error {
return s.client.Delete(fmt.Sprintf("%s/%s/%d/variants/%d.json", globalApiPathPrefix, productsBasePath, productID, variantID))
}