/**
* Delete a single tax.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function delete_item($request)
{
global $wpdb;
$id = (int) $request['id'];
$force = isset($request['force']) ? (bool) $request['force'] : false;
// We don't support trashing for this type, error out.
if (!$force) {
return new WP_Error('woocommerce_rest_trash_not_supported', __('Taxes do not support trashing.', 'woocommerce'), array('status' => 501));
}
$tax = WC_Tax::_get_tax_rate($id, OBJECT);
if (empty($id) || empty($tax)) {
return new WP_Error('woocommerce_rest_invalid_id', __('Invalid resource ID.', 'woocommerce'), array('status' => 400));
}
$request->set_param('context', 'edit');
$response = $this->prepare_item_for_response($tax, $request);
WC_Tax::_delete_tax_rate($id);
if (0 === $wpdb->rows_affected) {
return new WP_Error('woocommerce_rest_cannot_delete', __('The resource cannot be deleted.', 'woocommerce'), array('status' => 500));
}
/**
* Fires after a tax is deleted via the REST API.
*
* @param stdClass $tax The tax data.
* @param WP_REST_Response $response The response returned from the API.
* @param WP_REST_Request $request The request sent to the API.
*/
do_action('woocommerce_rest_delete_tax', $tax, $response, $request);
return $response;
}