gowoocommerce

package module
v0.0.0-...-67c8ae3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2019 License: GPL-3.0 Imports: 12 Imported by: 0

README

gowoocommerce

Go package to interface with the WooCommerce Product API. Based on a closed source project for stillgrove. Currently only supports Creating, Reading, Updateing, Deleting Products. Other features like manipulating Category Trees or Attributes are missing: Happy about every contribution :)

Installing

go get "github.com/michael-stiller/gowoocommerce"

Examples:

Initialize
import gwc "github.com/michael-stiller/gowoocommerce"

var w gwc.WooConnection

domain := "www.domain.com"
key := "yourkey"
secret := "yoursecret"

err := w.Init(domain, key, secret)
if err != nil {
    panic(err)
}

Query existing products:
products, _ := w.GetAllProducts()
fmt.Println(products)
Query Existing Categories:

https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-product-categories

// query parameters can be appended via query string
cats, _ := w.QueryCategories("")
Batch Create/Update/Delete products
// Define a new product
var newProduct = gwc.WooProduct{
    Name:             "test_product",
    ShortDescription: "Very good product",
}

// Append Product to the Create/Update/Delete array in the request struct
var req = gwc.WooBatchRequest{
    Endpoint: "/wp-json/wc/v3/products/batch",
    Create:   []gwc.WooProduct{newProduct},
}

// Push request to queue
w.PushToQueue(req)

// Execute queue
// (returns an array of raw json []byte in case you want to further use the response object)
_, err = w.ExecuteRequestQueue()
if err != nil {
    panic(err)
}

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the GNUgpl3 License - see the LICENSE.md file for details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WooAttribute

type WooAttribute struct {
	ID      int32    `json:"id,omitempty"`
	Name    string   `json:"name,omitempty"`
	Option  string   `json:"option,omitempty"`  // "term"
	Options []string `json:"options,omitempty"` // "terms"
	Slug    string   `json:"slug,omitempty"`
	Visible bool     `json:"visible,omitempty"`
	Type    string   `json:"type,omitempty"` // "select" by default
}

WooAttribute provides additional general fields for the products

func (WooAttribute) GetID

func (a WooAttribute) GetID() int32

GetID implements WooItem

type WooBatchPostRequest

type WooBatchPostRequest struct {
	Endpoint string    `json:"-"`
	Create   []WooItem `json:"create,omitempty"` // Create requests must not have IDs -the WC backend will generate them
	Update   []WooItem `json:"update,omitempty"` // Update requests must have IDs
	Delete   []int     `json:"delete,omitempty"` // Delete requests can only be IDs
}

WooBatchPostRequest sends a payload of batch creations, updates and/or deletions

func (WooBatchPostRequest) Send

func (b WooBatchPostRequest) Send(w *WooConnection) ([]byte, error)

Send implements the WooRequest Interface

type WooCategory

type WooCategory struct {
	ID          int32            `json:"id,omitempty"`
	Name        string           `json:"name"`
	Alt         string           `json:"alt,omitempty"`
	Slug        string           `json:"slug,omitempty"`
	Parent      int32            `json:"parent,omitempty"`
	Description string           `json:"description,omitempty"`
	Image       WooImage         `json:"image,omitempty"`
	MenuOrder   int32            `json:"menu_order,omitempty"`
	Count       int32            `json:"count,omitempty"`
	Links       WooCategoryLinks `json:"_links,omitempty"` // read-only
}

WooCategory convers objects relating to the WC Category tree

func (WooCategory) GetID

func (c WooCategory) GetID() int32

GetID implements WooItem

type WooCategoryLink struct {
	Href string `json:"href,omitempty"`
}

WooCategoryLink can be either self, collection, or up e.g.: "https://example.com/wp-json/wc/v3/products/categories/15"

type WooCategoryLinks struct {
	Self       []WooCategoryLink `json:"self,omitempty"`
	Collection []WooCategoryLink `json:"collection,omitempty"`
	Up         []WooCategoryLink `json:"up,omitempty"`
}

WooCategoryLinks are generated from the category ids: e.g.: "href": "https://example.com/wp-json/wc/v3/products/categories/15"

type WooConnection

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

WooConnection interfaces with the WooCommerce backend

func (*WooConnection) ExecuteRequestQueue

func (w *WooConnection) ExecuteRequestQueue(strict, verbose bool) ([][]byte, error)

ExecuteRequestQueue executes all the request that were pushed before and returns an array of the raw responses as bytes if strict: returns on any error; else: finishes regardless of errors

func (*WooConnection) GetAllProducts

func (w *WooConnection) GetAllProducts(verbose bool) ([]WooProduct, error)

GetAllProducts returns all products from the WC backend

func (*WooConnection) Init

func (w *WooConnection) Init(domain, key, secret string, productsPerBatch, maxConcurrentRequests, maxRetries int) error

Init takes in the credentials before dong any other operation

func (*WooConnection) PurgeProducts

func (w *WooConnection) PurgeProducts(verbose bool) error

PurgeProducts deletes all the products from the woo commerce backend Remember: Does not remove the image assets from the server!

func (*WooConnection) PushToQueue

func (w *WooConnection) PushToQueue(r WooRequest)

PushToQueue appends a WooREquest to the queue to later be executed

func (*WooConnection) QueryCategories

func (w *WooConnection) QueryCategories(searchString string) ([]WooCategory, error)

QueryCategories returns all categories from the WC backend https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-product-categories

func (*WooConnection) Request

func (w *WooConnection) Request(method, endpoint string, body []byte) ([]byte, error)

Request sends a request: ("GET", "POST"), endpoint, body

func (*WooConnection) ViewRequestQueue

func (w *WooConnection) ViewRequestQueue() ([][]byte, error)

ViewRequestQueue returns the marshalled requests as they will be sent by ExecuteRequestQueue

type WooDimension

type WooDimension struct {
	Length string `json:"length,omitempty"`
	Width  string `json:"width,omitempty"`
	Height string `json:"height,omitempty"`
}

WooDimension stores length, width, and height

type WooGetRequest

type WooGetRequest struct {
	Endpoint string
}

WooGetRequest implements GET request via a WooConnection

func (WooGetRequest) Send

func (g WooGetRequest) Send(w *WooConnection) ([]byte, error)

Send implementes the WooRequest interface

type WooImage

type WooImage struct {
	ID int32 `json:"id,omitempty"`
	//DateCreated     string `json:"date_created,omitempty"`
	DateCreatedGMT string `json:"date_created_gmt,omitempty"`
	//DateModified    string `json:"date_modified,omitempty"`
	DateModifiedGMT string `json:"date_modified_gmt,omitempty"`
	SRC             string `json:"src,omitempty"`
	Name            string `json:"name,omitempty"`
	Alt             string `json:"alt,omitempty"`
}

WooImage contains all the information on product images

type WooItem

type WooItem interface {
	GetID() int32
}

WooItem is the object used to hold requests and responses towards the woocommerce api Examples: WooProduct, WooAttribute, WooCategory

type WooPostRequest

type WooPostRequest struct {
	Endpoint string
	Payload  WooItem
}

WooPostRequest can be used for synchronous requests to the products, attributes, or categories endpoint

func (WooPostRequest) Send

func (p WooPostRequest) Send(w *WooConnection) ([]byte, error)

Send implements the WooRequest interface

type WooProduct

type WooProduct struct {
	ID uint32 `json:"id,omitempty"` // read-only!!!
	//Key               uint32                   `json:"-"`
	SKU       string `json:"sku,omitempty"`
	Name      string `json:"name,omitempty"`
	Slug      string `json:"slug,omitempty"`
	Permalink string `json:"permalink,omitempty"` // read-only
	//DateCreated       string                   `json:"date_created,omitempty"`      // read-only
	DateCreatedGmt string `json:"date_created_gmt,omitempty"` // read-only
	//DateModified      string                   `json:"date_modified,omitempty"`     // read-only
	DateModifiedGmt   string `json:"date_modified_gmt,omitempty"` // read-only
	Type              string `json:"type,omitempty"`
	Status            string `json:"status,omitempty"`
	Featured          bool   `json:"featured,omitempty"`
	CatalogVisibility string `json:"catalog_visibility,omitempty"` // Options: visible, catalog, search and hidden. Default is visible.
	Description       string `json:"description,omitempty"`
	ShortDescription  string `json:"short_description,omitempty"`
	//Price             string                   `json:"price,omitempty"`         // read-only
	RegularPrice      string `json:"regular_price,omitempty"`
	SalePrice         string `json:"sale_price,omitempty"`
	DateOnSaleFrom    string `json:"date_on_sale_from,omitempty"`
	DateOnSaleFromGmt string `json:"date_on_sale_from_gmt,omitempty"`
	DateOnSaleTo      string `json:"date_on_sale_to,omitempty"`
	DateOnSaleToGmt   string `json:"date_on_sale_to_gmt,omitempty"`
	//PriceHTML         string                   `json:"price_html,omitempty"`   // read-only
	OnSale bool `json:"on_sale,omitempty"` // read-only
	//Purchasable       bool                     `json:"purchasable,omitempty"`  // read-only
	TotalSales        int32                    `json:"total_sales,omitempty"`  // read-only
	ExternalURL       string                   `json:"external_url,omitempty"` // real outlink
	ButtonText        string                   `json:"button_text,omitempty"`  // external shop to link to
	TaxStatus         string                   `json:"tax_status,omitempty"`   // Options: taxable, shipping and none. Default is taxable
	TaxClass          string                   `json:"tax_class,omitempty"`
	StockQuantity     int32                    `json:"stock_quantity,omitempty"`
	StockStatus       string                   `json:"stock_status,omitempty"`      // Options: instock, outofstock, onbackorder. Default is instock.
	SoldIndividually  bool                     `json:"sold_individually,omitempty"` // Allow one item to be bought in a single order. Default is false
	Weight            string                   `json:"weight,omitempty"`
	Dimensions        WooDimension             `json:"dimensions,omitempty"`
	ShippingRequired  bool                     `json:"shipping_required,omitempty"` // read-only
	ReviewsAllowed    bool                     `json:"reviews_allowed,omitempty"`   // default: true
	AverageRating     string                   `json:"average_rating,omitempty"`    // read-only
	RatingCount       int32                    `json:"rating_count,omitempty"`      // read-only
	RelatedIds        []int32                  `json:"related_ids,omitempty"`       // read_only
	UpsellIds         []int32                  `json:"upsell_ids,omitempty"`
	CrossSellIds      []int32                  `json:"cross_sell_ids,omitempty"`
	ParentID          int32                    `json:"parent_id,omitempty"`
	Categories        []WooCategory            `json:"categories,omitempty"`
	Tags              []WooTag                 `json:"tags,omitempty"`
	Images            []WooImage               `json:"images,omitempty"`
	DefaultAttributes []map[string]interface{} `json:"default_attributes,omitempty"`
	Variations        []string                 `json:"variations,omitempty"`
	GroupedProducts   []int32                  `json:"grouped_products,omitempty"`
	MenuOrder         int32                    `json:"menu_order,omitempty"`
	MetaData          []map[string]interface{} `json:"meta_data,omitempty"`
	Attributes        []WooAttribute           `json:"attributes,omitempty"`
	Brands            []interface{}            `json:"brands,omitempty"`
	Language          string                   `json:"language,omitempty"`
	Lang              string                   `json:"lang,omitempty"`          // relates to the woocommerce multilingual package; otherwise: omit!
	CustomPrices      map[string]WpmlPrice     `json:"custom_prices,omitempty"` // "custom_prices": {"EUR": {"regular_price": 100, "sale_price": 99}}
}

WooProduct is the struct through which you interface with the WooCommerce backend

func (*WooProduct) AddImage

func (p *WooProduct) AddImage(url string, name string, text string)

AddImage adds an image +name ( +text to display when said image not available)

func (WooProduct) GetID

func (p WooProduct) GetID() int32

GetID implements WooItem

type WooRequest

type WooRequest interface {
	Send(w *WooConnection) ([]byte, error)
}

WooRequest is implemented for Batch/Post and Get

type WooTag

type WooTag struct {
	ID   int32  `json:"id,omitempty"`
	Name string `json:"name,omitempty"` // read-only
	Slug string `json:"slug,omitempty"` // read-only
}

WooTag interacts witht underlying category tree

type WpmlPrice

type WpmlPrice struct {
	RegularPrice string `json:"regular_price,omitempty"`
	SalePrice    string `json:"sale_price,omitempty"`
}

WpmlPrice holds custom prices

Jump to

Keyboard shortcuts

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