承接 mac2000/woo-commerce-api-client 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mac2000/woo-commerce-api-client

Composer 安装命令:

composer require mac2000/woo-commerce-api-client

包简介

API client for Wordpress WooCommerce plugin

README 文档

README

API client for Wordpress WooCommerce plugin.

It was created first of all form importing products into fresh installed store.

API documentation can be found here: http://woothemes.github.io/woocommerce-rest-api-docs/

Installation

This client can be installed using Composer. Add the following to your composer.json:

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

Usage Examples

Instantiation:

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

Creating simple product

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

Notice that if you want product to use predefined attributes you should provide attribute slug.

Create variable product

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

Retrieve products

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

Retrie all product ids

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

Security

Important notice. If you on shared hosting there is possibility that requests without user agent is restricted (In my case I got 404 Not found errors). To avoid this add user agent to your client like this:

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

Missing API

Unfortunately at moment not all things are implemented in WooCommerce API but there is still a way to manage your store.

Project contains class MissingApiHelper that implements some missing things via XMLRPC.

For example as you know attributes for product can be two types - taxonomy that can be used in layered navigation and custom post meta.

If you want create product with taxonomy attribute you should create it first.

Here is full example of creating such product:

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

This code will:

Create categories hierarchy

categories

Create attribute options

attributes

Notice: Attributes itself can not be created from outside, so you should create Brand and Color attributes before importing products

Create product with predefined attributes

product_attributes

mac2000/woo-commerce-api-client 适用场景与选型建议

mac2000/woo-commerce-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.54k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2015 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mac2000/woo-commerce-api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mac2000/woo-commerce-api-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 mac2000/woo-commerce-api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-02