我想修改购物车显示(以及后来的发票),以便有另一列显示每种产品的税金和税率。我没有找到作为数字的税率的函数或吸气剂,只有名称,$_product->get_tax_class()。我一直在寻找像 $_product->get_tax_rate() 这样的函数,但没有找到。所以我在 woocommerce/templates/cart/cart.php 中写了一个糟糕的解决方法。
在添加的简单部分之后
<th class="product-tax"><?php esc_html_e( 'Tax', 'woocommerce' ); ?></th>
在第 35 行,我从第 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."%)";
这目前有效,但仅限于德国,而且它当然不会存活很长时间。那么,更好的方法是什么?
谢谢!
更新
刚刚找到了一半的解决方案:
$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'] 包含我试图通过计算获得的值。现在只是缺少名称......“19%”或“7%”......