WooCommerce 中特定選定付款方式的購物車總計
P粉122932466
P粉122932466 2024-03-21 21:58:57
0
1
272

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

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

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學習者快速成長!