整理Woocommerce

时间:2018-05-08 20:08:20

标签: php wordpress woocommerce cart

我需要在我的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 );
}

感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

我相信这是你正在寻找的。这将使您的购物车总数达到下一美元。您还可以使用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);
}

答案 1 :(得分:0)

我尝试过 Justin R. 的代码,成功率为 50%。总计四舍五入,但小计未四舍五入。 Here 我找到了一个代码,两者都可以。它使用不同的钩子“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;
} );

答案 2 :(得分:0)

我正在寻找一些如何在 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);
}