WC_Tax::delete_tax_class_by()public staticWC 3.7.0

Delete an existing tax class.

Method of the class: WC_Tax{}

No Hooks.

Return

WP_Error|true|false. Returns true if deleted successfully, false if nothing was deleted, or WP_Error if there is an invalid request.

Usage

$result = WC_Tax::delete_tax_class_by( $field, $item );
$field(string) (required)
Field to delete by. Valid values are id, name, or slug.
$item(string|int) (required)
Item to delete.

Changelog

Since 3.7.0 Introduced.

WC_Tax::delete_tax_class_by() code WC 9.4.2

public static function delete_tax_class_by( $field, $item ) {
	global $wpdb;

	if ( ! in_array( $field, array( 'id', 'name', 'slug' ), true ) ) {
		return new WP_Error( 'invalid_field', __( 'Invalid field', 'woocommerce' ) );
	}

	$tax_class = self::get_tax_class_by( $field, $item );

	if ( ! $tax_class ) {
		return new WP_Error( 'invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) );
	}

	if ( 'id' === $field ) {
		$field = 'tax_rate_class_id';
	}

	$delete = $wpdb->delete(
		$wpdb->wc_tax_rate_classes,
		array(
			$field => $item,
		)
	);

	if ( $delete ) {
		// Delete associated tax rates.
		$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_class = %s;", $tax_class['slug'] ) );
		$wpdb->query( "DELETE locations FROM {$wpdb->prefix}woocommerce_tax_rate_locations locations LEFT JOIN {$wpdb->prefix}woocommerce_tax_rates rates ON rates.tax_rate_id = locations.tax_rate_id WHERE rates.tax_rate_id IS NULL;" );
	}

	wp_cache_delete( 'tax-rate-classes', 'taxes' );
	WC_Cache_Helper::invalidate_cache_group( 'taxes' );

	return (bool) $delete;
}