PHP code example of mac2000 / woo-commerce-api-client

1. Go to this page and download the library: Download mac2000/woo-commerce-api-client 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/ */

    

mac2000 / woo-commerce-api-client example snippets


use Mac2000\WooCommerceApiClient\Client as Woo;
$client = new Woo('consumer_key', 'consumer_secret', 'http://acme.com/');

print_r($client->post('products', [
    'json' => [
        'product' => [
            'title' => 'Simple T-Shirt',
            'type' => 'simple',
            'regular_price' => 9.99,
            'description' => 'T-Shirt description goes here',
            'short_description' => 'short description',
            'categories' => ['Wear', 'T-Shirts'],
            'images' => [
                ['src' => 'http://placehold.it/800x600', 'position' => 0]
            ],
            'attributes' => [
                //Important: for predefined attributes slug is 

print_r($client->post('products', [
    'json' => [
        'product' => [
            'title' => 'Variable T-Shirt',
            'type' => 'variable',
            'regular_price' => 9.99,
            'description' => 'T-Shirt description goes here',
            'short_description' => 'short description',
            'categories' => ['Wear', 'T-Shirts'],
            'images' => [
                ['src' => 'http://placehold.it/800x600', 'position' => 0]
            ],
            'attributes' => [
                ['name' => 'Brand', 'slug' => 'brand', 'options' => ['Nike']],
                ['name' => 'Size', 'slug' => 'size', 'options' => ['S','M','L'], 'variation' => true], //Notice: All options that will be used in variations should be here
                ['name' => 'Color', 'options' => ['White', 'Black']]
            ],
            'variations' => [
                [
                    'regular_price' => 8.99,
                    'attributes' => [['name' => 'Size', 'slug' => 'size', 'option' => 'S']]
                ],
                [
                    'regular_price' => 9.99,
                    'attributes' => [['name' => 'Size', 'slug' => 'size', 'option' => 'M']]
                ],
                [
                    'regular_price' => 10.99,
                    'attributes' => [['name' => 'Size', 'slug' => 'size', 'option' => 'L']]
                ]
            ]
        ]
    ]
])->json());

print_r($client->get('products')->json());

$page = 0;
$product_ids = [];

do {
    $page++;

    $response = $client->get('products', ['query' => [
        'fields' => 'id',
        'page' => $page,
        'filter' => [
            'limit' => 5
        ]
    ]]);

    preg_match_all('/<(?P<href>[^>]+)>; rel="(?P<rel>[^"]+)"/i', $response->getHeader('Link'), $links, PREG_SET_ORDER);
    $links = array_reduce($links, function($carry, $item) {
        $carry[$item['rel']] = $item['href'];
        return $carry;
    }, []);
    
    $json = $response->json();

    $product_ids = array_merge($product_ids, array_map(function($product) {
        return $product['id'];
    }, $json['products']));

} while(isset($links['next']));

print_r($product_ids);


use Mac2000\WooCommerceApiClient\Client;

 'http://acme.com/', [
    'defaults' => [
        'headers' => [
            'User-Agent' => 'WooCommerce API Client'
        ]
    ]
]);

print_r($client->get('products')->json());


use Mac2000\WooCommerceApiClient\Client;
use Mac2000\WooCommerceApiClient\MissingApiHelper;

r('http://acme.com/xmlrpc.php', 'admin', '******');

// This is optional step, WooCommerce api will create categories
// but it will create them flat, so we are creating them manually
// to ensure that they belong to each other
$helper->ensureTwoLevelProductCategory('Wear', 'T-Shirts');

// Notice: At this moment there is no way to create attributes from outside
// so you should create them by hand before adding options
$brand = $helper->ensureAttributeOption('Brand', 'Nike');
$color = $helper->ensureAttributeOption('Color', 'White');

$brand_slug = str_replace('pa_', '', $brand['taxonomy']);
$color_slug = str_replace('pa_', '', $color['taxonomy']);

$product = [
    'title' => 'Simple T-Shirt',
    'type' => 'simple',
    'regular_price' => 9.99,
    'description' => 'T-Shirt description goes here',
    'short_description' => 'short description',
    'categories' => ['Wear', 'T-Shirts'],
    'images' => [
        ['src' => 'http://placehold.it/800x600', 'position' => 0]
    ],
    'attributes' => [
        ['name' => 'Brand', 'slug' => $brand_slug, 'options' => [$brand['slug']]], // Select attribute
        ['name' => 'Color', 'slug' => $color_slug, 'options' => [$color['slug']]], // Text attribute
        ['name' => 'Jackets', 'options' => ['One']] // Custom attribute
    ]
];

$response = $client->post('products', ['json' => ['product' => $product]]);
print_r($response->json());