How to add additional charges/fees/amount based on the payment gateway (WooCommerce)

Description:

In this blog your are going to learn how to add extra fee based on user selected payment gateway. Good news: You dont need to install any kind of plugin for achieving this functionality.

Adding fee amount to a specific payment gateway.

In this step we are going to add amount to specific payment gateway, in this case we are going to use paypal.


add_action( 'woocommerce_cart_calculate_fees', 'coder618_add_extra_paypal_fee', 25 );
function coder618_add_extra_paypal_fee() {	
	if( 'paypal' == WC()->session->get( 'chosen_payment_method' ) ) {
		WC()->cart->add_fee( 'PayPal fee', 5 );
	}
}

Trigger the checkout update once payment method change

This step we are going to update the front-end once a user change the payment method. we need to done it through javascript.


jQuery( function( $ ) {
	$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
		$( 'body' ).trigger( 'update_checkout' );
	});
});