税费取决于您的设置(一个或多个税级)以及客户所在地的每个税级。您将在下面找到获取和显示产品税率的正确方法(在 WooCommerce 上设置):
// Get the WC_Product Object
global $product;
if( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_id() );
}
// Get an instance of the WC_Tax object
$tax_obj = new WC_Tax();
// Get the tax data from customer location and product tax class
$tax_rates_data = $tax_obj->find_rates( 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_city() ? WC()->customer->get_shipping_city() : WC()->customer->get_billing_city(),
'tax_class' => $product->get_tax_class()
) );
// Finally we get the tax rate (percentage number) and display it:
if( ! empty($tax_rates_data) ) {
$tax_rate = reset($tax_rates_data)['rate'];
// The display
printf( '<span class="tax-rate">' . __("The price includes %s Taxes", "woocommerce") . '</span>', $tax_rate . '%' );
}
经过测试并有效。您可以将该代码嵌入到可以重用的函数中。
使用示例:
显示单一产品的税率低于价格(使用挂钩函数):
add_action( 'woocommerce_single_product_summary', 'display_tax_rate_on_single_product', 15 );
function display_tax_rate_on_single_product() {
global $product; // The current WC_Product Object instance
// Get an instance of the WC_Tax object
$tax_obj = new WC_Tax();
// Get the tax data from customer location and product tax class
$tax_rates_data = $tax_obj->find_rates( 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_city() ? WC()->customer->get_shipping_city() : WC()->customer->get_billing_city(),
'tax_class' => $product->get_tax_class()
) );
// Finally we get the tax rate (percentage number) and display it:
if( ! empty($tax_rates_data) ) {
$tax_rate = reset($tax_rates_data)['rate'];
// The display
printf( '<span class="tax-rate">' . __("The price includes %s Taxes", "woocommerce") . '</span>', $tax_rate . '%' );
}
}
代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
有关的:单独获取 Woocommerce 中每个购物车和订单商品的税率 https://stackoverflow.com/questions/51357197/get-tax-rate-separately-for-every-cart-and-order-items-in-woocommerce/51359090#51359090