获取并显示 Woocommerce 单品页面上的税率
Get and display the tax rate on Woocommerce single product pages
我正在尝试寻找一种方法来只显示产品的税率(16% 或 7%)。基本上这个想法是应该有一个静态税,比如。
The price includes 16% Taxes
或
The price includes 7% Taxes
因此百分比率应该根据产品的费率动态变化。
知道如何解决这个问题。我找到的所有解决方案都显示完整的税额,但我只需要税率。
税费取决于您的设置,即一种或多种税费 类 以及客户所在地的每种税费 类。在下面,您将找到获取和显示产品税率的正确方法(在 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 文件。已测试并有效。
相关:Get tax rate separately for every cart and order items in Woocommerce
我正在尝试寻找一种方法来只显示产品的税率(16% 或 7%)。基本上这个想法是应该有一个静态税,比如。
The price includes 16% Taxes
或
The price includes 7% Taxes
因此百分比率应该根据产品的费率动态变化。
知道如何解决这个问题。我找到的所有解决方案都显示完整的税额,但我只需要税率。
税费取决于您的设置,即一种或多种税费 类 以及客户所在地的每种税费 类。在下面,您将找到获取和显示产品税率的正确方法(在 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 文件。已测试并有效。
相关:Get tax rate separately for every cart and order items in Woocommerce