WC_REST_Taxes_V1_Controller::delete_item
Delete a single tax.
Method of the class: WC_REST_Taxes_V1_Controller{}
Hooks from the method
Returns
WP_Error|WP_REST_Response.
Usage
$WC_REST_Taxes_V1_Controller = new WC_REST_Taxes_V1_Controller(); $WC_REST_Taxes_V1_Controller->delete_item( $request );
- $request(WP_REST_Request) (required)
- Full details about the request.
WC_REST_Taxes_V1_Controller::delete_item() WC REST Taxes V1 Controller::delete item code WC 10.5.0
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;
}