WooCommerce 中特定选定付款方式的购物车总计
P粉122932466
P粉122932466 2024-03-21 21:58:57
0
1
273

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

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

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() ) :
    ?>

    sssccc

    

根据您想要实现的逻辑,有多种方法可以对价格进行四舍五入,但如果用户选择“货到付款”作为付款方式,则以下是对总额进行四舍五入的最简单方法:

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;
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!