mac2000/woo-commerce-api-client

Wordpress WooCommerce 插件 API 客户端

1.2.1 2015-09-25 12:39 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:41:31 UTC


README

Wordpress WooCommerce 插件 API 客户端。

它最初是为了导入新安装的店铺中的产品而创建的。

API 文档可以在以下位置找到: http://woothemes.github.io/woocommerce-rest-api-docs/

安装

此客户端可以使用 Composer 安装。将以下内容添加到您的 composer.json 文件中

{
    "require": {
        "mac2000/woo-commerce-api-client": "1.0.*"
    }
}

使用示例

实例化

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 required, and options should contain slugs rather than names
                ['name' => 'Brand', 'slug' => 'brand', 'options' => ['nike']],
                ['name' => 'Size', 'slug' => 'size', 'options' => ['M']],
                ['name' => 'Color', 'options' => ['White']]
            ]
        ]
    ]
])->json());

注意,如果您想使用预定义属性的产品,您应该提供属性别名。

创建可变产品

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());

检索所有产品 ID

$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);

安全性

重要提示。如果您在共享主机上,那么没有用户代理的请求可能会被限制(在我的情况下,我得到了404找不到错误)。为了避免这种情况,请像这样向您的客户端添加用户代理

<?php
use Mac2000\WooCommerceApiClient\Client;

require_once 'vendor/autoload.php';

$client = new Client('ck_******', 'cs_******', 'http://acme.com/', [
    'defaults' => [
        'headers' => [
            'User-Agent' => 'WooCommerce API Client'
        ]
    ]
]);

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

缺少 API

遗憾的是,目前 WooCommerce API 中并未实现所有功能,但仍有一种管理您店铺的方法。

项目包含一个 MissingApiHelper 类,它通过 XMLRPC 实现了一些缺失的功能。

例如,您知道产品的属性有两种类型 - 可以用于分层导航的分类法以及自定义文章元数据。

如果您想创建具有分类属性的产品,您应该先创建它。

以下是一个创建此类产品的完整示例

<?php
use Mac2000\WooCommerceApiClient\Client;
use Mac2000\WooCommerceApiClient\MissingApiHelper;

require_once 'vendor/autoload.php';

$client = new Client('ck_******', 'cs_******', 'http://acme.com/');
$helper = new MissingApiHelper('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());

此代码将

创建分类层次结构

categories

创建属性选项

attributes

注意:属性本身不能从外部创建,因此您应该在导入产品之前创建品牌和颜色属性

使用预定义属性创建产品

product_attributes