PHPackages                             mac2000/woo-commerce-api-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mac2000/woo-commerce-api-client

ActiveLibrary[API Development](/categories/api)

mac2000/woo-commerce-api-client
===============================

API client for Wordpress WooCommerce plugin

1.2.1(10y ago)51.5k2[2 issues](https://github.com/mac2000/woo-commerce-api-client/issues)MITPHP

Since Apr 2Pushed 10y ago3 watchersCompare

[ Source](https://github.com/mac2000/woo-commerce-api-client)[ Packagist](https://packagist.org/packages/mac2000/woo-commerce-api-client)[ RSS](/packages/mac2000-woo-commerce-api-client/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (5)Used By (0)

WooCommerce API Client
======================

[](#woocommerce-api-client)

API client for Wordpress [WooCommerce](https://wordpress.org/plugins/woocommerce/) plugin.

It was created first of all form importing products into fresh installed store.

API documentation can be found here:

Installation
------------

[](#installation)

This client can be installed using Composer. Add the following to your composer.json:

```
{
    "require": {
        "mac2000/woo-commerce-api-client": "1.0.*"
    }
}
```

Usage Examples
--------------

[](#usage-examples)

Instantiation:

```
use Mac2000\WooCommerceApiClient\Client as Woo;
$client = new Woo('consumer_key', 'consumer_secret', 'http://acme.com/');
```

**Creating simple product**

```
print_r($client->post('products', [
    'json' => [
        'product' => [
            'title' => 'Simple T-Shirt',
            'type' => 'simple',
            'regular_price' => 9.99,
            'description' => 'T-Shirt description goes here',
            'short_description' => 'short description',
            'categories' => ['Wear', 'T-Shirts'],
            'images' => [
                ['src' => 'http://placehold.it/800x600', 'position' => 0]
            ],
            'attributes' => [
                //Important: for predefined attributes slug is required, and options should contain slugs rather than names
                ['name' => 'Brand', 'slug' => 'brand', 'options' => ['nike']],
                ['name' => 'Size', 'slug' => 'size', 'options' => ['M']],
                ['name' => 'Color', 'options' => ['White']]
            ]
        ]
    ]
])->json());
```

Notice that if you want product to use predefined attributes you should provide attribute slug.

**Create variable product**

```
print_r($client->post('products', [
    'json' => [
        'product' => [
            'title' => 'Variable T-Shirt',
            'type' => 'variable',
            'regular_price' => 9.99,
            'description' => 'T-Shirt description goes here',
            'short_description' => 'short description',
            'categories' => ['Wear', 'T-Shirts'],
            'images' => [
                ['src' => 'http://placehold.it/800x600', 'position' => 0]
            ],
            'attributes' => [
                ['name' => 'Brand', 'slug' => 'brand', 'options' => ['Nike']],
                ['name' => 'Size', 'slug' => 'size', 'options' => ['S','M','L'], 'variation' => true], //Notice: All options that will be used in variations should be here
                ['name' => 'Color', 'options' => ['White', 'Black']]
            ],
            'variations' => [
                [
                    'regular_price' => 8.99,
                    'attributes' => [['name' => 'Size', 'slug' => 'size', 'option' => 'S']]
                ],
                [
                    'regular_price' => 9.99,
                    'attributes' => [['name' => 'Size', 'slug' => 'size', 'option' => 'M']]
                ],
                [
                    'regular_price' => 10.99,
                    'attributes' => [['name' => 'Size', 'slug' => 'size', 'option' => 'L']]
                ]
            ]
        ]
    ]
])->json());
```

**Retrieve products**

```
print_r($client->get('products')->json());
```

**Retrie all product ids**

```
$page = 0;
$product_ids = [];

do {
    $page++;

    $response = $client->get('products', ['query' => [
        'fields' => 'id',
        'page' => $page,
        'filter' => [
            'limit' => 5
        ]
    ]]);

    preg_match_all('/]+)>; rel="(?P[^"]+)"/i', $response->getHeader('Link'), $links, PREG_SET_ORDER);
    $links = array_reduce($links, function($carry, $item) {
        $carry[$item['rel']] = $item['href'];
        return $carry;
    }, []);

    $json = $response->json();

    $product_ids = array_merge($product_ids, array_map(function($product) {
        return $product['id'];
    }, $json['products']));

} while(isset($links['next']));

print_r($product_ids);
```

Security
--------

[](#security)

Important notice. If you on shared hosting there is possibility that requests without user agent is restricted (In my case I got 404 Not found errors). To avoid this add user agent to your client like this:

```
