gpt4 book ai didi

php - 获取并在 Woocommerce 单一产品页面上显示税率

转载 作者:行者123 更新时间:2023-12-04 08:48:04 25 4
gpt4 key购买 nike

我正在尝试找到一种方法,如何仅显示产品所具有的税率(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 . '%' );
}

经过测试并有效。您可以将该代码嵌入到可以重用的函数中。


使用示例:

要显示单个产品的税率低于价格(使用 Hook 函数):

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

关于php - 获取并在 Woocommerce 单一产品页面上显示税率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64215704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com