【问题标题】:Displaying two decimal price rounded with one decimal in Woocommerce在 Woocommerce 中显示四舍五入的两位小数价格
【发布时间】:2019-03-24 06:42:32
【问题描述】:

正在处理一条消息,通知客户有关运费,如果他/她再花费 XX 美元,将提供免费送货服务。

我使用了各种钩子来确定它的显示位置(产品页面、购物车、结帐等),但我真正遇到的问题是如何降低成本。

如果客户消费至少 59 美元,它将提供免费送货服务。但如果客户为58.xx 添加产品,则消息将显示为"Spend 0.1223242 dollars more and get free shipping"

如何将其更改为两位小数,如何使其理解 0.1223 等于 0.100.15544 等于 0.2 等等? (我希望这足够清晰易懂)。

这是我使用的代码:

function show_shipping_message() {
    global $woocommerce;  
    $total_cart = $woocommerce->cart->total;
    $limit_free_shipping = 59;

    if ($total_cart != 0 && $total_cart < $limit_free_shipping) { 
    $dif = $limit_free_shipping - $total_cart;

    ?>
    <p class="free-shipping-notice" >
    <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/shipping-icon.png"> <?php _e('TEXT', THEME_TEXT_DOMAIN); ?> <?php echo $dif; ?> <?php _e('dollar, TEXT', THEME_TEXT_DOMAIN); ?> <strong><?php _e('TEXT', THEME_TEXT_DOMAIN); ?></strong>
    </p>

    <?php
    } 
    }

如果有人可以提供帮助,我将不胜感激。提前致谢。如果有人知道如何在从迷你购物车中删除物品后使其自动更新,那将是非常好的。再次,提前致谢。

【问题讨论】:

  • 您可以使用此功能wc_price( $price ); 正确格式化您的价格。

标签: php wordpress woocommerce rounding price


【解决方案1】:

您可以这样使用函数round()number_format()

echo number_format( round(0.1223242, 1), 2 ); // will display: 0.10

您将获得一个价格,该价格将先四舍五入到小数点后一位,然后根据需要以两位小数显示。


您还可以将round()与Woocommerce wc_price()一起使用格式化价格功能:

echo wc_price( round(0.1223242, 1) ); // will display: $0.10

您将获得格式化的价格(四舍五入到小数点后一位),但显示为其他 Woocommerce 价格(显示为货币符号的两位小数)... p>

【讨论】:

  • 感谢您的建议,但这不起作用。我尝试了一个价格为 58 的产品,但没有显示消息。我可能做错了,但我从上面复制了你的代码。
  • @LearningHowtoCode 对不起,它可以工作...试试这行:echo number_format( round(0.1223242, 1), 2 ); // will display: 0.10...它可以工作
  • 我一定是理解错了,你能解释一下我把那个回声放在哪里吗?将此代码与价格为 58.69 的产品一起使用没有任何显示:'

    ">

    '
  • @LearningHowtoCode 这仅用于测试具有多个小数的浮点数的输出,该浮点数将四舍五入为 1 个小数并以 2 个小数显示。现在在 stackOverFlow 中,您不能同时提出多个问题。所以我的问题 => 1 个答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-27
  • 2017-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多