woocommerce

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2021 License: MIT Imports: 8 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Billing

type Billing struct {
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
	Company   string `json:"company"`
	Address1  string `json:"address_1"`
	Address2  string `json:"address_2"`
	City      string `json:"city"`
	State     string `json:"state"`
	Postcode  string `json:"postcode"`
	Country   string `json:"country"`
	Email     string `json:"email"`
	Phone     string `json:"phone"`
}

Billing data

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the main SDK entry point for all methods. It wraps all available SDK objects into a single access point.

func NewClient

func NewClient(cc ClientConfig) Client

NewClient constructs a new woocommerce.Client object and sets the given configuration.

func (Client) Customers

func (c Client) Customers() Customers

Customers returns a Customers API client

func (Client) Index

func (c Client) Index() (IndexResponse, error)

Index method returns all available API endpoints for the current host.

func (Client) Orders

func (c Client) Orders() Orders

Orders returns a Orders API client

func (Client) Request

func (c Client) Request(method, endpoint string, params url.Values, response interface{}) error

Request performs a request and unmarshals JSON response into given response object.

type ClientConfig

type ClientConfig struct {
	APIHost        string // Complete REST API base URL: "https://example.com/wp-json/wc/v3"
	ConsumerKey    string
	ConsumerSecret string
	UseBasicAuth   bool
	Debug          bool
}

ClientConfig is used to instantiate a new Woocommerce API Client with the defined values.

func (ClientConfig) GetAPIEndpoint

func (cc ClientConfig) GetAPIEndpoint(method string) string

GetAPIEndpoint takes an API method name and returns the full URL for that endpoint based on the current configuration.

type Customer

type Customer struct {
	ID               int        `json:"id"`
	DateCreated      string     `json:"date_created"`
	DateCreatedGmt   string     `json:"date_created_gmt"`
	DateModified     string     `json:"date_modified"`
	DateModifiedGmt  string     `json:"date_modified_gmt"`
	Email            string     `json:"email"`
	FirstName        string     `json:"first_name"`
	LastName         string     `json:"last_name"`
	Role             string     `json:"role"`
	Username         string     `json:"username"`
	Billing          Billing    `json:"billing"`
	Shipping         Shipping   `json:"shipping"`
	IsPayingCustomer bool       `json:"is_paying_customer"`
	AvatarURL        string     `json:"avatar_url"`
	MetaData         []MetaData `json:"meta_data"`
	Links            Links      `json:"_links"`
}

Customer data

type Customers

type Customers struct {
	Client
}

Customers API client

func (Customers) Get

func (c Customers) Get(id int) (Customer, error)

Get customer by ID

func (Customers) List

func (c Customers) List(listParams url.Values) ([]Customer, error)

List all customers

type ErrorData

type ErrorData struct {
	Status int
}

ErrorData is used to store extra data from error responses.

type ErrorResponse

type ErrorResponse struct {
	Code    string
	Message string
	Data    ErrorData
}

ErrorResponse is the deafult error response data structure.

type IndexResponse

type IndexResponse struct {
	Namespace string
	Routes    map[string]interface{}
}

IndexResponse data

type Links struct {
	Self []struct {
		Href string `json:"href"`
	} `json:"self"`
	Collection []struct {
		Href string `json:"href"`
	} `json:"collection"`
}

Links data

type MetaData

type MetaData struct {
	ID    int         `json:"id"`
	Key   string      `json:"key"`
	Value interface{} `json:"value"`
}

MetaData object

type Order

type Order struct {
	ID                 int                 `json:"id"`
	ParentID           int                 `json:"parent_id"`
	Number             string              `json:"number"`
	OrderKey           string              `json:"order_key"`
	CreatedVia         string              `json:"created_via"`
	Version            string              `json:"version"`
	Status             string              `json:"status"`
	Currency           string              `json:"currency"`
	DateCreated        string              `json:"date_created"`
	DateCreatedGmt     string              `json:"date_created_gmt"`
	DateModified       string              `json:"date_modified"`
	DateModifiedGmt    string              `json:"date_modified_gmt"`
	DiscountTotal      string              `json:"discount_total"`
	DiscountTax        string              `json:"discount_tax"`
	ShippingTotal      string              `json:"shipping_total"`
	ShippingTax        string              `json:"shipping_tax"`
	CartTax            string              `json:"cart_tax"`
	Total              string              `json:"total"`
	TotalTax           string              `json:"total_tax"`
	PricesIncludeTax   bool                `json:"prices_include_tax"`
	CustomerID         int                 `json:"customer_id"`
	CustomerIPAddress  string              `json:"customer_ip_address"`
	CustomerUserAgent  string              `json:"customer_user_agent"`
	CustomerNote       string              `json:"customer_note"`
	Billing            Billing             `json:"billing"`
	Shipping           Shipping            `json:"shipping"`
	PaymentMethod      string              `json:"payment_method"`
	PaymentMethodTitle string              `json:"payment_method_title"`
	TransactionID      string              `json:"transaction_id"`
	DatePaid           string              `json:"date_paid"`
	DatePaidGmt        string              `json:"date_paid_gmt"`
	DateCompleted      string              `json:"date_completed"`
	DateCompletedGmt   string              `json:"date_completed_gmt"`
	CartHash           string              `json:"cart_hash"`
	MetaData           []MetaData          `json:"meta_data"`
	LineItems          []OrderLineItem     `json:"line_items"`
	TaxLines           []OrderTaxLine      `json:"tax_lines"`
	ShippingLines      []OrderShippingLine `json:"shipping_lines"`
	FeeLines           []OrderFeeLine      `json:"fee_lines"`
	CouponLines        []OrderCouponLine   `json:"coupon_lines"`
	Refunds            []OrderRefundLine   `json:"refunds"`
	Links              Links               `json:"_links"`
}

Order data

type OrderCouponLine

type OrderCouponLine struct {
	ID          int        `json:"id"`
	Code        string     `json:"code"`
	Discount    string     `json:"discount"`
	DiscountTax string     `json:"discount_tax"`
	MetaData    []MetaData `json:"meta_data"`
}

OrderCouponLine data

type OrderFeeLine

type OrderFeeLine struct {
	ID        int            `json:"id"`
	Name      string         `json:"name"`
	TaxClass  string         `json:"tax_class"`
	TaxStatus string         `json:"tax_status"`
	Total     string         `json:"total"`
	TotalTax  string         `json:"total_tax"`
	Taxes     []OrderTaxLine `json:"taxes"`
	MetaData  []MetaData     `json:"meta_data"`
}

OrderFeeLine data

type OrderLineItem

type OrderLineItem struct {
	ID          int                `json:"id"`
	Name        string             `json:"name"`
	ProductID   int                `json:"product_id"`
	VariationID int                `json:"variation_id"`
	Quantity    int                `json:"quantity"`
	TaxClass    string             `json:"tax_class"`
	Subtotal    string             `json:"subtotal"`
	SubtotalTax string             `json:"subtotal_tax"`
	Total       string             `json:"total"`
	TotalTax    string             `json:"total_tax"`
	Taxes       []OrderLineItemTax `json:"taxes"`
	MetaData    []MetaData         `json:"meta_data"`
	Sku         string             `json:"sku"`
	Price       int                `json:"price"`
}

OrderLineItem data

type OrderLineItemTax

type OrderLineItemTax struct {
	ID       int    `json:"id"`
	Total    string `json:"total"`
	Subtotal string `json:"subtotal"`
}

OrderLineItemTax data

type OrderNote

type OrderNote struct {
	ID             int    `json:"id"`
	Author         string `json:"author"`
	DateCreated    string `json:"date_created"`
	DateCreatedGmt string `json:"date_created_gmt"`
	Note           string `json:"note"`
	CustomerNote   bool   `json:"customer_note"`
	Links          Links  `json:"_links"`
}

OrderNote data

type OrderRefundLine

type OrderRefundLine struct {
	ID     int    `json:"id"`
	Reason string `json:"reason"`
	Total  string `json:"total"`
}

OrderRefundLine data

type OrderShippingLine

type OrderShippingLine struct {
	ID          int            `json:"id"`
	MethodTitle string         `json:"method_title"`
	MethodID    string         `json:"method_id"`
	Total       string         `json:"total"`
	TotalTax    string         `json:"total_tax"`
	Taxes       []OrderTaxLine `json:"taxes"`
	MetaData    []MetaData     `json:"meta_data"`
}

OrderShippingLine data

type OrderTaxLine

type OrderTaxLine struct {
	ID               int        `json:"id"`
	RateCode         string     `json:"rate_code"`
	RateID           int        `json:"rate_id"`
	Label            string     `json:"label"`
	Compound         bool       `json:"compound"`
	TaxTotal         string     `json:"tax_total"`
	ShippingTaxTotal string     `json:"shipping_tax_total"`
	MetaData         []MetaData `json:"meta_data"`
}

OrderTaxLine data

type Orders

type Orders struct {
	Client
}

Orders API client

func (Orders) Get

func (o Orders) Get(id int) (Order, error)

Get order by ID

func (Orders) GetOrderNote added in v0.1.1

func (o Orders) GetOrderNote(orderID, noteID int) (OrderNote, error)

GetOrderNote by IDs

func (Orders) List

func (o Orders) List(listParams url.Values) ([]Order, error)

List all orders

func (Orders) ListOrderNotes

func (o Orders) ListOrderNotes(id int) ([]OrderNote, error)

ListOrderNotes retrieves all notes for a given order

type Shipping

type Shipping struct {
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
	Company   string `json:"company"`
	Address1  string `json:"address_1"`
	Address2  string `json:"address_2"`
	City      string `json:"city"`
	State     string `json:"state"`
	Postcode  string `json:"postcode"`
	Country   string `json:"country"`
}

Shipping data

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL