在 WooCommerce 中获取产品税率的标签

Get the label of a product tax rate in WooCommerce

我需要在 WooCommerce 中找出税率标签。

用下面的代码我可以找出税率:

$taxclass = $product_variation->get_tax_class();
 $tax_rates = WC_Tax::get_rates( $taxclass );
if (!empty($tax_rates)) {
    $tax_rate = reset($tax_rates);
    $tax_rate_info = (int)$tax_rate['rate'];
}
else {
}

很遗憾,我无法获得该税率的确切标签。我尝试了以下方法:

 $tax_labels = WC_Tax::get_rate_label( $taxclass );

我需要以某种方式找出税率的 ID 并将该 ID 传递给 get_rate_label() 以便它起作用。有人可以帮助如何找到税率 ID。

您可以使用数组键label获取特定税率的标签名称,如下所示:

$variation_tax_class  = $product_variation->get_tax_class();
$variation_tax_rates  = WC_Tax::get_rates( $variation_tax_class );

foreach( $variation_tax_rates as $rate_id => $rate ) {
    if ( ! empty($rate) ) {
        $rate_percent  = (int) $rate['rate'];
        $rate_label    = $rate['label']; // <== HERE is the label name of the tax rate
        $rate_shipping = $rate['shipping'];
        $rate_compound = $rate['compound'];
    }
}