diff --git a/includes/class-wc-tax.php b/includes/class-wc-tax.php index 318164655cc..606d10c5d1a 100644 --- a/includes/class-wc-tax.php +++ b/includes/class-wc-tax.php @@ -37,6 +37,29 @@ class WC_Tax { public static function init() { self::$precision = wc_get_rounding_precision(); self::$round_at_subtotal = 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' ); + add_action( 'update_option_woocommerce_tax_classes', array( __CLASS__, 'maybe_remove_tax_class_rates' ), 10, 2 ); + } + + /** + * When the woocommerce_tax_classes option is changed, remove any orphan rates. + * @param string $old_value + * @param string $value + */ + public static function maybe_remove_tax_class_rates( $old_value, $value ) { + $old = array_filter( array_map( 'trim', explode( "\n", $old_value ) ) ); + $new = array_filter( array_map( 'trim', explode( "\n", $value ) ) ); + $removed = array_filter( array_map( 'sanitize_title', array_diff( $old, $new ) ) ); + + if ( $removed ) { + global $wpdb; + + foreach ( $removed as $removed_tax_class ) { + $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_class = %s;", $removed_tax_class ) ); + $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;" ); + } + + WC_Cache_Helper::incr_cache_prefix( 'taxes' ); + } } /** diff --git a/readme.txt b/readme.txt index 1c92f21ee97..17b02c97f85 100644 --- a/readme.txt +++ b/readme.txt @@ -178,6 +178,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc * Allow oAuth1.0a authentication using headers. * Removed internal scroll from log viewer. * Add reply-to to admin emails. +* When deleting a tax rate class, remove it's tax rates. [See changelog for all versions](https://raw.githubusercontent.com/woothemes/woocommerce/master/CHANGELOG.txt).