So I want to update products using REST API PUT method and it doesn’t work even if the keys are correct. That said, when I use the GET method(I used Postman to test), it can find the product using the same URL. Here’s the code that I’m using.
function update_product_price($product_id, $price, $woocommerce_url, $consumer_key, $consumer_secret) {
// WooCommerce API credentials
$woocommerce_url = 'domain.net/wp-json/wc/v3/products/';
$consumer_key = 'ck-key';
$consumer_secret = 'cs-key';
// Data to update product price
$data = array(
'regular_price' => $price
);
// Initialize cURL session
$ch = curl_init();
error_log($woocommerce_url);
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $woocommerce_url . $product_id);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Basic ' . base64_encode($consumer_key . ':' . $consumer_secret)
));
// Execute cURL request
$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Close cURL session
curl_close($ch);
// Check response status
if ($http_status == 200) {
return true; // Product updated successfully
} else {
return $response; // Error message
}
}
I tried to use PATCH and POST and it did not work. Tried making the url to “domain.net/wp-json/wc/v3/products?sku=” still 404 error.