WC_Tax::get_rates_for_tax_class()
Used by admin settings page.
Method of the class: WC_Tax{}
No Hooks.
Return
Array|null|Object
.
Usage
$result = WC_Tax::get_rates_for_tax_class( $tax_class );
- $tax_class(string) (required)
- Tax class slug.
WC_Tax::get_rates_for_tax_class() WC Tax::get rates for tax class code WC 9.5.1
public static function get_rates_for_tax_class( $tax_class ) { global $wpdb; $tax_class = self::format_tax_rate_class( $tax_class ); // Get all the rates and locations. Snagging all at once should significantly cut down on the number of queries. $rates = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM `{$wpdb->prefix}woocommerce_tax_rates` WHERE `tax_rate_class` = %s;", $tax_class ) ); $locations = $wpdb->get_results( "SELECT * FROM `{$wpdb->prefix}woocommerce_tax_rate_locations`" ); if ( ! empty( $rates ) ) { // Set the rates keys equal to their ids. $rates = array_combine( wp_list_pluck( $rates, 'tax_rate_id' ), $rates ); } // Drop the locations into the rates array. foreach ( $locations as $location ) { // Don't set them for nonexistent rates. if ( ! isset( $rates[ $location->tax_rate_id ] ) ) { continue; } // If the rate exists, initialize the array before appending to it. if ( ! isset( $rates[ $location->tax_rate_id ]->{$location->location_type} ) ) { $rates[ $location->tax_rate_id ]->{$location->location_type} = array(); } $rates[ $location->tax_rate_id ]->{$location->location_type}[] = $location->location_code; } foreach ( $rates as $rate_id => $rate ) { $rates[ $rate_id ]->postcode_count = isset( $rates[ $rate_id ]->postcode ) ? count( $rates[ $rate_id ]->postcode ) : 0; $rates[ $rate_id ]->city_count = isset( $rates[ $rate_id ]->city ) ? count( $rates[ $rate_id ]->city ) : 0; } $rates = self::sort_rates( $rates ); return $rates; }