每个税级税率都是在后端通过位置设置定义的……因此它们取决于客户位置。产品和运输的税率可能有所不同。
对于每个税种,您需要按国家或运输区域设置税率,具体取决于允许的购买地点或区域(见下面的例子).
使用以下内容显示客户的可用税率(根据其位置):
$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>';
}
你会得到类似的东西:
有关的:使用 WC_Tax::get_tax_classes() 获取所有 WooCommerce 税级 https://stackoverflow.com/questions/66196547/using-wc-taxget-tax-classes-to-get-all-woocommerce-tax-classes/66196816#66196816
指定位置的用法
由于您似乎想在后端使用它,正如您在评论中所询问的那样,您需要指定一个位置才能获取该特定位置的税率。
例如,要获取法国国家设置的税率(“FR”国家代码)你将替换$location
数组依据:
$location = array( 'country' => 'FR', 'state' => '', 'city' => '', 'postcode' => '' );
如果没有指定国家/地区,您将使用带有空值的数组,例如:
$location = array( 'country' => '', 'state' => '', 'city' => '', 'postcode' => '' );