在Woo商務中,我需要對特定的支付網關應用自定義處理費。百分比成本的自定義處理費和每個固定成本的自定義處理費。我有這2段代碼:A) 成本百分比 - 函數/************************************************************//* PERCENTACE COST **/// Add a custom fee based o cart subtotal// Add a custom fee based o cart subtotaladd_action( 'woocommerce_cart_calculate_fees', 'custom_percentage_fee', 20, 1 );function custom_percentage_fee ( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return; // Only checkout page $payment_method = WC()->session->get( 'chosen_payment_method' ); if ( 'cod' == $payment_method ) { $surcharge = $cart->subtotal * 0.025; $cart->add_fee( 'Percentage Cost', $surcharge, true ); }}// jQuery - Update checkout on methode payment change add_action( 'wp_footer', 'custom_checkout_jqscript' );function custom_checkout_jqscript() { if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return; // Only checkout page ?> <script type="text/javascript"> jQuery( function($){ $('form.checkout').on('change', 'input[name="payment_method"]', function(){ $(document.body).trigger('update_checkout'); }); }); </script> <?php}結果前端B) 固定成本 - 功能/************************************************************//* FIXED COST **/// Add a custom fee based o cart subtotaladd_action( 'woocommerce_cart_calculate_fees', 'custom_fixed_fee', 10, 1 );function custom_fixed_fee ( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; if ( 'cod' === WC()->session->get('chosen_payment_method') ) { $fee = 0.31; $cart->add_fee( 'Fixed Cost', $fee, true ); }}
1 回答

開滿天機
TA貢獻1786條經驗 獲得超13個贊
您發布的2個示例之間的差異非常小。您只需使用:
if ( 'cod' == $payment_method ) {
$surcharge = $cart->subtotal * 0.025;
$fee = 0.31;
$together = $surcharge + $fee;
$cart->add_fee( 'Plus Cost', $together, true );
}
- 1 回答
- 0 關注
- 86 瀏覽
添加回答
舉報
0/150
提交
取消