在Woocommerce中显示以两位小数舍入的两位小数价格

时间:2018-10-19 11:10:53

标签: php wordpress woocommerce rounding price

在处理一条通知客户有关运输的消息之前,如果他/她又花了XX美元,就可以免费送货。

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

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

如何将其更改为两个小数,如何理解0.1223等于0.10,而0.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
    } 
    }

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

1 个答案:

答案 0 :(得分:2)

您可以通过以下方式将功能round()number_format()结合使用:

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

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


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

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

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