提高 WooCommerce 税收计算精度并保持显示的价格保留两位小数 [英] Increase WooCommerce tax calculations precision and keep displayed prices with two decimals

查看:38
本文介绍了提高 WooCommerce 税收计算精度并保持显示的价格保留两位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Woocommerce 设置中,我设置了 6 位小数,以便获得更准确的税收计算.但是,我需要在前端、电子邮件等中仅显示 2 位小数的所有价格和金额.我发现了两个功能

In Woocommerce settings, I have set 6 decimals in order to get more accurate tax calculation. However, I need all prices and amounts to be displayed with only 2 decimals in frontend, emails etc. I found two functions

add_filter('wc_price_args', 'custom_decimals_price_args', 10, 1);
function custom_decimals_price_args($args) {
$args['decimals'] = 2;
return $args;
}

add_filter( 'wc_get_price_decimals', 'change_prices_decimals', 20, 1 );
function change_prices_decimals( $decimals ){
$decimals = 2;
return $decimals;
}

这些和我应该使用哪个有什么区别?

What is the difference between these and which one should I use?

推荐答案

注意 WC_ROUNDING_PRECISION 常量在 6="https://github.com/woocommerce/woocommerce/blob/5.0.0/includes/class-woocommerce.php#L241" rel="nofollow noreferrer">WC_Woocommercedefine_constants() 方法.

Note that WC_ROUNDING_PRECISION constant is set to 6 in WC_Woocommerce define_constants() method.

这意味着 WooCommerce Tax 计算精度已经设置为 6 位小数.

税收计算精度基于wc_get_rounding_precision() 核心函数 用于WC_Tax:

Tax calculation precision are based on wc_get_rounding_precision() core function used in WC_Tax Class:

function wc_get_rounding_precision() {
    $precision = wc_get_price_decimals() + 2;
    if ( absint( WC_ROUNDING_PRECISION ) > $precision ) {
        $precision = absint( WC_ROUNDING_PRECISION );
    }
    return $precision;
}

如您所见,如果显示的价格十进制值 + 2 小于 WC_ROUNDING_PRECISION 常量,则 WC_ROUNDING_PRECISION 常量优先.但是,由于您希望显示价格保留两位小数,这需要其他一些东西.

As you can see WC_ROUNDING_PRECISION constant is prioritized if the displayed price decimal value + 2 is smaller than WC_ROUNDING_PRECISION constant. But as you want to keep displayed prices with 2 decimals, this requires something else.

所以你不应该增加显示的价格小数,也不应该使用 wc_price_args 或/和 wc_get_price_decimals 钩子来提高税收计算的精度.

So you should not increase displayed price decimals and not use wc_price_args or/and wc_get_price_decimals hooks, to increase precision in tax calculation.

如果6位小数的精度不够而您想获得更高的精度:

If precision of 6 decimals is not enough and you want to get more precision:

获得更精确的税收计算并保持显示的价格保留两位小数的最佳方法是编辑 WordPress wp_config.php 文件并添加以下行 (您可以在其中增加 WC_ROUNDING_PRECISION 常量值随你喜欢,这里的值设置为8例如):

The best way to get more precision on tax calculations and keep displayed prices with 2 decimals is to edit WordPress wp_config.php file and add the following lines (where you can increase WC_ROUNDING_PRECISION constant value as you like, here the value is set to 8 for example):

// Change WooCommerce rounding precision
define('WC_ROUNDING_PRECISION', 8);

这将改变 WC_ROUNDING_PRECISION 常量而不影响显示的价格小数.

This will change WC_ROUNDING_PRECISION constant without affecting displayed price decimals.

这篇关于提高 WooCommerce 税收计算精度并保持显示的价格保留两位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆