Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1902,3 +1902,69 @@ GetWebhookPayload returns a WebhookPayload object and the raw payload from the
BigCommerce API Arguments: r - the http.Request object Returns: *WebhookPayload
- the WebhookPayload object []byte - the raw payload from the BigCommerce API
error - the error, if any

#### type Adjustment
```go
type Adjustment struct {
Reason string `json:"reason"`
Items []AdjustmentItem `json:"items"`
}
```

#### type AdjustmentItem
```go
type AdjustmentItem struct {
LocationId int `json:"location_id"`
VariantId int `json:"variant_id,omitempty"`
Quantity int `json:"quantity"`
Sku string `json:"sku,omitempty"`
ProductId int `json:"product_id,omitempty"`
}
```

#### func AdjustInventoryRelative
```go
func (bc *Client) AdjustInventoryRelative(adjustment *Adjustment) error
```
Adjusts the inventory relatively to it's current value. -1 will subtract one, and 1 will add one to the stock.

#### func AdjustInventoryAbsolute
```go
func (bc *Client) AdjustInventoryAbsolute(adjustment *Adjustment) error
```
Set the inventory's stock value absolutely to the value given. -1 will set the value to -1, 1 will set it to 1.

#### type Location
```go
type Location struct {
Code string `json:"code"`
Label string `json:"label"`
Description string `json:"description"`
ManagedByExternalSource bool `json:"managed_by_external_source"`
TypeId string `json:"type_id"`
Enabled bool `json:"enabled"`
OperatingHours LocationOpeningHours `json:"operating_hours"`
TimeZone string `json:"time_zone"`
Address LocationAddress `json:"address"`
StorefrontVisibility bool `json:"storefront_visibility"`
SpecialHours []LocationSpecialHours `json:"special_hours"`
}
```

#### func GetLocations
```go
func (bc *Client) GetLocations(filters map[string]string) ([]Location, error)
```
Gets all the locations that belong to a shop

#### func CreateLocation
```go
func (bc *Client) CreateLocation(location *Location) error
```
Create a new location

#### func UpdateLocation
```go
func (bc *Client) UpdateLocation(location *Location) error
```
Updates the locations values
48 changes: 48 additions & 0 deletions adjustments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package bigcommerce

import (
"bytes"
"encoding/json"
"net/http"
)

type Adjustment struct {
Reason string `json:"reason"`
Items []AdjustmentItem `json:"items"`
}

type AdjustmentItem struct {
LocationId int `json:"location_id"`
VariantId int `json:"variant_id,omitempty"`
Quantity int `json:"quantity"`
Sku string `json:"sku,omitempty"`
ProductId int `json:"product_id,omitempty"`
}

// AdjustInventoryRelative changes the stock value relative to it's current value
func (bc *Client) AdjustInventoryRelative(adjustment *Adjustment) error {
url := "/v3/inventory/adjustments/relative"

reqJSON, _ := json.Marshal(adjustment)
req := bc.getAPIRequest(http.MethodPost, url, bytes.NewReader(reqJSON))
_, err := bc.HTTPClient.Do(req)
if err != nil {
return err
}

return nil
}

// AdjustInventoryAbsolute sets the stock value to a specific value
func (bc *Client) AdjustInventoryAbsolute(adjustment *Adjustment) error {
url := "/v3/inventory/adjustments/absolute"

reqJSON, _ := json.Marshal(adjustment)
req := bc.getAPIRequest(http.MethodPost, url, bytes.NewReader(reqJSON))
_, err := bc.HTTPClient.Do(req)
if err != nil {
return err
}

return nil
}
134 changes: 134 additions & 0 deletions locations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package bigcommerce

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)

type Location struct {
Code string `json:"code"`
Label string `json:"label"`
Description string `json:"description"`
ManagedByExternalSource bool `json:"managed_by_external_source"`
TypeId string `json:"type_id"`
Enabled bool `json:"enabled"`
OperatingHours LocationOpeningHours `json:"operating_hours"`
TimeZone string `json:"time_zone"`
Address LocationAddress `json:"address"`
StorefrontVisibility bool `json:"storefront_visibility"`
SpecialHours []LocationSpecialHours `json:"special_hours,omitempty"`
}

type LocationAddress struct {
Address1 string `json:"address1"`
Address2 string `json:"address2"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Email string `json:"email"`
Phone string `json:"phone"`
GeoCoordinates Coordenates `json:"geo_coordinates"`
CountryCode string `json:"country_code"`
}

type Coordenates struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}

type LocationOpeningHours struct {
Sunday Weekday `json:"sunday"`
Monday Weekday `json:"monday"`
Tuesday Weekday `json:"tuesday"`
Wednesday Weekday `json:"wednesday"`
Thursday Weekday `json:"thursday"`
Friday Weekday `json:"friday"`
Saturday Weekday `json:"saturday"`
}

type LocationSpecialHours struct {
Label string `json:"label"`
Date string `json:"date"`
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
AllDay bool `json:"all_day"`
Annual bool `json:"annual"`
}

type Weekday struct {
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
}

// GetLocations returns all locations using filters.
// filters: request query parameters for BigCommerce locations endpoint, for example {"is_active": true}
func (bc *Client) GetLocations(filters map[string]string) ([]Location, error) {
var params []string
for k, v := range filters {
params = append(params, fmt.Sprintf("%s=%s", k, v))
}
url := "/v3/inventory/locations?" + strings.Join(params, "&")

req := bc.getAPIRequest(http.MethodGet, url, nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return nil, err
}

defer res.Body.Close()
body, err := processBody(res)
if err != nil {
if res.StatusCode == http.StatusNoContent {
return []Location{}, nil
}
return nil, err
}

var locations []Location
err = json.Unmarshal(body, &locations)
print(string(body))
if err != nil {
return nil, err
}
return locations, nil
}

// CreateLocations creates a new location based on the Location struct
func (bc *Client) CreateLocations(location *[]Location) error {
url := "/v3/inventory/locations"

reqJSON, _ := json.Marshal(location)

fmt.Println(string(reqJSON))
req := bc.getAPIRequest(http.MethodPost, url, bytes.NewReader(reqJSON))
res, err := bc.HTTPClient.Do(req)
if err != nil {
return err
}

if res.StatusCode != 200 {
return errors.New(res.Status)
}

return nil
}

// UpdateLocation alters the locations values
func (bc *Client) UpdateLocation(location *Location) error {
url := "/v3/inventory/locations"

reqJSON, _ := json.Marshal(location)
req := bc.getAPIRequest(http.MethodPut, url, bytes.NewReader(reqJSON))
_, err := bc.HTTPClient.Do(req)
if err != nil {
return err
}

return nil
}