PHP code example of baldie81 / woocommerce-sdk

1. Go to this page and download the library: Download baldie81/woocommerce-sdk 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/ */

    

baldie81 / woocommerce-sdk example snippets


use Baldie81\WooCommerceSDK\Configuration;
use Baldie81\WooCommerceSDK\WooCommerceClient;

$woo = WooCommerceClient::create(new Configuration(
    baseUrl:        'https://shop.example.com',
    consumerKey:    'ck_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    consumerSecret: 'cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    // timeout: 30.0, connectTimeout: 10.0, verifySsl: true,
));

$product = $woo->products()->get(123);

new Configuration(
    baseUrl:         'http://localhost:8080',
    consumerKey:     'ck_...',
    consumerSecret:  'cs_...',
    verifySsl:       false,
    queryStringAuth: true,
);

use Baldie81\WooCommerceSDK\Support\ProductInput;
use Baldie81\WooCommerceSDK\Enum\ProductStatus;
use Baldie81\WooCommerceSDK\Enum\ProductType;

// Builder — a simple product with an image and a variation attribute:
$woo->products()->create(
    ProductInput::make()
        ->name('Example')->type(ProductType::Simple)->sku('SKU-123')
        ->regularPrice('19.90')->status(ProductStatus::Publish)
        ->manageStock(true)->stockQuantity(100)
        ->categories([15, 21])
        ->image('https://example.com/front.jpg', 'Front')
        ->meta('_origin', 'import')
);

// …or the exact same call with a raw array — accepted everywhere:
$woo->products()->create([
    'name'           => 'Example',
    'type'           => 'simple',
    'sku'            => 'SKU-123',
    'regular_price'  => '19.90',
    'status'         => 'publish',
    'manage_stock'   => true,
    'stock_quantity' => 100,
    'categories'     => [['id' => 15], ['id' => 21]],
]);

use Baldie81\WooCommerceSDK\Support\VariationInput;

$parent = $woo->products()->create(
    ProductInput::make()
        ->name('T-Shirt')->type(ProductType::Variable)
        ->attribute('Color', ['Red', 'Blue'], variation: true)
        ->attribute('Size', ['S', 'M', 'L'], variation: true)
);

$variations = $woo->products()->variations($parent['id']);
$variations->create(
    VariationInput::make()
        ->sku('TS-RED-M')->regularPrice('24.90')
        ->attribute('Color', 'Red')->attribute('Size', 'M')
        ->manageStock(true)->stockQuantity(10)
);

use Baldie81\WooCommerceSDK\Support\OrderInput;
use Baldie81\WooCommerceSDK\Support\Address;
use Baldie81\WooCommerceSDK\Support\LineItem;
use Baldie81\WooCommerceSDK\Enum\OrderStatus;

$order = $woo->orders()->create(
    OrderInput::make()
        ->status(OrderStatus::Processing)->setPaid(true)
        ->paymentMethod('bacs', 'Direct bank transfer')
        ->billing(
            Address::make()->name('Jane', 'Doe')
                ->line1('1 High St')->city('London')->postcode('EC1A 1AA')->country('GB')
                ->email('[email protected]')
        )
        ->lineItem(LineItem::make()->product(93)->quantity(2))
        ->shippingLine('flat_rate', 'Flat rate', '5.00')
        ->couponLine('SAVE10')
);

$woo->orders()->setStatus($order['id'], OrderStatus::Completed);

// Notes and refunds hang off an order id:
$woo->orders()->notes($order['id'])->create('Picked & packed', customerNote: false);
$woo->orders()->refunds($order['id'])->refundAmount('12.50', 'Damaged item');

use Baldie81\WooCommerceSDK\Support\Query;
use Baldie81\WooCommerceSDK\Enum\ProductStatus;

// One page:
$page = $woo->products()->list(
    Query::create()->search('shirt')->status(ProductStatus::Publish)->perPage(50)
);

// Every matching record, across all pages:
$everything = $woo->products()->all(
    Query::create()->after('2026-01-01T00:00:00')->orderBy('date', 'desc')
);

// Direct look-ups by natural key:
$bySku   = $woo->products()->bySku('SKU-123');     // ?array
$byEmail = $woo->customers()->byEmail('[email protected]');
$byCode  = $woo->coupons()->byCode('SAVE10');

use Baldie81\WooCommerceSDK\Support\BatchPayload;
use Baldie81\WooCommerceSDK\Support\ProductInput;

$result = $woo->products()->batch(
    BatchPayload::make()
        ->create(ProductInput::make()->name('A')->sku('A-1')->regularPrice('5'))
        ->update(ProductInput::make()->id(42)->regularPrice('9.99'))
        ->delete(43, 44)
);

// $result['create'], $result['update'], $result['delete']

$woo->products()->delete(123);                // permanent
$woo->orders()->delete(456, force: false);    // trashed

use Baldie81\WooCommerceSDK\Exception\ApiException;
use Baldie81\WooCommerceSDK\Exception\TransportException;

try {
    $woo->products()->get(999999);
} catch (ApiException $e) {
    if ($e->statusCode === 404) {
        // not found — $e->errorCode is "woocommerce_rest_product_invalid_id"
    }
} catch (TransportException $e) {
    // network-level failure, safe to retry
}

use Baldie81\WooCommerceSDK\WooCommerceClient;

$woo = WooCommerceClient::fromGuzzle($myGuzzleClient, 'wp-json/wc/v3/');