2

我想修改购物车显示(以及后来的发票),以便有另一列显示每种产品的税金和税率。我没有找到作为数字的税率的函数或吸气剂,只有名称,$_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%”......

4

1 回答 1

2

2020 年 10 月更新 (删除了一些错误 - 在 WooCommerce 4.5.x 版本上测试)

我想这woocommerce_cart_item_tax是一个自定义钩子,因为我没有找到它……</p>

税费取决于您的设置,即一个或多个税级以及每个税级:

  • 所有国家或/和国家
  • 一个国家的所有州或/和各州
  • 一个国家的所有邮政编码或/和按邮政编码
  • 一种或多种税率。
  • (和其他设置)

现在要以正确的方式处理税收,您将使用对象WC_Tax类和所有相关方法。我们将在这里仅使用基于国家/地区的税率:

 // Getting an instance of the WC_Tax object
 $wc_tax = new WC_Tax();

// Get User billing country
$billing_country = WC()->customer->get_billing_country();

// Get the item tax class (your code)
$tax_class = apply_filters( 'woocommerce_cart_item_tax', $_product->get_tax_class(), $cart_item, $cart_item_key );

// Get the related Data for Germany and "default" tax class
$tax_data = $wc_tax->find_rates( array('country' => $billing_country, 'tax_class' => $tax_class ) );

// Get the rate (percentage number)
if( ! empty($tax_data) ) {
    $tax_rate = reset($tax_data)['rate'];

    // Display it
    printf( '<span class="tax-rate">' . __("The tax rate is %s", "woocommerce") . '</span>',  $tax_rate . '%' );
}

测试和工作。


对于订单 (pdf 发票),它应该非常相似,您需要替换此行:

// Get the item tax class (your code)
$tax_class = apply_filters( 'woocommerce_cart_item_tax', $_product->get_tax_class(), $cart_item, $cart_item_key );

通过类似的东西(对象$item的实例在哪里WC_Order_Item_Product):

// Get the WC_Product Object instance
$_product = $item->get_product();

// Get the item tax class
$tax_class = $_product->get_tax_class();
于 2018-07-16T09:53:45.240 回答