从 WooCommerce 3 中的订单获取税率

Mr.*_* Jo 4 php wordpress orders woocommerce tax

我在购物车中展示我的产品没有任何税款,因为我将它们添加到购物车中的小计中。问题是我无法找到稍后在“我的订单”页面上获取用于订单的税率的方法。所以我正在寻找一种方法来获取订单结账期间使用的税率。费率可能因国家/地区而异。

我所拥有的是:

global $order;

$order->get_the_used_tax_rate_somehow();
Run Code Online (Sandbox Code Playgroud)

Loi*_*tec 6

要从订单中获取税率,您必须获取订单“税”项

您将获得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
}
Run Code Online (Sandbox Code Playgroud)

注意:一个订单可以有多个税率(项目“税”)。


也可以WC_Abstract_OrderWC_OrderObject上使用一些相关的方法来获取:

  • 获取订单的纳税地点:$order->get_tax_location() (array)
  • 获取订单中物品的所有税类:$order->get_items_tax_classes() (array)它将显示“标准”税类的空值