扫码关注官方订阅号
如果有人选择货到付款,我需要在结帐时对最终价格进行四舍五入
我想要实现的总体思路是:
if payment_method == 'cod'{ $cart_subtotal = round($cart_subtotal); }
首先,确保每次用户更改付款方式时都会重新计算购物车总额:
add_action('wp_footer', 'trigger_checkout_refresh_on_payment_method_change'); function trigger_checkout_refresh_on_payment_method_change(){ if ( is_checkout() && ! is_wc_endpoint_url() ) : ?> 根据您想要实现的逻辑,有多种方法可以对价格进行四舍五入,但如果用户选择“货到付款”作为付款方式,则以下是对总额进行四舍五入的最简单方法: add_filter( 'woocommerce_calculated_total', 'round_total_for_specific_payment_methods', 10, 2 ); function round_total_for_specific_payment_methods( $total, $cart ) { $chosen_payment_method = WC()->session->get('chosen_payment_method'); if ( $chosen_payment_method && $chosen_payment_method === 'cod' ) { $total = round( $total ); } return $total; }
根据您想要实现的逻辑,有多种方法可以对价格进行四舍五入,但如果用户选择“货到付款”作为付款方式,则以下是对总额进行四舍五入的最简单方法:
add_filter( 'woocommerce_calculated_total', 'round_total_for_specific_payment_methods', 10, 2 ); function round_total_for_specific_payment_methods( $total, $cart ) { $chosen_payment_method = WC()->session->get('chosen_payment_method'); if ( $chosen_payment_method && $chosen_payment_method === 'cod' ) { $total = round( $total ); } return $total; }
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
首先,确保每次用户更改付款方式时都会重新计算购物车总额: