guidemaster/laravel-woocommerce

dev-main 2024-05-23 13:46 UTC

This package is auto-updated.

Last update: 2024-05-23 13:46:26 UTC


README

Latest Version on Packagist Total Downloads Build Status StyleCI

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

Installation

##Step 1: Install through Composer

composer require guidemaster/laravel-woocommerce

##Step 2: Publish configuration

php artisan vendor:publish --tag=GuideMaster\LaravelWoocommerce\LaravelWoocommerceServiceProvide

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

WOOCOMMERCE_STORE_URL=https://example-store.org
WOOCOMMERCE_CONSUMER_KEY=ck_your-consumer-key
WOOCOMMERCE_CONSUMER_SECRET=cs_your-consumer-secret
WOOCOMMERCE_VERIFY_SSL=false
WOOCOMMERCE_VERSION=v1
WOOCOMMERCE_WP_API=true
WOOCOMMERCE_WP_QUERY_STRING_AUTH=false
WOOCOMMERCE_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 documentation.

Change log

Please see the changelog for more information on what has changed recently.

Testing

composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email author@email.com instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.