This repository has been archived by the owner on Sep 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 323
/
variant.go
111 lines (97 loc) · 4.31 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
package goshopify
import (
"fmt"
"time"
"github.com/shopspring/decimal"
)
const variantsBasePath = "admin/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(int, interface{}) ([]Variant, error)
Count(int, interface{}) (int, error)
Get(int, interface{}) (*Variant, error)
Create(int, Variant) (*Variant, error)
Update(Variant) (*Variant, error)
Delete(int, int) 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 int `json:"id,omitempty"`
ProductID int `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"`
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 int `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"`
}
// 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 int, options interface{}) ([]Variant, error) {
path := fmt.Sprintf("%s/%d/variants.json", productsBasePath, productID)
resource := new(VariantsResource)
err := s.client.Get(path, resource, options)
return resource.Variants, err
}
// Count variants
func (s *VariantServiceOp) Count(productID int, options interface{}) (int, error) {
path := fmt.Sprintf("%s/%d/variants/count.json", productsBasePath, productID)
return s.client.Count(path, options)
}
// Get individual variant
func (s *VariantServiceOp) Get(variantID int, options interface{}) (*Variant, error) {
path := fmt.Sprintf("%s/%d.json", 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 int, variant Variant) (*Variant, error) {
path := fmt.Sprintf("%s/%d/variants.json", 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/%d.json", 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 int, variantID int) error {
return s.client.Delete(fmt.Sprintf("%s/%d/variants/%d.json", productsBasePath, productID, variantID))
}