Skip to content

Support for taxes endpoint #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

**version 3.0 - 2015-08-11**
- Fix v3 authentication
- Implemented bulk

-- mikylucky's fork start

**version 2.0.1 - 2015-07-13**
- Fix composer configuration

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ A PHP wrapper for the WooCommerce REST API. Easily interact with the WooCommerce

Feedback and bug reports are appreciated.

NOTE: This is a fork of kloon's repository adapted to work with v3 WooCommerce API and include the bulk product update/creation

## Requirements

PHP 5.2.x
Expand Down Expand Up @@ -72,6 +74,10 @@ Exceptions are thrown when errors are encountered, most will be instances of `WC
* `$client->orders->get( null, array( 'status' => 'completed' ) )` - get a list of completed orders
* `$client->orders->get( $order_id )` - get a single order

## Bulk edit/create

* `$client->bulk->send( $products_array )` - bulk edit/create maximum 100 products each call. Be sure to put the product ID in the array if you want to update that product. Missing ID will create a new product


## Credit

Expand Down
4 changes: 4 additions & 0 deletions example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

$client = new WC_API_Client( 'http://your-store-url.com', 'ck_enter_your_consumer_key', 'cs_enter_your_consumer_secret', $options );

// bulk
//print_r( $client->bulk->send( $products_array ) );

// coupons
//print_r( $client->coupons->get() );
//print_r( $client->coupons->get( $coupon_id ) );
Expand Down Expand Up @@ -74,6 +77,7 @@
//print_r( $client->products->get_count( array( 'type' => 'simple' ) ) );
//print_r( $client->products->get_categories() );
//print_r( $client->products->get_categories( $category_id ) );
//print_r( $client->products->create_categroy( array( 'product_category' => array( 'name' => 'Test Category' ) ) ) );

// reports
//print_r( $client->reports->get() );
Expand Down
3 changes: 2 additions & 1 deletion lib/woocommerce-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* WooCommerce API Client Class
*
* @version 2.0.0
* @version 3.1.0
* @license GPL 3 or later http://www.gnu.org/licenses/gpl.html
*/

Expand All @@ -21,6 +21,7 @@

// resources
require_once( $dir . '/resources/abstract-wc-api-client-resource.php' );
require_once( $dir . '/resources/class-wc-api-client-resource-bulk.php' );
require_once( $dir . '/resources/class-wc-api-client-resource-coupons.php' );
require_once( $dir . '/resources/class-wc-api-client-resource-custom.php' );
require_once( $dir . '/resources/class-wc-api-client-resource-customers.php' );
Expand Down
9 changes: 6 additions & 3 deletions lib/woocommerce-api/class-wc-api-client-authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ public function generate_oauth_signature( $params, $http_method ) {

// form string to sign (first key)
$string_to_sign = $http_method . '&' . $base_request_uri . '&' . $query_string;

return base64_encode( hash_hmac( self::HASH_ALGORITHM, $string_to_sign, $this->consumer_secret, true ) );

// append '&' to consumer_secret to be compliant with v3 API
$secret = $this->consumer_secret . '&';

return base64_encode( hash_hmac( self::HASH_ALGORITHM, $string_to_sign, $secret, true ) );
}


Expand Down Expand Up @@ -181,4 +184,4 @@ public function get_consumer_secret() {
}


}
}
9 changes: 6 additions & 3 deletions lib/woocommerce-api/class-wc-api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class WC_API_Client {


/** API client version */
const VERSION = '2.0.1';
const VERSION = '3.0.0';

/** @var string store URL, e.g. http://www.woothemes.com */
public $store_url;
Expand Down Expand Up @@ -68,6 +68,9 @@ class WC_API_Client {

/** @var WC_API_Client_Resource_Webhooks instance */
public $webhooks;

/** @var WC_API_Client_Resource_Bulk instance */
public $bulk;


/**
Expand Down Expand Up @@ -118,6 +121,7 @@ public function __construct( $store_url, $consumer_key, $consumer_secret, $optio
public function init_resources() {

$resources = array(
'WC_API_Client_Resource_Bulk' => 'bulk',
'WC_API_Client_Resource_Coupons' => 'coupons',
'WC_API_Client_Resource_Custom' => 'custom',
'WC_API_Client_Resource_Customers' => 'customers',
Expand Down Expand Up @@ -161,7 +165,7 @@ public function build_api_url() {
$path = isset( $url['path'] ) ? rtrim( $url['path'], '/' ) : '';

// add WC API path
$path .= '/wc-api/v2/';
$path .= '/wc-api/v3/';

// build URL
$this->api_url = "{$scheme}://{$host}{$path}";
Expand Down Expand Up @@ -280,7 +284,6 @@ public function make_api_call( $method, $path, $request_data ) {
'debug' => $this->debug,
)
);

$request = new WC_API_Client_HTTP_Request( $args );

return $request->dispatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public function __construct( $endpoint, $object_namespace, $client ) {
* @param array $args
*/
protected function set_request_args( $args ) {

$this->request_method = $args['method'];
$this->request_path = isset( $args['path'] ) ? $args['path'] : null;
$this->request_params = isset( $args['params'] ) ? $args['params'] : null;
Expand All @@ -70,7 +69,8 @@ protected function set_request_args( $args ) {
// a convenience for client code so creating/updating resources doesn't need a
// nested array like array( 'order_note' => array( 'note' => 'foo' ) ) and can instead
// use array( 'note' => 'foo' ) ʘ‿ʘ
if ( $this->request_body && ! isset( $args['body'][ $this->object_namespace ] ) ) {
// NOTE: not working for create_category()
if ( $this->request_body && ( ! isset( $args['body'][ $this->object_namespace ] ) && ! isset( $args['body']['product_category'] ) ) ) {

$this->request_body = array(
$this->object_namespace => $this->request_body,
Expand Down Expand Up @@ -118,7 +118,6 @@ protected function get_request_data() {
* @return array|object
*/
protected function do_request() {

return $this->client->make_api_call( $this->request_method, $this->get_endpoint_path(), $this->get_request_data() );
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* WC API Client Bulk resource class
*
* @since 3.0
*/
class WC_API_Client_Resource_Bulk extends WC_API_Client_Resource {


/**
* Setup the resource
*
* @since 3.0
* @param WC_API_Client $client class instance
*/
public function __construct( $client ) {

parent::__construct( 'products', 'products', $client );
}


/**
* Bulk update/create products
*
* POST /products/bulk
*
* @since 3.0
* @param array $data valid products data
* @return array|object your newly-created product
*/
public function send( $data ) {

$this->set_request_args( array(
'method' => 'PUT',
'body' => $data,
'path' => 'bulk',
) );

return $this->do_request();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,18 @@ public function get( $id = null, $args = array() ) {
/**
* Get product by SKU
*
* GET /products/sku/{sku}
* GET /products?filter[sku]=$sku
*
* Note this will throw an exception if no products are found (404 not found)
*
* @since 2.0
* @since 3.0
* @param string $sku product SKU
* @param array $args acceptable product SKU lookup endpoint args, currently only `fields`
* @return array|object product!
*/
public function get_by_sku( $sku, $args = array() ) {
public function get_by_sku( $sku ) {

$this->set_request_args( array(
'method' => 'GET',
'path' => array( 'sku', urlencode( $sku ) ),
'params' => $args,
'params' => array( "filter[sku]" => $sku )
) );

return $this->do_request();
Expand Down Expand Up @@ -194,6 +191,25 @@ public function get_categories( $id = null, $args = array() ) {

return $this->do_request();
}

/**
* Create a new category
*
* POST /products/categories
*
* @since 3.1
* @param array with fields of a single category
* @return array|object product category! (NO)
*/
public function create_category( $args = array() ) {
$this->set_request_args( array(
'method' => 'POST',
'path' => 'categories',
'body' => $args,
) );

return $this->do_request();
}


/** Convenience methods - these do not map directly to an endpoint ********/
Expand All @@ -219,5 +235,4 @@ public function update_stock( $id, $quantity ) {
return $this->do_request();
}


}
}