WooCommerce API无法使用SKU创建产品

时间:2017-07-07 20:59:10

标签: php wordpress woocommerce woocommerce-rest-api

我有这个代码在WooCommerce中创建一个新产品:

$woocommerce = new Client(
    'http://www.xxxx.com.br', 
    'ck_xxxxxx', 
    'cs_xxxxxx',
    [
        'wp_api' => true, // Enable the WP REST API integration
        'version' => 'wc/v1' // WooCommerce WP REST API version
    ]
);

    $data = [
        'name' => 'Premium Quality',
        'type' => 'variable', 
        'sku' => '123450000',
    ];

    print_r($woocommerce->post('products', $data));

产品已插入数据库,名称为高级质量,问题是 SKU未插入,当我尝试在WordPress面板中查看时我看到它是空的。

为什么会这样?

我的wordpress和WooCommerce升级到最新版本(我的PHP API也是> 1.3.0)

1 个答案:

答案 0 :(得分:1)

尝试卷曲

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://example.com/wp-json/wc/v3/products",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\n  \"name\": \"Premium Quality\",\n  \"type\": \"variable\",\n  \"sku\" : \"123450000\"\n}",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic ",
    "Content-Type: application/json",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}