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/');
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());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.