Add fee based on specific payment methods in WooCommerce

2021 UPDATE

Note: All payment methods are only available on Checkout page.

The following code will add conditionally a specific fee based on the chosen payment method:

// Add a custom fee (fixed or based cart subtotal percentage) by payment
add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee' );
function custom_handling_fee ( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $chosen_payment_id = WC()->session->get('chosen_payment_method');

    if ( empty( $chosen_payment_id ) )
        return;

    $subtotal = $cart->subtotal;

    // SETTINGS: Here set in the array the (payment Id) / (fee cost) pairs
    $targeted_payment_ids = array(
        'cod' => 8, // Fixed fee
        'paypal' => 5 * $subtotal / 100, // Percentage fee
    );

    // Loop through defined payment Ids array
    foreach ( $targeted_payment_ids as $payment_id => $fee_cost ) {
        if ( $chosen_payment_id === $payment_id ) {
            $cart->add_fee( __('Handling fee', 'woocommerce'), $fee_cost, true );
        }
    }
}

You will need the following to refresh checkout on payment method change, to get it work:

// jQuery - Update checkout on payment method change
add_action( 'woocommerce_checkout_init', 'payment_methods_refresh_checkout' );
function payment_methods_refresh_checkout() {
    wc_enqueue_js( "jQuery( function($){
        $('form.checkout').on('change', 'input[name=payment_method]', function(){
            $(document.body).trigger('update_checkout');
        });
    });");
}

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

How to find a specific payment method ID in WooCommerce Checkout page?

The following will display on checkout payment methods the payment Id just for admins:

add_filter( 'woocommerce_gateway_title', 'display_payment_method_id_for_admins_on_checkout', 100, 2 );
function display_payment_method_id_for_admins_on_checkout( $title, $payment_id ){
  if( is_checkout() && ( current_user_can( 'administrator') || current_user_can( 'shop_manager') ) ) {
      $title .= ' <code style="border:solid 1px #ccc;padding:2px 5px;color:red;">' . $payment_id . '</code>';
  }
  return $title;
}

Code goes in functions.php file of your active child theme (or active theme). Once used, remove it.

enter image description here


Similar answer:

  • Add a custom fee for a specific payment gateway in Woocommerce
  • Add a fee based on shipping method and payment method in Woocommerce
  • Percentage discount based on user role and payment method in Woocommerce

First of all there are some key things we need to understand:

  1. We are going to use the only filter hook which is woocommerce_cart_calculate_fees
  2. In order to get user selected payment method we must retrieve it from user sessions using this method WC()->session->get( 'chosen_payment_method' )
  3. calculate_fees() and calculate_totals() methods are not necessary.
  4. We are going to add the fee using cart method WC()->cart->add_fee() which accepts two parameters – the first one, is the fee description, the second one – fee amount.
  5. And one more thing – we will need a payment method slug, the easiest way to get it described in this tutorial https://rudrastyh.com/woocommerce/add-id-column-to-payment-methods-table.html

Let's go now:

add_action( 'woocommerce_cart_calculate_fees', 'rudr_paypal_fee', 25 );
function rudr_paypal_fee() {

    if( 'paypal' == WC()->session->get( 'chosen_payment_method' ) ) {
        WC()->cart->add_fee( 'PayPal fee', 2 ); // let's add a fee for paypal
    }

}

It would be also great to refresh the checkout every time payment gateway is changed, it is easy to do using this code:

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

That's it. Everything is described in details here as well: https://rudrastyh.com/woocommerce/charge-additional-fees-based-on-payment-gateway.html