【问题标题】:Round up cart total Woocommerce汇总购物车总数 Woocommerce
【发布时间】:2018-05-08 20:08:20
【问题描述】:

我需要在我的 Woocommerce 购物车上汇总价格。它四舍五入为 1。例如:

如果购物车的总价是 40.85 美元,我需要购物车显示 41.00 美元。

我试过这个命令,但是这个价格折扣了 0.15,我不知道该怎么做。我在其他网站上找到了这个,但在目录中收集了产品。

add_filter( 'woocommerce_calculated_total', 'custom_calculated_total', 10, 2 );
function custom_calculated_total( $total, $cart ){
    return round( $total - ($total * 0.15), $cart->dp );
}

感谢您的帮助。

【问题讨论】:

    标签: php wordpress woocommerce cart


    【解决方案1】:

    我相信这就是您正在寻找的。这会将您的购物车总数四舍五入到下一美元。您还可以在将商品添加到购物车之前使用 WooCommerce 设置对显示的产品价格进行四舍五入。

    //round cart total up to nearest dollar
    add_filter( 'woocommerce_calculated_total', 'custom_calculated_total' );
    function custom_calculated_total( $total ) {
    $total = round( $total, 1 );
    return ceil($total);
    }
    

    【讨论】:

    • 没问题。很高兴这很有帮助。 :)
    【解决方案2】:

    我正在寻找一些如何在 woocommerce 购物车中舍入最后一位数字的选项,但我找不到它。也许它会帮助某人。这个功能对我有用。只需将此代码添加到您的主题 functions.php 文件中即可。

    //Rounding total price last digit in cart to zero:
    add_filter( 'woocommerce_calculated_total', 'custom_calculated_total' );
    function custom_calculated_total( $total ) {
    $total = round($total / 10) * 10;
    return ($total);
    }
    

    【讨论】:

      【解决方案3】:

      我尝试了 Justin R. 的代码,成功率为 50%。总计已四舍五入,但小计未四舍五入。 Here 我找到了一个代码,两者兼而有之。它使用不同的 Hook “raw_woocommerce_price”。 这是代码:

      // Round price for .99 .01 decimals
      add_filter( 'raw_woocommerce_price', function ( $price ) {
      $decimals = round( $price - floor( $price ), 2 );
      return ( $decimals == 0.01 || $decimals == 0.99 ) ? round( $price ) : $price;
      } );
      

      【讨论】:

        猜你喜欢
        • 2011-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多