automattic/woocommerce

A PHP wrapper for the WooCommerce REST API

Installs: 3 925 028

Dependents: 32

Suggesters: 0

Security: 0

Stars: 528

Watchers: 163

Forks: 160

Open Issues: 56

3.1.0 2022-03-18 21:46 UTC

This package is auto-updated.

Last update: 2024-11-13 21:06:28 UTC


README

A PHP wrapper for the WooCommerce REST API. Easily interact with the WooCommerce REST API securely using this library. If using a HTTPS connection this library uses BasicAuth, else it uses Oauth to provide a secure connection to WooCommerce.

CI status Scrutinizer Code Quality PHP version

Installation

composer require automattic/woocommerce

Getting started

Generate API credentials (Consumer Key & Consumer Secret) following this instructions http://docs.woocommerce.com/document/woocommerce-rest-api/ .

Check out the WooCommerce API endpoints and data that can be manipulated in https://woocommerce.github.io/woocommerce-rest-api-docs/.

Setup

Setup for the new WP REST API integration (WooCommerce 2.6 or later):

require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

$woocommerce = new Client(
  'http://example.com',
  'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  [
    'version' => 'wc/v3',
  ]
);

Client class

$woocommerce = new Client($url, $consumer_key, $consumer_secret, $options);

Options

Client options

Client methods

GET

$woocommerce->get($endpoint, $parameters = []);

POST

$woocommerce->post($endpoint, $data);

PUT

$woocommerce->put($endpoint, $data);

DELETE

$woocommerce->delete($endpoint, $parameters = []);

OPTIONS

$woocommerce->options($endpoint);

Arguments

Response

All methods will return arrays on success or throwing HttpClientException errors on failure.

use Automattic\WooCommerce\HttpClient\HttpClientException;

try {
  // Array of response results.
  $results = $woocommerce->get('customers');
  // Example: ['customers' => [[ 'id' => 8, 'created_at' => '2015-05-06T17:43:51Z', 'email' => ...
  echo '<pre><code>' . print_r($results, true) . '</code><pre>'; // JSON output.

  // Last request data.
  $lastRequest = $woocommerce->http->getRequest();
  echo '<pre><code>' . print_r($lastRequest->getUrl(), true) . '</code><pre>'; // Requested URL (string).
  echo '<pre><code>' .
    print_r($lastRequest->getMethod(), true) .
    '</code><pre>'; // Request method (string).
  echo '<pre><code>' .
    print_r($lastRequest->getParameters(), true) .
    '</code><pre>'; // Request parameters (array).
  echo '<pre><code>' .
    print_r($lastRequest->getHeaders(), true) .
    '</code><pre>'; // Request headers (array).
  echo '<pre><code>' . print_r($lastRequest->getBody(), true) . '</code><pre>'; // Request body (JSON).

  // Last response data.
  $lastResponse = $woocommerce->http->getResponse();
  echo '<pre><code>' . print_r($lastResponse->getCode(), true) . '</code><pre>'; // Response code (int).
  echo '<pre><code>' .
    print_r($lastResponse->getHeaders(), true) .
    '</code><pre>'; // Response headers (array).
  echo '<pre><code>' . print_r($lastResponse->getBody(), true) . '</code><pre>'; // Response body (JSON).
} catch (HttpClientException $e) {
  echo '<pre><code>' . print_r($e->getMessage(), true) . '</code><pre>'; // Error message.
  echo '<pre><code>' . print_r($e->getRequest(), true) . '</code><pre>'; // Last request data.
  echo '<pre><code>' . print_r($e->getResponse(), true) . '</code><pre>'; // Last response data.
}

Release History

  • 2022-03-18 - 3.1.0 - Added new options to support _method and X-HTTP-Method-Override from WP, supports 7+, dropped support to PHP 5.
  • 2019-01-16 - 3.0.0 - Legacy API turned off by default, and improved JSON error handler.
  • 2018-03-29 - 2.0.1 - Fixed fatal errors on lookForErrors.
  • 2018-01-12 - 2.0.0 - Responses changes from arrays to stdClass objects. Added follow_redirects option.
  • 2017-06-06 - 1.3.0 - Remove BOM before decoding and added support for multi-dimensional arrays for oAuth1.0a.
  • 2017-03-15 - 1.2.0 - Added user_agent option.
  • 2016-12-14 - 1.1.4 - Fixed WordPress 4.7 compatibility.
  • 2016-10-26 - 1.1.3 - Allow set oauth_timestamp and improved how is handled the response headers.
  • 2016-09-30 - 1.1.2 - Added wp_api_prefix option to allow custom WP REST API URL prefix.
  • 2016-05-10 - 1.1.1 - Fixed oAuth and error handler for WP REST API.
  • 2016-05-09 - 1.1.0 - Added support for WP REST API, added method Automattic\WooCommerce\Client::options and fixed multiple headers responses.
  • 2016-01-25 - 1.0.2 - Fixed an error when getting data containing non-latin characters.
  • 2016-01-21 - 1.0.1 - Sort all oAuth parameters before build request URLs.
  • 2016-01-11 - 1.0.0 - Stable release.