diff --git a/README.md b/README.md index b53dd75..591139a 100644 --- a/README.md +++ b/README.md @@ -364,6 +364,35 @@ if err != nil { } ``` +## Update a product variant price + +If you want to update the price of a product variant, you can do so using the following function. + +```go +// Define request +r := goshopify.Request{ + StoreName: "", + AccessToken: "", +} + +// Define body +body := ProductVariantPriceBody{ + ProductVariantPriceBodyVariant{ + Id: 6917353078968, + Price: "24,99", + CompareAtPrice: "48,99", + }, +} + +// Update variant price +variantPrice, err := goshopify.UpdateProductVariantPrice(body, r) +if err != nil { + fmt.Println(err) +} else { + fmt.Println(variantPrice) +} +``` + ## Get a list of metafields for a product variant If you want to read out all metafields of a product variant, you can do this as follows. diff --git a/product_variants.go b/product_variants.go index 4a6e329..048d829 100644 --- a/product_variants.go +++ b/product_variants.go @@ -99,6 +99,17 @@ type ProductVariantReturn struct { Errors interface{} `json:"errors,omitempty"` } +// ProductVariantPriceBody is to structure the data +type ProductVariantPriceBody struct { + Variant ProductVariantPriceBodyVariant `json:"variant"` +} + +type ProductVariantPriceBodyVariant struct { + Id int `json:"id"` + Price string `json:"price"` + CompareAtPrice string `json:"compare_at_price,omitempty"` +} + // ProductVariantMetafieldsReturn is to decode the json data type ProductVariantMetafieldsReturn struct { Metafields []struct { @@ -233,6 +244,40 @@ func DeleteVariant(productId, variantId int, r Request) error { } +// UpdateProductVariantPrice is to update the inventory quantity of a product variant +func UpdateProductVariantPrice(body ProductVariantPriceBody, r Request) (ProductVariantReturn, error) { + + // Convert body + convert, err := json.Marshal(body) + if err != nil { + return ProductVariantReturn{}, err + } + + // Set config for new request + c := Config{fmt.Sprintf("/variants/%d.json", body.Variant.Id), http.MethodPut, convert} + + // Send request + response, err := c.Send(r) + if err != nil { + return ProductVariantReturn{}, err + } + + // Close request + defer response.Body.Close() + + // Decode data + var decode ProductVariantReturn + + err = json.NewDecoder(response.Body).Decode(&decode) + if err != nil { + return ProductVariantReturn{}, err + } + + // Return data + return decode, err + +} + // ProductVariantMetafields is to get a list of all product variant metafields func ProductVariantMetafields(productId, variantId int, r Request) (ProductVariantMetafieldsReturn, error) {