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

2024-02-07

我正在尝试找到一种方法,如何仅显示产品所具有的税率(16% 或 7%)。基本上,这个想法是应该有一个静态税。

价格包含16%税费

or

价格含7%税

因此,百分比利率应该根据产品的利率而动态变化。

知道如何解决这个问题。我找到的所有解决方案都显示完整的税额,但我只需要税率。


税费取决于您的设置(一个或多个税级)以及客户所在地的每个税级。您将在下面找到获取和显示产品税率的正确方法(在 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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取并在 Woocommerce 单一产品页面上显示税率 的相关文章

随机推荐