Skip to content

leonelquinteros/woocommerce

Repository files navigation

GitHub release MIT license GoDoc Go Report Card

Woocommerce SDK

Woocommerce REST API Client for Go

Quick start

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)
    }
}

Available endpoints

Customers

client.Customers().List(params)
client.Customers().Get(customerID)

Orders

client.Orders().List(params)
client.Orders().Get(orderID)
client.Orders().ListOrderNotes(orderID)

Tests

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.