PHP code example of badgerwise / wc-connect

1. Go to this page and download the library: Download badgerwise/wc-connect library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

badgerwise / wc-connect example snippets


use BadgerWise\WcConnect\WcConnect;
use BadgerWise\WcConnect\Auth\ApplicationPassword;

$wc = new WcConnect(
    siteUrl: 'https://store.example.com',
    auth: new ApplicationPassword('your-wp-username', 'xxxx xxxx xxxx xxxx xxxx xxxx'),
);

$orders = $wc->get('orders', ['per_page' => 20, 'status' => 'processing']);

use BadgerWise\WcConnect\WcConnect;
use BadgerWise\WcConnect\Auth\ConsumerKeys;

$wc = new WcConnect(
    siteUrl: 'https://store.example.com',
    auth: new ConsumerKeys('ck_xxxxxxxx', 'cs_xxxxxxxx'),
);

new ConsumerKeys('ck_xxxxxxxx', 'cs_xxxxxxxx', useQueryString: true);

use BadgerWise\WcConnect\Auth\AuthorizationFlow;

$flow = new AuthorizationFlow('https://store.example.com');

// 1. Send the user here (browser / in-app web view).
$url = $flow->buildAuthorizeUrl(
    appName: 'My Store App',
    successUrl: 'myapp://auth/success', // or an https:// callback
    rejectUrl: 'myapp://auth/reject',
);

// 2. The user logs in and approves. WordPress redirects to your success_url
//    with site_url, user_login and password in the query string.

// 3. Turn those callback params into ready-to-use auth.
$auth = AuthorizationFlow::fromCallback($_GET); // or your deep-link params

$wc = new WcConnect('https://store.example.com', $auth);

$products = $wc->get('products', ['per_page' => 50]);
$order    = $wc->get('orders/123');

$created  = $wc->post('products', ['name' => 'Widget', 'regular_price' => '9.99']);
$updated  = $wc->put('products/123', ['regular_price' => '7.99']);
$deleted  = $wc->delete('products/123', ['force' => true]);

$response = $wc->send('GET', 'orders', ['per_page' => 20]);

$response->statusCode;                 // 200
$response->data;                       // array of orders
$response->intHeader('X-WP-Total');    // e.g. 137

$wp = new WcConnect('https://store.example.com', $auth, apiNamespace: 'wp/v2');
$posts = $wp->get('posts');

$media = $wc->uploadMedia(
    contents: file_get_contents('/path/to/porch.jpg'),
    filename: 'porch.jpg',
    mimeType: 'image/jpeg',
);

$media['id'];         // 510
$media['source_url']; // https://store.example.com/wp-content/uploads/porch.jpg

$wc->orders();
$wc->products();
$wc->customers();

$order   = $wc->orders()->find(123);
$created  = $wc->products()->create(['name' => 'Widget', 'regular_price' => '9.99']);
$updated  = $wc->products()->update(123, ['regular_price' => '7.99']);
$wc->products()->delete(123, ['force' => true]); // permanent; omit to trash

$page = $wc->orders()->list(['per_page' => 20, 'status' => 'processing']);

foreach ($page as $order) {
    // ...
}

$page->total;         // total matching orders across all pages
$page->totalPages;    // total number of pages
$page->page;          // 1
$page->hasMorePages(); // bool
$page->nextPage();     // 2, or null on the last page

$resource = $wc->orders();
$page = 1;

do {
    $result = $resource->list(['per_page' => 100, 'page' => $page]);
    foreach ($result as $order) {
        // ...
    }
    $page = $result->nextPage();
} while ($page !== null);

use BadgerWise\WcConnect\WebhookSignature;

if (! WebhookSignature::verify($rawBody, $webhookSecret, $signatureHeader)) {
    // reject: 401 / ignore
}
// trusted — decode $rawBody and handle the event

use BadgerWise\WcConnect\Exception\ApiException;
use BadgerWise\WcConnect\Exception\WcConnectException;

try {
    $order = $wc->orders()->find(999999);
} catch (ApiException $e) {
    $e->statusCode; // 404
    $e->errorCode;  // 'woocommerce_rest_shop_order_invalid_id'
    $e->body;       // full decoded error body
} catch (WcConnectException $e) {
    // transport failure or malformed response
}

$wc = new WcConnect(
    siteUrl: 'https://store.example.com',
    auth: $auth,
    client: $yourPsr18Client,
    requestFactory: $yourPsr17RequestFactory,
    streamFactory: $yourPsr17StreamFactory,
);