如何在 WooCommerce 中获取订单税详细信息和税率? [英] How to get order tax details and rates in WooCommerce?
问题描述
我正在尝试从订单中获取插件中自定义变量的税收百分比.当然,我可以通过 $order-> 请求大量数据.get_ ...
,但我找不到获取税率的方法(例如21"-> 21%).
I'm trying to get the tax percentage for a custom variable in a plugin from an order. Of course I could request a lot of data via $order-> get_ ...
, but I cannot find a method to get the tax percentage (like for example '21' -> 21%).
有人想办法让这一切变得简单吗?
Anyone have an idea to make this simple?
推荐答案
您将有获得订单税项,这将为您提供WC_Order_Item_Tax
使用 WC_Order_Item_Tax
可用方法 如:
You will have to get order tax item(s) which will give you an array of WC_Order_Item_Tax
Objects which protected properties are accessible using WC_Order_Item_Tax
available methods like:
// Get the the WC_Order Object from an order ID (optional)
$order = wc_get_order( $order_id );
foreach($order->get_items('tax') as $item_id => $item ) {
$tax_rate_id = $item->get_rate_id(); // Tax rate ID
$tax_rate_code = $item->get_rate_code(); // Tax code
$tax_label = $item->get_label(); // Tax label name
$tax_name = $item->get_name(); // Tax name
$tax_total = $item->get_tax_total(); // Tax Total
$tax_ship_total = $item->get_shipping_tax_total(); // Tax shipping total
$tax_compound = $item->get_compound(); // Tax compound
$tax_percent = WC_Tax::get_rate_percent( $tax_rate_id ); // Tax percentage
$tax_rate = str_replace('%', '', $tax_percent); // Tax rate
echo '<p>Rate id: '. $tax_rate_id . '<br>'; // Tax rate ID
echo 'Rate code: '. $tax_rate_code . '<br>'; // Tax code
echo 'Tax label: '. $tax_label . '<br>'; // Tax label name
echo 'Tax name: '. $tax_name . '<br>'; // Tax name
echo 'Tax Total: '. $tax_total . '<br>'; // Tax Total
echo 'Tax shipping total: '. $tax_ship_total . '<br>'; // Tax shipping total
echo 'Tax compound: '. $tax_compound . '<br>'; // Tax shipping total
echo 'Tax percentage: '. $tax_percent . '<br>'; // Tax shipping total
echo 'Tax rate: '. $tax_rate . '</p>'; // Tax percentage
}
经过测试并有效
您还可以使用一些WC_Tax
可用的方法 就像在代码中
You can also use some WC_Tax
available methods like in the code
请注意,您可以在一个订单中许多具有不同税率的税"商品.运费也可以有自己的税率.费用也是如此,因为您可以设置税级.另请记住,税率取决于客户所在地.
Note that you can have many 'tax" items with different tax rates in an order. Shipping can have its own tax rate too. Same thing for fees, as you can set a tax class. Also remember that taxes rates are based on customer location.
<小时>
最后你也可以使用一些WC_Abstract_Order
获取税款详细信息的方法,例如:
Finally you can also use some WC_Abstract_Order
methods to get taxes amounts details like:
// Get the the WC_Order Object from an order ID (optional)
$order = wc_get_order( $order_id );
$discount_tax = $order->get_discount_tax();
$shipping_tax_total = $order->get_shipping_tax();
$order_total_tax = $order->get_total_tax();
$cart_total_tax = $order->get_cart_tax();
$formatted_tax_totals = $order->get_tax_totals();
这篇关于如何在 WooCommerce 中获取订单税详细信息和税率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!