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 . '*';
}
// Run the query
$found_rates = $wpdb->get_results( $wpdb->prepare( "
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
(
// Build transient key and try to retrieve them from cache
$rates_transient_key = 'wc_tax_rates_' . md5( sprintf( '%s+%s+%s+%s+%s', $country, $state, $city, implode( ',', $valid_postcodes), $tax_class ) );
$matched_tax_rates = get_transient( $rates_transient_key );
if ( false === $matched_tax_rates ) {
// Run the query
$found_rates = $wpdb->get_results( $wpdb->prepare( "
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 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
(
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
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
)
)
)
GROUP BY
tax_rate_id
ORDER BY
tax_rate_priority, tax_rate_order
) as ordered_taxes
GROUP BY
tax_rate_id
ORDER BY
tax_rate_priority, tax_rate_order
) as ordered_taxes
GROUP BY
tax_rate_priority
",
strtoupper( $country ),
strtoupper( $state ),
sanitize_title( $tax_class ),
strtoupper( $city ),
strtoupper( $city )
) );
tax_rate_priority
",
strtoupper( $country ),
strtoupper( $state ),
sanitize_title( $tax_class ),
strtoupper( $city ),
strtoupper( $city )
) );
// Put results into array
$matched_tax_rates = array();
// 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,
'label' => $found_rate->tax_rate_name,
'shipping' => $found_rate->tax_rate_shipping ? 'yes' : 'no',
'compound' => $found_rate->tax_rate_compound ? 'yes' : 'no'
);
foreach ( $found_rates as $found_rate )
$matched_tax_rates[ $found_rate->tax_rate_id ] = array(
'rate' => $found_rate->tax_rate,
'label' => $found_rate->tax_rate_name,
'shipping' => $found_rate->tax_rate_shipping ? '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;
}
/**