WC_Tax::delete_tax_class_by( string $field, string|int $item )

Delete an existing tax class.


Description Description


Parameters Parameters

$field

(Required) Field to delete by. Valid values are id, name, or slug.

$item

(Required) Item to delete.


Top ↑

Return Return

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


Top ↑

Source Source

File: includes/class-wc-tax.php

	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;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.7.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.