在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 关注
- 101 浏览
添加回答
举报
0/150
	提交
		取消
	