I'd like to modify the cart display (and later the invoice) so that there is another column showing the tax and tax rate for each product. I have not found a function or a getter for the tax rate as a number, only the name, with $_product->get_tax_class(). I was looking for a function like $_product->get_tax_rate() but found none. So I wrote a terrible workaround in woocommerce/templates/cart/cart.php.
After the easy part of adding
<th class="product-tax"><?php esc_html_e( 'Tax', 'woocommerce' ); ?></th>
in Line 35, I added from Line 121:
$tax_name = apply_filters( 'woocommerce_cart_item_tax', $_product->get_tax_class(), $cart_item, $cart_item_key );
if ($tax_name == "reduced-tax-rate") $tax_rate= 7; else $tax_rate= 19;
$with_tax = $_product->get_price( $_product );
$without_tax = $with_tax/((100+$tax_rate)/100);
$tax = $with_tax-$without_tax;
$tax = $tax*$cart_item['quantity'];
$tax = number_format($tax, 2, '.', '')." €";
echo " ".$tax." (".$tax_rate."%)";
This works for now, but only in Germany, and it would of course not survive for a very long time. So, what is the better way to do this?
Thanks!
UPDATE
Just found half of the solution:
$tax_name = apply_filters( 'woocommerce_cart_item_tax', $_product->get_tax_class(), $cart_item, $cart_item_key );
if ($tax_name == "reduced-tax-rate") $tax_rate= 7; else $tax_rate= 19;
echo " ".$cart_item['line_subtotal_tax']." (".$tax_rate."%)";
$cart_item['line_subtotal_tax'] holds the value I was trying to get by calculation. Now just the name is missing..."19%" or "7%"...