Add a custom fee for a specific payment gateway in Woocommerce

前端 未结 1 702
名媛妹妹
名媛妹妹 2020-12-06 21:21

How can I add a percentage to the total amount when choosing a payment (credit card)?

For example: If the customer pays for cash on delivery, then the base price, an

相关标签:
1条回答
  • 2020-12-06 22:15

    Try the following revisited code instead that should do the trick:

    // Add a custom fee based o cart subtotal
    add_action( 'woocommerce_cart_calculate_fees', 'custom_fee_for_tinkoff', 20, 1 );
    function custom_fee_for_tinkoff ( $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 ( 'tinkoff' == $payment_method ) {
            $surcharge = $cart->subtotal * 0.025;
            $cart->add_fee( 'Комиссия банка', $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
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    0 讨论(0)
提交回复
热议问题