猿问

将百分比和固定成本的自定义费用添加到WooCommerce中的特定支付网关

在Woo商务中,我需要对特定的支付网关应用自定义处理费。


百分比成本的自定义处理费和每个固定成本的自定义处理费。


我有这2段代码:


A) 成本百分比 - 函数


/************************************************************/

/*  PERCENTACE COST 

**/

// Add a custom fee based o cart subtotal

// Add a custom fee based o cart subtotal


add_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 subtotal

add_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 );

    }

}



LEATH
浏览 76回答 1
1回答

开满天机

您发布的2个示例之间的差异非常小。您只需使用:if ( 'cod' == $payment_method ) {&nbsp; &nbsp; $surcharge = $cart->subtotal * 0.025;&nbsp; &nbsp; $fee = 0.31;&nbsp; &nbsp; $together = $surcharge + $fee;&nbsp; &nbsp; $cart->add_fee( 'Plus Cost', $together, true );}
随时随地看视频慕课网APP
我要回答