Woocommerce REST API Client for Go
go get github.com/leonelquinteros/woocommerce
package main
import "github.com/leonelquinteros/woocommerce"
func main() {
// Grab config from environment variables
conf := woocommerce.ClientConfig{
APIHost: os.Getenv("WC_API_HOST"),
ConsumerKey: os.Getenv("WC_API_CONSUMER_KEY"),
ConsumerSecret: os.Getenv("WC_API_CONSUMER_SECRET"),
}
// Get Woocommerce client
client := woocommerce.NewClient(conf)
// List orders
params := url.Values{}
params.Set("orderby", "id")
params.Set("order", "desc")
orders, err := client.Orders().List(params)
if err != nil {
log.Fatal(err)
}
// Print results
for _, order := range orders {
log.Printf("%+v", order)
}
}
client.Customers().List(params)
client.Customers().Get(customerID)
client.Orders().List(params)
client.Orders().Get(orderID)
client.Orders().ListOrderNotes(orderID)
Most tests will depend on several environment variables to use as configuration for the Woocommerce client.
Check the helper function at env_test.go
:
func getTestClient() Client {
cc := ClientConfig{
APIHost: os.Getenv("WC_API_HOST"),
ConsumerKey: os.Getenv("WC_API_CONSUMER_KEY"),
ConsumerSecret: os.Getenv("WC_API_CONSUMER_SECRET"),
Debug: true,
}
return NewClient(cc)
}
You have to set WC_API_HOST
, WC_API_CONSUMER_KEY
and WC_API_CONSUMER_SECRET
to the corresponding configuration values for your environment.