WC_Tax::get_shipping_tax_ratespublic staticWC 1.0

Gets shipping tax rates based on tax class and customer location.

This method determines which tax rates to apply to shipping costs by following this priority:

  1. Uses the explicitly configured shipping tax class from WooCommerce settings if not set to 'inherit'
  2. If tax_class is provided (per-item shipping), uses that specific class
  3. If no tax_class provided (per-order shipping), analyzes cart items to determine the appropriate class:
  • Returns empty array if cart has no taxable items
  • For multiple tax classes: prioritizes standard rate, then uses first class found in tax class hierarchy
  • For single tax class: uses that class directly
4. Returns only rates that have shipping tax enabled for the determined tax class
```php

- If no shipping rates exist for the tax class, returns empty array (no fallback to standard rates)
- This ensures tax class inheritance works correctly - if a tax class doesn't apply to shipping, no shipping tax is charged

Метод класса: WC_Tax{}

Хуки из метода

Возвращает

Массив. Array of tax rate arrays, each containing 'rate', 'label', 'shipping', and 'compound' keys. Empty array if no shipping rates found for the tax class.

Использование

$result = WC_Tax::get_shipping_tax_rates( $tax_class, $customer );
$tax_class(строка|null)
Specific tax class slug to get rates for. If null, determines from cart contents.
По умолчанию: null
$customer(WC_Customer|null)
Customer object to get location from. Uses current customer if null.
По умолчанию: null

Код WC_Tax::get_shipping_tax_rates() WC 10.8.1

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;
	}

	// If we don't have a shipping tax class yet, work out which one to use.
	if ( is_null( $tax_class ) ) {
		$tax_class = self::get_shipping_tax_class_from_cart_items();
	}

	// If we still don't have a tax class, there must be no taxable items.
	if ( is_null( $tax_class ) ) {
		return array();
	}

	$location = self::get_tax_location( $tax_class, $customer );
	$cart     = WC()->cart ?? null;

	/**
	 * Filters the shipping tax class before calculating tax rates.
	 *
	 * This filter allows plugins to modify or replace the shipping tax class
	 * that will be used to calculate shipping tax rates. It fires after core
	 * logic determines the tax class but before rates are looked up.
	 *
	 * @since 10.5.0
	 *
	 * @param string|null      $tax_class The tax class determined by core logic. Can be null, empty string (standard), or a tax class slug.
	 * @param WC_Cart|null     $cart      The cart object containing all cart items, or null if not available.
	 * @param WC_Customer|null $customer  The customer object, or null if not available.
	 * @param array            $location  The tax location array [country, state, postcode, city].
	 */
	$tax_class = apply_filters( 'woocommerce_shipping_tax_class', $tax_class, $cart, $customer, $location );

	// If filter returned null, treat as no taxable items.
	if ( is_null( $tax_class ) ) {
		return array();
	}

	// Check for a valid location.
	if ( 4 !== count( $location ) ) {
		return array();
	}

	list( $country, $state, $postcode, $city ) = $location;

	return self::find_shipping_rates(
		array(
			'country'   => $country,
			'state'     => $state,
			'postcode'  => $postcode,
			'city'      => $city,
			'tax_class' => $tax_class,
		)
	);
}