Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

php - Additional paypal fee on Woocommerce checkout page

In Woocommerce, we are trying to add an additional cost to the order when is purchased via Paypal payment gateway.

We did it changing the price that is sent to Paypal this way:

add_filter('woocommerce_paypal_args', 'addition_pay');

function addition_pay($paypal_args){
        $new_value=$paypal_args['amount_1']+10;
        $paypal_args['amount_1']=$new_value;
        return $paypal_args;
} 

It works, but the problem is after the payment process, this additional cost is not reflected in Orders and email notifications.

Is this can be solved in some way? Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should better add a fee based on payment gateway (here Paypal for you), like in the following:

// Add a fee of 10.00 when Paypal is chosen
add_action( 'woocommerce_cart_calculate_fees', 'custom_paypal_additional_fee', 20, 1 );
function custom_paypal_additional_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if( WC()->session->get( 'chosen_payment_method' ) == 'paypal' )
        $cart->add_fee( __( 'Paypal fee', 'woocommerce' ), 10.00 );
}

// Add the information on checkout paypal text gateways section
add_filter('woocommerce_gateway_icon', 'custom_paypal_gateway_text', 20, 2 );
function custom_paypal_gateway_text( $html, $gateway_id ) {
    if( $gateway_id == 'paypal' )
        $html .= ' <small class="paypal-fee">(+ '.wc_price(10.00).')</small>';

    return $html;
}

// Enable ajax update checkout event when choosing a gateway to refresh the fee
add_action('wp_footer', 'payment_gateways_update_checkout_event' );
function payment_gateways_update_checkout_event() {
    ?>
    <script type="text/javascript">
        (function($){
            $('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
                var t = { updateTimer: !1,  dirtyInput: !1,
                    reset_update_checkout_timer: function() {
                        clearTimeout(t.updateTimer)
                    },  trigger_update_checkout: function() {
                        t.reset_update_checkout_timer(), t.dirtyInput = !1,
                        $(document.body).trigger("update_checkout")
                    }
                };
                $(document.body).trigger('update_checkout')
            });
        })(jQuery);
    </script>
    <?php
}

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...