Skip to content

Commit

Permalink
Merge pull request #47 from jjideenschmiede/development
Browse files Browse the repository at this point in the history
feat: Add new function for variant prices.
  • Loading branch information
gowizzard authored Mar 5, 2024
2 parents b96e071 + 2828dcf commit 7e71066
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions product_variants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {

Expand Down

0 comments on commit 7e71066

Please sign in to comment.