WC_Tax::get_matched_tax_rates() private WC 1.0
Loop through a set of tax rates and get the matching rates (1 per priority).
{} Это метод класса: WC_Tax{}
Хуки из метода
Возвращает
Массив.
Использование
$result = WC_Tax::get_matched_tax_rates( $country, $state, $postcode, $city, $tax_class );
- $country(строка) (обязательный)
- Country code to match against.
- $state(строка) (обязательный)
- State code to match against.
- $postcode(строка) (обязательный)
- Postcode to match against.
- $city(строка) (обязательный)
- City to match against.
- $tax_class(строка) (обязательный)
- Tax class to match against.
Код WC_Tax::get_matched_tax_rates() WC Tax::get matched tax rates WC 4.9.1
private static function get_matched_tax_rates( $country, $state, $postcode, $city, $tax_class ) {
global $wpdb;
// Query criteria - these will be ANDed.
$criteria = array();
$criteria[] = $wpdb->prepare( "tax_rate_country IN ( %s, '' )", strtoupper( $country ) );
$criteria[] = $wpdb->prepare( "tax_rate_state IN ( %s, '' )", strtoupper( $state ) );
$criteria[] = $wpdb->prepare( 'tax_rate_class = %s', sanitize_title( $tax_class ) );
// Pre-query postcode ranges for PHP based matching.
$postcode_search = wc_get_wildcard_postcodes( $postcode, $country );
$postcode_ranges = $wpdb->get_results( "SELECT tax_rate_id, location_code FROM {$wpdb->prefix}woocommerce_tax_rate_locations WHERE location_type = 'postcode' AND location_code LIKE '%...%';" );
if ( $postcode_ranges ) {
$matches = wc_postcode_location_matcher( $postcode, $postcode_ranges, 'tax_rate_id', 'location_code', $country );
if ( ! empty( $matches ) ) {
foreach ( $matches as $matched_postcodes ) {
$postcode_search = array_merge( $postcode_search, $matched_postcodes );
}
}
}
$postcode_search = array_unique( $postcode_search );
/**
* Location matching criteria - ORed
* Needs to match:
* - rates with no postcodes and cities
* - rates with a matching postcode and city
* - rates with matching postcode, no city
* - rates with matching city, no postcode
*/
$locations_criteria = array();
$locations_criteria[] = 'locations.location_type IS NULL';
$locations_criteria[] = "
locations.location_type = 'postcode' AND locations.location_code IN ('" . implode( "','", array_map( 'esc_sql', $postcode_search ) ) . "')
AND (
( locations2.location_type = 'city' AND locations2.location_code = '" . esc_sql( strtoupper( $city ) ) . "' )
OR NOT EXISTS (
SELECT sub.tax_rate_id FROM {$wpdb->prefix}woocommerce_tax_rate_locations as sub
WHERE sub.location_type = 'city'
AND sub.tax_rate_id = tax_rates.tax_rate_id
)
)
";
$locations_criteria[] = "
locations.location_type = 'city' AND locations.location_code = '" . esc_sql( strtoupper( $city ) ) . "'
AND NOT EXISTS (
SELECT sub.tax_rate_id FROM {$wpdb->prefix}woocommerce_tax_rate_locations as sub
WHERE sub.location_type = 'postcode'
AND sub.tax_rate_id = tax_rates.tax_rate_id
)
";
$criteria[] = '( ( ' . implode( ' ) OR ( ', $locations_criteria ) . ' ) )';
$criteria_string = implode( ' AND ', $criteria );
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$found_rates = $wpdb->get_results(
"
SELECT tax_rates.*, COUNT( locations.location_id ) as postcode_count, COUNT( locations2.location_id ) as city_count
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 1=1 AND {$criteria_string}
GROUP BY tax_rates.tax_rate_id
ORDER BY tax_rates.tax_rate_priority
"
);
// phpcs:enable
$found_rates = self::sort_rates( $found_rates );
$matched_tax_rates = array();
$found_priority = array();
foreach ( $found_rates as $found_rate ) {
if ( in_array( $found_rate->tax_rate_priority, $found_priority, true ) ) {
continue;
}
$matched_tax_rates[ $found_rate->tax_rate_id ] = array(
'rate' => (float) $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',
);
$found_priority[] = $found_rate->tax_rate_priority;
}
return apply_filters( 'woocommerce_matched_tax_rates', $matched_tax_rates, $country, $state, $postcode, $city, $tax_class );
}