From a50538dc594d4397548332323cde3ab260facd6d Mon Sep 17 00:00:00 2001 From: Michele Fortunato Date: Sun, 8 Nov 2015 09:03:38 +0100 Subject: [PATCH 1/3] v3 API adapted and bulk implemented --- README.md | 6 +++ example/example.php | 3 ++ lib/woocommerce-api.php | 3 +- .../class-wc-api-client-authentication.php | 9 ++-- lib/woocommerce-api/class-wc-api-client.php | 9 ++-- .../class-wc-api-client-resource-bulk.php | 43 +++++++++++++++++++ .../class-wc-api-client-resource-products.php | 14 +++--- 7 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 lib/woocommerce-api/resources/class-wc-api-client-resource-bulk.php diff --git a/README.md b/README.md index 87d0bba..331a7bd 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/example/example.php b/example/example.php index 3197bda..bb21bb7 100644 --- a/example/example.php +++ b/example/example.php @@ -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 ) ); diff --git a/lib/woocommerce-api.php b/lib/woocommerce-api.php index 86a0235..2948749 100644 --- a/lib/woocommerce-api.php +++ b/lib/woocommerce-api.php @@ -2,7 +2,7 @@ /** * WooCommerce API Client Class * - * @version 2.0.0 + * @version 3.0.0 * @license GPL 3 or later http://www.gnu.org/licenses/gpl.html */ @@ -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' ); diff --git a/lib/woocommerce-api/class-wc-api-client-authentication.php b/lib/woocommerce-api/class-wc-api-client-authentication.php index 1bffe54..f2d76c0 100644 --- a/lib/woocommerce-api/class-wc-api-client-authentication.php +++ b/lib/woocommerce-api/class-wc-api-client-authentication.php @@ -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 ) ); } @@ -181,4 +184,4 @@ public function get_consumer_secret() { } -} +} \ No newline at end of file diff --git a/lib/woocommerce-api/class-wc-api-client.php b/lib/woocommerce-api/class-wc-api-client.php index e27052a..c3b3b1d 100644 --- a/lib/woocommerce-api/class-wc-api-client.php +++ b/lib/woocommerce-api/class-wc-api-client.php @@ -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; @@ -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; /** @@ -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', @@ -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}"; @@ -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(); diff --git a/lib/woocommerce-api/resources/class-wc-api-client-resource-bulk.php b/lib/woocommerce-api/resources/class-wc-api-client-resource-bulk.php new file mode 100644 index 0000000..e18db25 --- /dev/null +++ b/lib/woocommerce-api/resources/class-wc-api-client-resource-bulk.php @@ -0,0 +1,43 @@ +set_request_args( array( + 'method' => 'PUT', + 'body' => $data, + 'path' => 'bulk', + ) ); + + return $this->do_request(); +} + + +} \ No newline at end of file diff --git a/lib/woocommerce-api/resources/class-wc-api-client-resource-products.php b/lib/woocommerce-api/resources/class-wc-api-client-resource-products.php index 47b2aee..76558d4 100644 --- a/lib/woocommerce-api/resources/class-wc-api-client-resource-products.php +++ b/lib/woocommerce-api/resources/class-wc-api-client-resource-products.php @@ -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(); @@ -219,5 +216,4 @@ public function update_stock( $id, $quantity ) { return $this->do_request(); } - -} +} \ No newline at end of file From 4615bc5cf555c5a4bb2dc58ade067ea5362c2db8 Mon Sep 17 00:00:00 2001 From: Michele Fortunato Date: Sun, 8 Nov 2015 09:13:20 +0100 Subject: [PATCH 2/3] hello 3.0 --- CHANGELOG | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 5b1f1d7..f66862b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 From 2e49370ff433ce64f8ae92a972c4bb57327a3b8b Mon Sep 17 00:00:00 2001 From: mikylucky Date: Wed, 16 Dec 2015 09:05:28 +0000 Subject: [PATCH 3/3] Added support to create categories - Supports POST v3/products/category (since API v3.1) --- example/example.php | 1 + lib/woocommerce-api.php | 2 +- .../abstract-wc-api-client-resource.php | 5 ++--- .../class-wc-api-client-resource-products.php | 19 +++++++++++++++++++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/example/example.php b/example/example.php index bb21bb7..2a9c058 100644 --- a/example/example.php +++ b/example/example.php @@ -77,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() ); diff --git a/lib/woocommerce-api.php b/lib/woocommerce-api.php index 2948749..1bcd18c 100644 --- a/lib/woocommerce-api.php +++ b/lib/woocommerce-api.php @@ -2,7 +2,7 @@ /** * WooCommerce API Client Class * - * @version 3.0.0 + * @version 3.1.0 * @license GPL 3 or later http://www.gnu.org/licenses/gpl.html */ diff --git a/lib/woocommerce-api/resources/abstract-wc-api-client-resource.php b/lib/woocommerce-api/resources/abstract-wc-api-client-resource.php index 7fbc17c..a9b169b 100644 --- a/lib/woocommerce-api/resources/abstract-wc-api-client-resource.php +++ b/lib/woocommerce-api/resources/abstract-wc-api-client-resource.php @@ -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; @@ -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, @@ -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() ); } diff --git a/lib/woocommerce-api/resources/class-wc-api-client-resource-products.php b/lib/woocommerce-api/resources/class-wc-api-client-resource-products.php index 76558d4..1ae5d50 100644 --- a/lib/woocommerce-api/resources/class-wc-api-client-resource-products.php +++ b/lib/woocommerce-api/resources/class-wc-api-client-resource-products.php @@ -191,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 ********/