gpt4 book ai didi

php - WooCommerce API 不使用 SKU 创建产品

转载 作者:搜寻专家 更新时间:2023-10-31 21:22:07 33 4
gpt4 key购买 nike

我有这段代码可以在 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));

产品以名称 Premium Quality 插入数据库,问题是 SKU 未插入,当我尝试在 WordPress 面板中查看时我看到它是空的。

为什么会这样?

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

最佳答案

尝试 curl

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

关于php - WooCommerce API 不使用 SKU 创建产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44979518/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com