【问题标题】:Get the tax rate(s) from an order in WooCommerce 3从 WooCommerce 3 中的订单中获取税率
【发布时间】:2020-01-11 15:18:36
【问题描述】:
我在购物车中展示我的产品时没有任何税费,因为我将它们添加到购物车中的小计中。问题是我无法找到稍后在 我的订单 页面上获取用于订单的税率的方法。所以我正在寻找一种方法来获取结账时使用的税率。费率可能因国家/地区而异。
我拥有的是:
global $order;
$order->get_the_used_tax_rate_somehow();
【问题讨论】:
标签:
php
wordpress
woocommerce
orders
tax
【解决方案1】:
要从订单中获取税率,您需要获取订单“税”项。
您将获得WC_Order_Item_Tax 受保护对象,并且您必须使用dedicated available methods。
示例代码:
// 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
}
注意:一个订单可以有多个税率(项目“税”)。
也可以在WC_Order对象上使用一些相关的WC_Abstract_Order方法来获取:
- 获取订单的纳税地点:
$order->get_tax_location()(array)。
- 获取订单中项目的所有税类:
$order->get_items_tax_classes()(array)。 它将为“标准”税类显示一个空值。