Merge pull request #4488 from GeertDD/cached_tax_rates

Cached tax rates
This commit is contained in:
Mike Jolley 2014-01-13 03:19:52 -08:00
commit 2e0291eff1
1 changed files with 77 additions and 66 deletions

View File

@ -237,79 +237,90 @@ class WC_Tax {
$valid_postcodes[] = $wildcard_postcode . '*'; $valid_postcodes[] = $wildcard_postcode . '*';
} }
// Run the query // Build transient key and try to retrieve them from cache
$found_rates = $wpdb->get_results( $wpdb->prepare( " $rates_transient_key = 'wc_tax_rates_' . md5( sprintf( '%s+%s+%s+%s+%s', $country, $state, $city, implode( ',', $valid_postcodes), $tax_class ) );
SELECT * FROM ( $matched_tax_rates = get_transient( $rates_transient_key );
SELECT tax_rates.* FROM
{$wpdb->prefix}woocommerce_tax_rates as tax_rates if ( false === $matched_tax_rates ) {
LEFT OUTER JOIN
{$wpdb->prefix}woocommerce_tax_rate_locations as locations ON tax_rates.tax_rate_id = locations.tax_rate_id // Run the query
LEFT OUTER JOIN $found_rates = $wpdb->get_results( $wpdb->prepare( "
{$wpdb->prefix}woocommerce_tax_rate_locations as locations2 ON tax_rates.tax_rate_id = locations2.tax_rate_id SELECT * FROM (
WHERE SELECT tax_rates.* FROM
tax_rate_country IN ( %s, '' ) {$wpdb->prefix}woocommerce_tax_rates as tax_rates
AND tax_rate_state IN ( %s, '' ) LEFT OUTER JOIN
AND tax_rate_class = %s {$wpdb->prefix}woocommerce_tax_rate_locations as locations ON tax_rates.tax_rate_id = locations.tax_rate_id
AND 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 IS NULL (
) locations.location_type IS NULL
OR )
( OR
locations.location_type = 'postcode' (
AND locations.location_code IN ('" . implode( "','", $valid_postcodes ) . "') locations.location_type = 'postcode'
AND locations2.location_type = 'city' AND locations.location_code IN ('" . implode( "','", $valid_postcodes ) . "')
AND locations2.location_code = %s AND locations2.location_type = 'city'
) AND locations2.location_code = %s
OR )
( OR
locations.location_type = 'postcode' (
AND locations.location_code IN ('" . implode( "','", $valid_postcodes ) . "') locations.location_type = 'postcode'
AND 0 = ( AND locations.location_code IN ('" . implode( "','", $valid_postcodes ) . "')
SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_tax_rate_locations as sublocations AND 0 = (
WHERE sublocations.location_type = 'city' SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_tax_rate_locations as sublocations
AND sublocations.tax_rate_id = tax_rates.tax_rate_id 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 GROUP BY
( tax_rate_id
locations.location_type = 'city' ORDER BY
AND locations.location_code = %s tax_rate_priority, tax_rate_order
AND 0 = ( ) as ordered_taxes
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
)
)
)
GROUP BY GROUP BY
tax_rate_id tax_rate_priority
ORDER BY ",
tax_rate_priority, tax_rate_order strtoupper( $country ),
) as ordered_taxes strtoupper( $state ),
GROUP BY sanitize_title( $tax_class ),
tax_rate_priority strtoupper( $city ),
", strtoupper( $city )
strtoupper( $country ), ) );
strtoupper( $state ),
sanitize_title( $tax_class ),
strtoupper( $city ),
strtoupper( $city )
) );
// Put results into array // Put results into array
$matched_tax_rates = array(); $matched_tax_rates = array();
foreach ( $found_rates as $found_rate ) foreach ( $found_rates as $found_rate )
$matched_tax_rates[ $found_rate->tax_rate_id ] = array( $matched_tax_rates[ $found_rate->tax_rate_id ] = array(
'rate' => $found_rate->tax_rate, 'rate' => $found_rate->tax_rate,
'label' => $found_rate->tax_rate_name, 'label' => $found_rate->tax_rate_name,
'shipping' => $found_rate->tax_rate_shipping ? 'yes' : 'no', 'shipping' => $found_rate->tax_rate_shipping ? 'yes' : 'no',
'compound' => $found_rate->tax_rate_compound ? 'yes' : 'no' 'compound' => $found_rate->tax_rate_compound ? 'yes' : 'no'
); );
return apply_filters( 'woocommerce_matched_tax_rates', $matched_tax_rates, $country, $state, $postcode, $city, $tax_class ); $matched_tax_rates = apply_filters( 'woocommerce_matched_tax_rates', $matched_tax_rates, $country, $state, $postcode, $city, $tax_class );
set_transient( $rates_transient_key, $matched_tax_rates, DAY_IN_SECONDS );
}
return $matched_tax_rates;
} }
/** /**