如何在 WooCommerce 中获得所有可用的税率? [英] How can I get all available tax rates in WooCommerce?
问题描述
我目前正在构建自定义表单.在此表单中,我想显示结帐时已存在的国家/地区选择.
I'm currently building a custom form. In this form I want to display the country select which is already present at the checkout.
这是我的税务设置列表:
This is my tax settings list:
这是我的代码:
<select class="country-select">
<?php
foreach ( WC_Tax::get_rates() as $rate ) {
echo '<option value="' . $rate->tax_rate . '">' . $rate->country . '</option>';
}
?>
</select>
我期待以下选项:
<option value="19">Deutschland</option>
<option value="20">Österreich</option>
问题是我没有从 get_rates()
函数返回任何结果.我发现没有任何函数可以返回正确的费率.你知道我如何完成这项工作吗?
The problem is that I don't get any results back from the get_rates()
function. I've found no function which can return me the correct rates. Do you have any idea how I can get this done?
推荐答案
TL;DR
$all_tax_rates = [];
$tax_classes = WC_Tax::get_tax_classes(); // Retrieve all tax classes.
if ( !in_array( '', $tax_classes ) ) { // Make sure "Standard rate" (empty class name) is present.
array_unshift( $tax_classes, '' );
}
foreach ( $tax_classes as $tax_class ) { // For each tax class, get all rates.
$taxes = WC_Tax::get_rates_for_tax_class( $tax_class );
$all_tax_rates = array_merge( $all_tax_rates, $taxes );
}
说明:
据我所知,您只能获得给定特定 tax_class
的所有税率的列表,用于按 tax_rate_class 过滤
列(好吧,我们总是可以直接查询数据库,但我假设您想使用 WooCommerce 提供的函数来解决问题).此外,标准费率"没有 woocommerce_tax_rates
表tax_class
,即.tax_rate_class
列是一个空字符串.
As far as I know, you can only get a list of all tax rates given a specific tax_class
, which is used to filter woocommerce_tax_rates
table by tax_rate_class
column (well, we can always query the database directly, but I'm assuming you want to solve the problem using functions provided by WooCommerce). Also, "Standard rates" have no tax_class
, ie. tax_rate_class
column is an empty string.
因此,如果您想获取每个类别的所有税率,则需要使用 WC_Tax::get_rates_for_tax_class( $tax_class )
.但如果你真的想要不分等级的所有税率,则需要先获取所有税种,然后获取每个等级的所有税率.
So if you want to get all tax rates per class, you need to use WC_Tax::get_rates_for_tax_class( $tax_class )
. But if you really want all tax rates regardless of class, you need to get all tax classes first, then get all tax rates per class.
这篇关于如何在 WooCommerce 中获得所有可用的税率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!