A simple Laravel 5 wrapper for the official WooCommerce REST API PHP Library from Automattic.
composer require mindsize/laravel5-woocommerce
php artisan vendor:publish --provider="Mindsize\WooCommerce\ServiceProvider"
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
use WooCommerce;
return WooCommerce::get('');
use WooCommerce;
return WooCommerce::get('orders');
(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
}
(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
}
use WooCommerce;
$data = [
'product' => [
'title' => 'Updated title'
]
];
return WooCommerce::put('products/1', $data);
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
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']
Refer to WooCommerce REST API Documentation for more examples and documention.
Run the tests with:
vendor/bin/phpunit
The MIT License (MIT). Please see License File for more information.
Forked from pixelpeter/laravel5-woocommerce-api-client
Thanks Peter!