问题
I have this code that creates a new product in 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));
The product is inserted into the database with the name Premium Quality, the problem is that the SKU is not inserted, and when I try to view in the WordPress panel I see that it is empty.
Why is this happening?
My wordpress and WooCommerce are upgraded to the latest version (and my PHP API too => 1.3.0)
回答1:
Try with 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;
}
来源:https://stackoverflow.com/questions/44979518/woocommerce-api-doesnt-create-product-with-sku