简体   繁体   English

Woocommerce - 如何隐藏小数点后第二位的尾随零?

[英]Woocommerce - How to hide trailing zeros after the 2nd decimal place?


So I have a question I was hoping somebody might be able to help me out with...所以我有一个问题,我希望有人可以帮助我解决...
I have a WordPress website that uses WooCommerce and I'm trying to do something relatively specific.我有一个使用 WooCommerce 的 WordPress 网站,我正在尝试做一些相对具体的事情。 The site has a fair deal of bulk pricing discounts for various products.该网站为各种产品提供大量的批量定价折扣。
Now when it comes to the bulk pricing I kind of need the calculations to be done with four decimal places (ie $0.1234), So I have the WooCommerce settings set so that the number of decimals is 4.现在,当涉及到批量定价时,我需要用小数点后四位(即 0.1234 美元)进行计算,所以我设置了 WooCommerce 设置,以便小数位数为 4。
The thing is, that on a lot of our products we don't have bulk pricing discounts;问题是,我们的很多产品都没有批量定价折扣; in most of these cases the four decimal places are completely unnecessary.在大多数情况下,小数点后四位是完全没有必要的。 (For example: $0.5500 or $12.0000) Is there any way I can trim the trailing the zeros but only if it is after the second decimal place? (例如:0.5500 美元或 12.0000 美元)有什么方法可以修剪尾随的零,但前提是它位于小数点后第二位?
For example, if I had $0.5500 it would be displayed as $0.55 and if I had $12.0000 it would be displayed as $12.00例如,如果我有 0.5500 美元,它将显示为 0.55 美元,如果我有 12.0000 美元,它将显示为 12.00 美元

I know a very similar question was asked a few years ago ( see here ), but unfortunately, the answer that worked in that case does not seem to work for me... That being said, I could be trying to use it the wrong way.我知道几年前有人问过一个非常相似的问题( 见这里),但不幸的是,在那种情况下有效的答案似乎对我不起作用......话虽如此,我可能会尝试错误地使用它方法。 I am not very familiar with using regular expressions in a WordPress "functions.php" file.我不太熟悉在 WordPress“functions.php”文件中使用正则表达式。

Any help at all would be greatly appreciated!任何帮助都将不胜感激! Thanks!谢谢!

Edit:编辑:
At this point I have something along the lines of:在这一点上,我有一些类似的东西:

add_action('wc_price', 'custom_price_disp');
function custom_price_disp($price) {
    $count = null;
    $resultingPrice = preg_replace('/(?<=\d{2})(0+)$/', '', $price, -1, $count);
    return $resultingPrice;
}

But this code does not seem to work.但是这段代码似乎不起作用。 Prices are still displayed the same as they were before.价格仍然显示为与以前相同。

You can use this function in your functions.php child theme, it works fine and removes .00 from every price:您可以在functions.php子主题中使用此函数,它工作正常并从每个价格中删除.00

add_filter('woocommerce_price_trim_zeros', 'wc_hide_trailing_zeros', 10, 1);
function wc_hide_trailing_zeros( $trim ) {
    // set to false to show trailing zeros
    return true;
}

I've not used it myself, but based on the aforementioned wc_price() method's code, it looks like you can take advantage of a filter:我自己没有使用过它,但是根据前面提到的wc_price()方法的代码,看起来您可以利用过滤器:

if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 ) {
  $price = wc_trim_zeros( $price );
}

You should be able to add this to your functions.php :您应该能够将此添加到您的functions.php

add_filter( 'woocommerce_price_trim_zeros', true );

To trigger this behavior.触发此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM