WooCommerce 中特定选定付款方式的购物车总计
P粉122932466
P粉122932466 2024-03-21 21:58:57
[PHP讨论组]

如果有人选择货到付款,我需要在结帐时对最终价格进行四舍五入

我想要实现的总体思路是:

if payment_method == 'cod'{
    $cart_subtotal = round($cart_subtotal);
}

P粉122932466
P粉122932466

全部回复(1)
P粉554842091

首先,确保每次用户更改付款方式时都会重新计算购物车总额:

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;
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号