12年专注,只做WordPress定制开发一件事

搜索
登录/注册

使用 WooCommerce REST API 实现外部系统对接

王超
2025-03-19
Woocommerce开发
49 次

WooCommerce REST API 允许开发者通过 HTTP 请求与 WooCommerce 进行交互,例如获取订单、管理产品、处理支付等功能。本文将详细讲解如何使用 WooCommerce REST API 对接外部系统,实现自动化操作。

1. 启用 WooCommerce REST API

在 WooCommerce 中,REST API 默认是启用的,但需要生成 API 凭据以进行身份验证。

1.1 生成 API Key

  1. 登录 WordPress 后台,进入 WooCommerce > 设置 > 高级 > REST API
  2. 点击 添加密钥,填写 描述 并选择 用户权限(建议选择 Read/Write 以允许读取和写入数据)
  3. 生成后,会获得 Consumer KeyConsumer Secret,请妥善保存。

2. 通过 API 获取 WooCommerce 订单数据

一旦获得 API 凭据,就可以使用它们来获取 WooCommerce 的订单数据。

2.1 使用 PHP 请求 WooCommerce API

$consumer_key = 'your_consumer_key';
$consumer_secret = 'your_consumer_secret';
$api_url = 'https://example.com/wp-json/wc/v3/orders';

$response = wp_remote_get($api_url, [
    'headers' => [
        'Authorization' => 'Basic ' . base64_encode("$consumer_key:$consumer_secret")
    ]
]);

if (is_wp_error($response)) {
    echo '请求失败';
} else {
    $orders = json_decode(wp_remote_retrieve_body($response), true);
    print_r($orders);
}

2.2 使用 cURL 进行 API 请求

如果你的环境不支持 wp_remote_get(),可以使用 cURL:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/wp-json/wc/v3/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'your_consumer_key:your_consumer_secret');
$result = curl_exec($ch);
curl_close($ch);

$orders = json_decode($result, true);
print_r($orders);

3. 向 WooCommerce 添加新订单

外部系统可以通过 WooCommerce REST API 创建订单,例如,来自 CRM 系统的订单数据可以自动同步到 WooCommerce。

3.1 发送 POST 请求创建订单

$api_url = 'https://example.com/wp-json/wc/v3/orders';

$order_data = [
    'payment_method' => 'bacs',
    'payment_method_title' => 'Bank Transfer',
    'set_paid' => true,
    'billing' => [
        'first_name' => 'John',
        'last_name' => 'Doe',
        'email' => 'john.doe@example.com'
    ],
    'line_items' => [
        [
            'product_id' => 123,
            'quantity' => 1
        ]
    ]
];

$response = wp_remote_post($api_url, [
    'headers' => [
        'Authorization' => 'Basic ' . base64_encode("$consumer_key:$consumer_secret"),
        'Content-Type' => 'application/json'
    ],
    'body' => json_encode($order_data)
]);

$new_order = json_decode(wp_remote_retrieve_body($response), true);
print_r($new_order);

4. 通过 API 更新订单状态

如果订单已经创建,外部系统可能需要更新订单状态。例如,支付完成后,修改订单为 completed 状态。

4.1 发送 PUT 请求更新订单

$order_id = 456; // 订单 ID
$api_url = "https://example.com/wp-json/wc/v3/orders/$order_id";

$update_data = [
    'status' => 'completed'
];

$response = wp_remote_request($api_url, [
    'method' => 'PUT',
    'headers' => [
        'Authorization' => 'Basic ' . base64_encode("$consumer_key:$consumer_secret"),
        'Content-Type' => 'application/json'
    ],
    'body' => json_encode($update_data)
]);

$updated_order = json_decode(wp_remote_retrieve_body($response), true);
print_r($updated_order);

5. WooCommerce Webhooks 实现自动化对接

除了使用 REST API 轮询数据,你还可以使用 Webhooks 来监听 WooCommerce 事件,如订单创建、付款成功等。

5.1 在 WooCommerce 设置 Webhook

  1. 进入 WooCommerce > 设置 > 高级 > Webhooks
  2. 点击 添加 Webhook,设置 触发事件(例如 order.created)。
  3. 输入 Webhook URL(外部系统的回调地址),保存后即生效。

5.2 处理 Webhook 请求(示例 PHP 代码)

$data = json_decode(file_get_contents('php://input'), true);
if (!empty($data)) {
    file_put_contents('webhook_log.txt', print_r($data, true));
}

本文介绍了如何使用 WooCommerce REST API 进行外部系统对接,包括 API 认证、获取订单、创建订单、更新订单状态,并介绍了 Webhooks 的使用方式。通过这些技术,可以实现 WooCommerce 与 CRM、ERP、物流等系统的深度集成,提高业务自动化水平。

文章标签:

WordPress主题开发业务联系方式

WordPress日记主要承接WordPress主题定制开发PSD转WordPressWordPress仿站以及以WordPress为管理后端的小程序、APP,我们一直秉持“做一个项目,交一个朋友”的理念,希望您是我们下一个朋友。如果您有WordPress主题开发需求,可随时联系QQ:919985494 微信:18539976310

上一篇:已是最新文章

下一篇:

搜索

在线客服
嘿,有问题找我来帮您!