在Woocommerce中分别获取每个购物车和订单项的税率 [英] Get tax rate separately for every cart and order items in Woocommerce
问题描述
我想修改购物车的显示(以及后来的发票),以便有另一列显示每种产品的税率和税率.我没有找到带有数字的税率函数或吸气剂,只是带有$ _product-> get_tax_class()的名称.我在寻找类似$ _product-> get_tax_rate()的函数,但没有找到.所以我在woocommerce/templates/cart/cart.php中写了一个可怕的解决方法.
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.
简单的添加之后
<th class="product-tax"><?php esc_html_e( 'Tax', 'woocommerce' ); ?></th>
在35行中,我是从121行添加的:
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?
谢谢!
更新
仅找到解决方案的一半:
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']保存我试图通过计算获取的值.现在只剩下名字了……"19%"或"7%" ...
$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年10月更新 (已删除一些错误-在WooCommerce 4.5.x版本上进行了测试)
我想 woocommerce_cart_item_tax
是一个自定义钩子,因为我没有找到它……
I suppose that woocommerce_cart_item_tax
is 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_Tax
object Class and all related methods. We will use here only country based tax rates:
// 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 . '%' );
}
经过测试,可以正常工作.
Tested and works.
对于订单 (pdf发票)应该非常相似,您需要替换以下行:
For orders (pdf invoice) it should be something very similar, where you will need to replace this line:
// 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
对象的实例)之类的
by something like (where $item
is the instance of the WC_Order_Item_Product
object):
// Get the WC_Product Object instance
$_product = $item->get_product();
// Get the item tax class
$tax_class = $_product->get_tax_class();
这篇关于在Woocommerce中分别获取每个购物车和订单项的税率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!