WC_Tax::calc_exclusive_tax()public staticWC 1.0

Calc tax from exclusive price.

Method of the class: WC_Tax{}

Hooks from the method

Return

Array.

Usage

$result = WC_Tax::calc_exclusive_tax( $price, $rates );
$price(float) (required)
Price to calculate tax for.
$rates(array) (required)
Array of tax rates.

WC_Tax::calc_exclusive_tax() code WC 8.7.0

public static function calc_exclusive_tax( $price, $rates ) {
	$taxes = array();
	$price = (float) $price;

	if ( ! empty( $rates ) ) {
		foreach ( $rates as $key => $rate ) {
			if ( 'yes' === $rate['compound'] ) {
				continue;
			}

			$tax_amount = $price * ( floatval( $rate['rate'] ) / 100 );
			$tax_amount = apply_filters( 'woocommerce_price_ex_tax_amount', $tax_amount, $key, $rate, $price ); // ADVANCED: Allow third parties to modify this rate.

			if ( ! isset( $taxes[ $key ] ) ) {
				$taxes[ $key ] = (float) $tax_amount;
			} else {
				$taxes[ $key ] += (float) $tax_amount;
			}
		}

		$pre_compound_total = array_sum( $taxes );

		// Compound taxes.
		foreach ( $rates as $key => $rate ) {
			if ( 'no' === $rate['compound'] ) {
				continue;
			}
			$the_price_inc_tax = $price + $pre_compound_total;
			$tax_amount        = $the_price_inc_tax * ( floatval( $rate['rate'] ) / 100 );
			$tax_amount        = apply_filters( 'woocommerce_price_ex_tax_amount', $tax_amount, $key, $rate, $price, $the_price_inc_tax, $pre_compound_total ); // ADVANCED: Allow third parties to modify this rate.

			if ( ! isset( $taxes[ $key ] ) ) {
				$taxes[ $key ] = (float) $tax_amount;
			} else {
				$taxes[ $key ] += (float) $tax_amount;
			}

			$pre_compound_total = array_sum( $taxes );
		}
	}

	/**
	 * Round all taxes to precision (4DP) before passing them back. Note, this is not the same rounding
	 * as in the cart calculation class which, depending on settings, will round to 2DP when calculating
	 * final totals. Also unlike that class, this rounds .5 up for all cases.
	 */
	$taxes = array_map( array( __CLASS__, 'round' ), $taxes );

	return $taxes;
}