It seems as if $data isn’t correct. I can list products and I get the whole product JSON as a response. But the product in WooCommerce doesn’t change the price. When I do the same thing via curl command line, the update is working.
Referring to that: https://woocommerce.github.io/woocommerce-rest-api-docs/?php#update-a-product
What am I doing wrong?
<?php
$url = "https://wooexample.com/wp-json/wc/v3/products/455";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/json",
"Authorization: Basic " . base64_encode ( 'ck_33fbff7b90dcddba9bd4cbedaeda6b2fa3:cs_4156da18fc357b288e42a7e7b75fa6682b' ),
);
// print_r($headers);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = [
'regular_price' => '24',
'price' => '24'
];
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
print_r(json_decode($resp));
2
Answers
The problem lies on the data format you are sending.
the docs is using a library that’s supposedly transform the data already in a supported format, while your curl request is posting raw array data via curl
you can try transforming your data into a form data with
http_build_query
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data) );
or convert them into JSON format
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data) );
EDIT
You also appear to have wrong curl request, you’re passing the keys on as base64_encoded authorization in headers, but based on the docs you’re supposed to do
Your php curl request should have be something like
Ty this one, I have checked, it’s working perfectly for me.