/woocommerce_api

Primary LanguageRubyMIT LicenseMIT

Ruby Client for Woocommerce API

Build Status

Installation

gem "woocommerce_api", github: "tradegecko/woocommerce_api"
require "woocommerce_api"

Initialize authorize access

# Through https
WoocommerceAPI::Client.new(
  consumer_key: "ck_1234", 
  consumer_secret: "cs_1234", 
  store_url: "https://example.com")

# Through https but sending consumer_key and consumer_secret as query string
WoocommerceAPI::Client.new(
  consumer_key: "ck_1234", 
  consumer_secret: "cs_1234", 
  store_url: "https://example.com", 
  mode: :query_https)

# Through http (Oauth) 
WoocommerceAPI::Client.new(
  consumer_key: "ck_1234", 
  consumer_secret: "cs_1234", 
  store_url: "https://example.com", 
  mode: :oauth_http)

Calling API

Collection endpoints

returns a collection of the resource, size of collection limited by a configuration in Woocommerce setting (10 by default)

WoocommerceAPI::Product.all
=> [collection of products]

collection with parameters, check more http://woothemes.github.io/woocommerce-rest-api-docs/#parameters

WoocommerceAPI::Product.all(filter: { limit: 50 })
WoocommerceAPI::Order.all(filter: { status: "complete", created_at_min: "2015-11-01" })

Count

WoocommerceAPI::Product.count
WoocommerceAPI::Order.count(filter: { status: "commplete" })

Finding a resource

WoocommerceAPI::Product.find(1) 
WoocommerceAPI::Order.find(1)

Update a resource

product = WoocommerceAPI::Product.find(1) 
product.update_attributes(regular_price: 100, managing_stock: true)
# or
product.regular_price = 100
product.managing_stock = true
product.save

Destroy a resource

product.destroy # Soft delete a product (move to trash)
product.destroy(force: true) # Permanently delete a product

Debugging

We are using HTTParty, then...

WoocommerceAPI::OauthClient.debug_output $stdout
WoocommerceAPI::Client.debug_output $stdout