首页  >  问答  >  正文

WooCommerce 中特定选定付款方式的购物车总计

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

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

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

P粉122932466P粉122932466185 天前277

全部回复(1)我来回复

  • P粉554842091

    P粉5548420912024-03-22 21:21:25

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

    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;
    }
    

    回复
    0
  • 取消回复