首頁  >  問答  >  主體

WooCommerce 中特定選定付款方式的購物車總計

如果有人選擇貨到付款,我需要在結帳時對最終價格進行四捨五入

我想要實現的整體想法是:

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

P粉122932466P粉122932466185 天前276

全部回覆(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
  • 取消回覆