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%"...
2020 October Update (removed some mistakes - Tested on WooCommerce 4.5.x version)
I suppose that
woocommerce_cart_item_taxis a custom hook as I didn't find it…The taxes depends on your settings which are one or multiple tax classes and for each tax classes:
Now to handle taxes in a correct way you will use the
WC_Taxobject Class and all related methods. We will use here only country based tax rates:Tested and works.
For orders (pdf invoice) it should be something very similar, where you will need to replace this line:
by something like (where
$itemis the instance of theWC_Order_Item_Productobject):