我目前正在构建自定义表单。在此表格中,我要显示结帐时已经存在的国家/地区选择。
这是我的税收设置列表:
这是我的代码:
<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()
函数中得到任何结果。我没有找到可以返回正确利率的函数。你有什么主意我该怎么做吗?
答案 0 :(得分:0)
如果您已配置税率,则它们可能无法覆盖您以任何身份登录的用户。
尝试将$tax_class
和$customer
参数输入您的WC_Tax::get_rates
方法!具体来说,您的示例中德国和/或Österreich所适用的客户和税收类别!
查看实际的功能定义here:
<?php
// class-wc-tax.php
class WC_Tax {
// ...
/**
* Get's an array of matching rates for a tax class.
*
* @param string $tax_class Tax class to get rates for.
* @param object $customer Override the customer object to get their location.
* @return array
*/
public static function get_rates( $tax_class = '', $customer = null ) {
$tax_class = sanitize_title( $tax_class );
$location = self::get_tax_location( $tax_class, $customer );
$matched_tax_rates = array();
if ( count( $location ) === 4 ) {
list( $country, $state, $postcode, $city ) = $location;
$matched_tax_rates = self::find_rates(
array(
'country' => $country,
'state' => $state,
'postcode' => $postcode,
'city' => $city,
'tax_class' => $tax_class,
)
);
}
return apply_filters( 'woocommerce_matched_rates', $matched_tax_rates, $tax_class );
}
}
答案 1 :(得分:0)
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
的所有税率列表,该列表用于按woocommerce_tax_rates
列过滤tax_rate_class
表(嗯,我们总是可以直接查询数据库,但我假设您想使用WooCommerce提供的功能解决问题。另外,“标准费率”没有tax_class
,即tax_rate_class
列为空字符串。
因此,如果要获取每个类别的所有税率,则需要使用WC_Tax::get_rates_for_tax_class( $tax_class )
。但是,如果您确实希望所有税率与类别无关,则需要首先获取所有税种,然后再获取每个类别的所有税率。