首页 > 解决方案 > 使用 woocommerce API 更改状态时出现 PHP 错误

问题描述

我需要通过将 id 和 status 作为 get 参数传递来使用 woocommerce API 更改交易的状态。我不确定这是否是加载 woo 库的正确方法。我尝试了以下代码但徒劳无功

<?php

require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;

$woocommerce = new Client(
    'http://woosite.com', 
    'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 
    'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    [
        'version' => 'wc/v3',
    ]
);
$json = '{"1": "pending","2": "processing","3": "on-hold","4": "completed","5": "cancelled","6": "refunded","7": "failed"}'

$actionObj = json_decode($json)

$id = $_GET['id'];
$action = $_GET['action'];

    if (isset($id) && isset($action)) {
        $data = [
            'status' => $actionObj[$id]
        ];

        $response  = $woocommerce->put('orders/'. $id , $data);
        print_r("You have changed the status of the order " . $id . " as " + $response["status"])
    }

?>

标签: phpwordpresswoocommercewoocommerce-rest-api

解决方案


推荐阅读