每个税级税率都是在后端按位置设置定义的……因此它们取决于客户所在的位置。税率可以以不同的方式应用于产品和运输。
对于每个税种,您需要根据允许的购买地点或区域,按国家或地区设置税率(参见下面的示例)。
使用以下内容显示客户的可用税率(基于其位置):
$location = array(
'country' => WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->customer->get_billing_country(),
'state' => WC()->customer->get_shipping_state() ? WC()->customer->get_shipping_state() : WC()->customer->get_billing_state(),
'city' => WC()->customer->get_shipping_city() ? WC()->customer->get_shipping_city() : WC()->customer->get_billing_city(),
'postcode' => WC()->customer->get_shipping_postcode() ? WC()->customer->get_shipping_postcode() : WC()->customer->get_billing_postcode(),
);
$output = array(); // Initialiizing (for display)
// Loop through tax classes
foreach ( wc_get_product_tax_class_options() as $tax_class => $tax_class_label ) {
// Get the tax data from customer location and product tax class
$tax_rates = WC_Tax::find_rates( array_merge( $location, array( 'tax_class' => $tax_class ) ) );
// Finally we get the tax rate (percentage number) and display it:
if( ! empty($tax_rates) ) {
$rate_id = array_keys($tax_rates);
$rate_data = reset($tax_rates);
$rate_id = reset($rate_id); // Get the tax rate Id
$rate = $rate_data['rate']; // Get the tax rate
$rate_label = $rate_data['label']; // Get the tax label
$is_compound = $rate_data['compound']; // Is tax rate compound
$for_shipping = $rate_data['shipping']; // Is tax rate used for shipping
// set for display
$output[] = '<li class="tax-rate '.$tax_class.'">'.$tax_class_label.': <strong>'.$rate.'</strong> <small>(label: '.$rate_label.' | id: ' . $rate_id . ')</small></li>';
}
}
// Display
if ( ! empty($output) ) {
echo '<div><h4>' . __("Available Tax rates") . '</h4><ul>' . implode('', $output) . '</ul></div>';
}
你会得到类似的东西:
相关:Using WC_Tax::get_tax_classes() to get all WooCommerce tax-classes
指定位置的用法
您似乎想按照评论中的要求在后端使用它,因此您需要指定一个位置才能获得该特定位置的税率。
例如,要获得为法国国家/地区设置的税率(“FR”国家代码),您需要将 $location 数组替换为:
$location = array( 'country' => 'FR', 'state' => '', 'city' => '', 'postcode' => '' );
如果没有指定国家,你将使用一个空值的数组,例如:
$location = array( 'country' => '', 'state' => '', 'city' => '', 'postcode' => '' );