Skip to content
Advertisement

Unable to delete product category via WooCommerce REST API

I’m using WooCommerce REST API v3, to perform basic CRUD operations on products and categories, remotely – WP is hosted on one server (e.g. example.com), and the logic doing the CRUD is on another server (e.g. mysite.com).

While I am able to list, create and update product categories, I can not delete them. None of the categories I want to delete have subcategories, and there are no products attached to them.

This is my code:

$consumer_key = "<consumer_key>";
$consumer_secret = "<consumer_secret>";
$catUrl = "https://www.example.com/wp-json/wc/v3/products/categories/";
$catId = 63;  // id of the category which I want to delete

$url = $catUrl.$catId."?force=true&consumer_key=".$consumer_key."&consumer_secret=".$consumer_secret;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

$response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "<pre>";
if(isJson($result)) {
    print_r(json_decode($result,true));
} else {
    print_r($result);
}
echo "<hr>";
print_r($response);
echo "</pre>";
echo $url;

cURL returns what I’d expect, and what the documentation says, the status code is 200, but when I look at the product categories in my WordPress site, the category is still there.

I have also tried changing the request type to POST (the server doesn’t support DELETE as a custom request type), like this:

$url = $catUrl.$catId."?consumer_key=".$consumer_key."&consumer_secret=".$consumer_secret;

$data = array(
    "force" => true
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$result = curl_exec($ch);

My guess is that the API doesn’t know that I’m trying to delete something, and that’s why it’s just returning the info on the category (force=true not being enough for deleting the category).

Is there any other parameter which I can pass to WC API, in order to make it delete the category I want it to delete? If so, how?

Advertisement

Answer

I’ve managed to solve it by adding an additional parameter – _method=DELETE – thanks to instructions on how to do an override found here.

Working code:

$consumer_key = "<consumer_key>";
$consumer_secret = "<consumer_secret>";
$catUrl = "https://www.example.com/wp-json/wc/v3/products/categories/";
$catId = 63;  // id of the category which I want to delete
$url = $catUrl.$catId."?_method=DELETE&consumer_key=".$consumer_key."&consumer_secret=".$consumer_secret;

$data = array(
    "force" => true
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$result = curl_exec($ch);

curl_close($ch);

echo "<pre>";
if(isJson($result)) {
    print_r(json_decode($result,true));
} else {
    print_r($result);
}
echo "<hr>";
print_r($response);
echo "</pre>";
echo $url;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement