Skip to content

Commit

Permalink
Merge pull request #43 from jjideenschmiede/development
Browse files Browse the repository at this point in the history
feat: Add product images request.
  • Loading branch information
gowizzard authored Feb 28, 2024
2 parents a740907 + 41490dd commit 610c83f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,35 @@ if err != nil {
}
```

## Create a product image

If you want to add an image to a product, you can do so using the following function. The ID of the product is required.

```go
// Define request
r := goshopify.Request{
StoreName: "",
AccessToken: "",
}

// Define body
body := goshopify.ProductImageBody{
Image: ProductImageBodyImage{
Position: 1,
Src: "https://cdn.jj-ideenschmiede.de/placeholder.png",
VariantIds: []int{12345678},
},
}

// Add new product image
productImage, err := goshopify.AddProductImage(6881118224568, body, r)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(productImage)
}
```

## Get a list of metafields for a product

If you want to read out all metafields of a product, you can do this as follows. You need the ID of the product.
Expand Down
72 changes: 72 additions & 0 deletions product_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2024 J&J Ideenschmiede GmbH. All rights reserved.

package goshopify

import (
"encoding/json"
"fmt"
"net/http"
"time"
)

// ProductImageBody is to structure the data
type ProductImageBody struct {
Image ProductImageBodyImage `json:"image"`
}

type ProductImageBodyImage struct {
Position int `json:"position,omitempty"`
Src string `json:"src"`
VariantIds []int `json:"variant_ids"`
}

// ProductImageReturn is to decode the json data
type ProductImageReturn struct {
Image struct {
Width int `json:"width"`
Height int `json:"height"`
Position int `json:"position"`
Alt string `json:"alt"`
Id int `json:"id"`
ProductId int `json:"product_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Src string `json:"src"`
VariantIds []int `json:"variant_ids"`
AdminGraphqlApiId string `json:"admin_graphql_api_id"`
} `json:"image"`
}

// AddProductImage is to add a new image to a product
func AddProductImage(id int, body ProductImageBody, r Request) (ProductImageReturn, error) {

// Convert body
convert, err := json.Marshal(body)
if err != nil {
return ProductImageReturn{}, err
}

// Set config for new request
c := Config{fmt.Sprintf("/products/%d/images.json", id), http.MethodPost, convert}

// Send request
response, err := c.Send(r)
if err != nil {
return ProductImageReturn{}, err
}

// Close request
defer response.Body.Close()

// Decode data
var decode ProductImageReturn

err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ProductImageReturn{}, err
}

// Return data
return decode, err

}
5 changes: 3 additions & 2 deletions products.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ type ProductBodyProduct struct {
}

type ProductBodyImages struct {
Src string `json:"src,omitempty"`
Alt string `json:"alt,omitempty"`
Position int `json:"position,omitempty"`
Src string `json:"src"`
Alt string `json:"alt,omitempty"`
}

type ProductBodyVariants struct {
Expand Down

0 comments on commit 610c83f

Please sign in to comment.