从 WooCommerce 3 中的订单获取税率 [英] Get the tax rate(s) from an order in WooCommerce 3
问题描述
我在购物车中展示我的产品而没有缴纳任何税款,因为我将它们添加到购物车中的小计中.问题是我无法找到稍后在我的订单页面上获取用于订单的税率的方法.所以我正在寻找一种方法来获取订单结账期间使用的税率.费率可能因国家/地区而异.
I'm displaying my products in the cart without any taxes because I'm adding them afterwords to the subtotal sum in the cart. The problem is that I can't find a way to get the tax rate used for an order later on the My Orders page. So I'm looking for a way to get the tax rate used during the checkout for an order. The rate can be different depending on the country.
我拥有的是:
global $order;
$order->get_the_used_tax_rate_somehow();
推荐答案
要从订单中获取税率,您将获取订单税"项.
To get the tax rate(s) from an order you will have to get order "tax" item(s).
您将获得 WC_Order_Item_Tax
受保护的对象,您必须使用 专用的可用方法.
示例代码:
// Get an instance of the WC_Order Object
$order = wc_get_order($order_id);
// Loop through order tax items
foreach( $order->get_items('tax') as $item ){
$name = $item->get_name(); // Get rate code name (item title)
$rate_code = $item->get_rate_code(); // Get rate code
$rate_label = $item->get_label(); // Get label
$rate_id = $item->get_rate_id(); // Get rate Id
$tax_total = $item->get_tax_total(); // Get tax total amount (for this rate)
$ship_total = $item->get_shipping_tax_total(); // Get shipping tax total amount (for this rate)
$is_compound = $item->is_compound(); // check if is compound (conditional)
$compound = $item->get_compound(); // Get compound
}
注意:一个订单可以有多个税率(项目税").
Note: An order can have multiple tax rates (items "tax").
你也可以使用一些相关的WC_Abstract_Order
要获取的 WC_Order
对象上的 a> 方法:
You can also use some related WC_Abstract_Order
methods on the WC_Order
Object to get:
- 获取订单的纳税地点:
$order->get_tax_location()
(array). - 获取订单中商品的所有税种:
$order->get_items_tax_classes()
(array).它将显示标准"税种的空值.
这篇关于从 WooCommerce 3 中的订单获取税率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!