From 06090808020847b0cd8d670641f904e60c965dec Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 27 Mar 2013 14:32:36 +0000 Subject: [PATCH] find_rates() fixes. GROUP BY after ORDER BY, and use sub queries so we can match both cities and postcodes (not OR) --- classes/class-wc-tax.php | 73 +++++++++++++++++++++++++++------------- readme.txt | 1 + 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/classes/class-wc-tax.php b/classes/class-wc-tax.php index 08b100f8dd6..dcb39100d25 100644 --- a/classes/class-wc-tax.php +++ b/classes/class-wc-tax.php @@ -89,37 +89,68 @@ class WC_Tax { // Run the query $found_rates = $wpdb->get_results( $wpdb->prepare( " - SELECT tax_rates.* FROM - {$wpdb->prefix}woocommerce_tax_rates as tax_rates - LEFT OUTER JOIN - {$wpdb->prefix}woocommerce_tax_rate_locations as locations ON tax_rates.tax_rate_id = locations.tax_rate_id - WHERE - tax_rate_country IN ( %s, '' ) - AND tax_rate_state IN ( %s, '' ) - AND tax_rate_class = %s - AND ( + SELECT * FROM ( + SELECT tax_rates.* FROM + {$wpdb->prefix}woocommerce_tax_rates as tax_rates + LEFT OUTER JOIN + {$wpdb->prefix}woocommerce_tax_rate_locations as locations ON tax_rates.tax_rate_id = locations.tax_rate_id + LEFT OUTER JOIN + {$wpdb->prefix}woocommerce_tax_rate_locations as locations2 ON tax_rates.tax_rate_id = locations2.tax_rate_id + WHERE + tax_rate_country IN ( %s, '' ) + AND tax_rate_state IN ( %s, '' ) + AND tax_rate_class = %s + AND ( - locations.location_type = 'postcode' AND locations.location_code IN ('" . implode( "','", $valid_postcodes ) . "') + ( + locations.location_type IS NULL + ) + OR + ( + locations.location_type = 'postcode' + AND locations.location_code IN ('" . implode( "','", $valid_postcodes ) . "') + AND locations2.location_type = 'city' + AND locations2.location_code = %s + ) + OR + ( + locations.location_type = 'postcode' + AND locations.location_code IN ('" . implode( "','", $valid_postcodes ) . "') + AND 0 = ( + SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_tax_rate_locations as sublocations + WHERE sublocations.location_type = 'city' + AND sublocations.tax_rate_id = tax_rates.tax_rate_id + ) + ) + OR + ( + locations.location_type = 'city' + AND locations.location_code = %s + AND 0 = ( + SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_tax_rate_locations as sublocations + WHERE sublocations.location_type = 'postcode' + AND sublocations.tax_rate_id = tax_rates.tax_rate_id + ) + ) ) - OR - ( - locations.location_type = 'city' AND locations.location_code = %s - ) - OR locations.location_type IS null - ) + GROUP BY + tax_rate_id + ORDER BY + tax_rate_priority, tax_rate_order + ) as ordered_taxes GROUP BY tax_rate_priority - ORDER BY - tax_rate_priority, tax_rate_order ", strtoupper( $country ), strtoupper( $state ), sanitize_title( $tax_class ), + strtoupper( $city ), strtoupper( $city ) ) ); // Put results into array $matched_tax_rates = array(); + foreach ( $found_rates as $found_rate ) $matched_tax_rates[ $found_rate->tax_rate_id ] = array( 'rate' => $found_rate->tax_rate, @@ -128,12 +159,6 @@ class WC_Tax { 'compound' => $found_rate->tax_rate_compound ? 'yes' : 'no' ); - /* - echo '
' . __( 'Matched tax rates:', 'woocommerce' );
-		var_dump( $matched_tax_rates );
-		echo '
'; - */ - return apply_filters( 'woocommerce_matched_tax_rates', $matched_tax_rates, $country, $state, $postcode, $city, $tax_class ); } diff --git a/readme.txt b/readme.txt index 111d15b2235..8767e51a184 100644 --- a/readme.txt +++ b/readme.txt @@ -169,6 +169,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc * Fix - Saving of meta values from paypal after payment. * Fix - woocommerce_nav_menu_items - only hide pages, not other objects. * Fix - woocommerce_add_tinymce_lang array key. +* Fix - Find_rates now works with both postcode and city together. = 2.0.5 - 26/03/2013 = * Tweak - Made no shipping available messages filterable via woocommerce_cart_no_shipping_available_html and woocommerce_no_shipping_available_html.