mindsize/laravel5-woocommerce

WooCommerce API Client for Laravel 5

1.0.3 2017-09-25 16:18 UTC

This package is auto-updated.

Last update: 2024-11-19 08:51:22 UTC


README

Latest Version on Packagist Software License Travis Build Total Downloads

A simple Laravel 5 wrapper for the official WooCommerce REST API PHP Library from Automattic.

Installation

Step 1: Install Through Composer

composer require mindsize/laravel5-woocommerce

Step 2: Publish configuration

php artisan vendor:publish --provider="Mindsize\WooCommerce\ServiceProvider"

Step 3: Customize configuration

You can directly edit the configuration in config/woocommerce.php or copy these values to your .env file.

WC_STORE_URL=http://example.org
WC_CONSUMER_KEY=ck_your-consumer-key
WC_CONSUMER_SECRET=cs_your-consumer-secret
WC_VERIFY_SSL=false
WC_VERSION=v1
WC_WP_API=true
WC_WP_QUERY_STRING_AUTH=false
WC_WP_TIMEOUT=15

Examples

Get the index of all available endpoints

use WooCommerce;

return WooCommerce::get('');

View all orders

use WooCommerce;

return WooCommerce::get('orders');

View all completed orders created after a specific date

For legacy API versions

(WC 2.4.x or later, WP 4.1 or later) use this syntax

use WooCommerce;

$data = [
    'status' => 'completed',
    'filter' => [
        'created_at_min' => '2016-01-14'
    ]
];

$result = WooCommerce::get('orders', $data);

foreach($result['orders'] as $order)
{
    // do something with $order
}

// you can also use array access
$orders = WooCommerce::get('orders', $data)['orders'];

foreach($orders as $order)
{
    // do something with $order
}

For current API versions

(WC 2.6.x or later, WP 4.4 or later) use this syntax. after needs to be a ISO-8601 compliant date!≠

use WooCommerce;

$data = [
    'status' => 'completed',
    'after' => '2016-01-14T00:00:00'
    ]
];

$result = WooCommerce::get('orders', $data);

foreach($result['orders'] as $order)
{
    // do something with $order
}

// you can also use array access
$orders = WooCommerce::get('orders', $data)['orders'];

foreach($orders as $order)
{
    // do something with $order
}

Update a product

use WooCommerce;

$data = [
    'product' => [
        'title' => 'Updated title'
    ]
];

return WooCommerce::put('products/1', $data);

Pagination

So you don't have to mess around with the request and response header and the calculations this wrapper will do all the heavy lifting for you. (WC 2.6.x or later, WP 4.4 or later)

use WooCommerce;

// assuming we have 474 orders in pur result
// we will request page 5 with 25 results per page
$params = [
    'per_page' => 25,
    'page' => 5
];

WooCommerce::get('orders', $params);

WooCommerce::totalResults(); // 474
WooCommerce::firstPage(); // 1
WooCommerce::lastPage(); // 19
WooCommerce::currentPage(); // 5 
WooCommerce::totalPages(); // 19
WooCommerce::previousPage(); // 4
WooCommerce::nextPage(); // 6
WooCommerce::hasPreviousPage(); // true 
WooCommerce::hasNextPage(); // true
WooCommerce::hasNotPreviousPage(); // false 
WooCommerce::hasNotNextPage(); // false

HTTP Request & Response (Headers)

use WooCommerce;

// first send a request
WooCommerce::get('orders');

// get the request
WooCommerce::getRequest();

// get the response headers
WooCommerce::getResponse();

// get the total number of results
WooCommerce::getResponse()->getHeaders()['X-WP-Total']

More Examples

Refer to WooCommerce REST API Documentation for more examples and documention.

Testing

Run the tests with:

vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.

Forked from pixelpeter/laravel5-woocommerce-api-client

Thanks Peter!