WC_Tax::calc_inclusive_tax
Calc tax from inclusive price.
Method of the class: WC_Tax{}
Hooks from the method
Returns
Array.
Usage
$result = WC_Tax::calc_inclusive_tax( $price, $rates );
- $price(float) (required)
- Price to calculate tax for.
- $rates(array) (required)
- Array of tax rates.
WC_Tax::calc_inclusive_tax() WC Tax::calc inclusive tax code WC 10.4.3
public static function calc_inclusive_tax( $price, $rates ) {
$taxes = array();
$compound_rates = array();
$regular_rates = array();
// Index array so taxes are output in correct order and see what compound/regular rates we have to calculate.
foreach ( $rates as $key => $rate ) {
$taxes[ $key ] = 0;
if ( 'yes' === $rate['compound'] ) {
$compound_rates[ $key ] = $rate['rate'];
} else {
$regular_rates[ $key ] = $rate['rate'];
}
}
$compound_rates = array_reverse( $compound_rates, true ); // Working backwards.
$non_compound_price = $price;
foreach ( $compound_rates as $key => $compound_rate ) {
$tax_amount = apply_filters( 'woocommerce_price_inc_tax_amount', $non_compound_price - ( $non_compound_price / ( 1 + ( $compound_rate / 100 ) ) ), $key, $rates[ $key ], $price );
$taxes[ $key ] += $tax_amount;
$non_compound_price = $non_compound_price - $tax_amount;
}
// Regular taxes.
$regular_tax_rate = 1 + ( array_sum( $regular_rates ) / 100 );
foreach ( $regular_rates as $key => $regular_rate ) {
$the_rate = ( $regular_rate / 100 ) / $regular_tax_rate;
$net_price = $price - ( $the_rate * $non_compound_price );
$tax_amount = apply_filters( 'woocommerce_price_inc_tax_amount', $price - $net_price, $key, $rates[ $key ], $price );
$taxes[ $key ] += $tax_amount;
}
/**
* 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;
}