WC_Tax::get_shipping_tax_rates( string $tax_class = null, object $customer = null )
Gets an array of matching shipping tax rates for a given class.
Description Description
Parameters Parameters
- $tax_class
-
(Optional) Tax class to get rates for.
Default value: null
- $customer
-
(Optional) Override the customer object to get their location.
Default value: null
Return Return
(mixed)
Source Source
File: includes/class-wc-tax.php
public static function get_shipping_tax_rates( $tax_class = null, $customer = null ) {
// See if we have an explicitly set shipping tax class.
$shipping_tax_class = get_option( 'woocommerce_shipping_tax_class' );
if ( 'inherit' !== $shipping_tax_class ) {
$tax_class = $shipping_tax_class;
}
$location = self::get_tax_location( $tax_class, $customer );
$matched_tax_rates = array();
if ( 4 === count( $location ) ) {
list( $country, $state, $postcode, $city ) = $location;
if ( ! is_null( $tax_class ) ) {
// This will be per item shipping.
$matched_tax_rates = self::find_shipping_rates(
array(
'country' => $country,
'state' => $state,
'postcode' => $postcode,
'city' => $city,
'tax_class' => $tax_class,
)
);
} elseif ( WC()->cart->get_cart() ) {
// This will be per order shipping - loop through the order and find the highest tax class rate.
$cart_tax_classes = WC()->cart->get_cart_item_tax_classes_for_shipping();
// No tax classes = no taxable items.
if ( empty( $cart_tax_classes ) ) {
return array();
}
// If multiple classes are found, use the first one found unless a standard rate item is found. This will be the first listed in the 'additional tax class' section.
if ( count( $cart_tax_classes ) > 1 && ! in_array( '', $cart_tax_classes, true ) ) {
$tax_classes = self::get_tax_class_slugs();
foreach ( $tax_classes as $tax_class ) {
if ( in_array( $tax_class, $cart_tax_classes, true ) ) {
$matched_tax_rates = self::find_shipping_rates(
array(
'country' => $country,
'state' => $state,
'postcode' => $postcode,
'city' => $city,
'tax_class' => $tax_class,
)
);
break;
}
}
} elseif ( 1 === count( $cart_tax_classes ) ) {
// If a single tax class is found, use it.
$matched_tax_rates = self::find_shipping_rates(
array(
'country' => $country,
'state' => $state,
'postcode' => $postcode,
'city' => $city,
'tax_class' => $cart_tax_classes[0],
)
);
}
}
// Get standard rate if no taxes were found.
if ( ! count( $matched_tax_rates ) ) {
$matched_tax_rates = self::find_shipping_rates(
array(
'country' => $country,
'state' => $state,
'postcode' => $postcode,
'city' => $city,
)
);
}
}
return $matched_tax_rates;
}