WooCommerce: Add Checkout Fee for a Payment Gateway (e.g. PayPal)

Here’s a simple PHP snippet to add a fee to the checkout for every payment or for a specific payment gateway. Please do remember that for certain payment gateways such as PayPal, adding checkout fees is currently against their Terms of Service so make sure to check this first. As usual, this needs to be copied and pasted in your child theme’s functions.php file. Enjoy!

Add fee/surcharge to the WooCommerce Cart/Checkout

PHP Snippet #1: Add fee to checkout – WooCommerce


/**
 * @snippet       WooCommerce Add fee to checkout
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=17446
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.4.3
 */

add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee' );

function bbloomer_add_checkout_fee() {

  // Edit "Fee" and "5" below to control Label and Fee Amount
  WC()->cart->add_fee( __('Fee', 'woocommerce'), 5 );

}

PHP Snippet #2: Add fee to checkout for a specific payment gateway – WooCommerce


/**
 * @snippet       WooCommerce Add fee to checkout for a gateway ID
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=17446
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.4.3
 */

// Part 1: assign fee

add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee_for_gateway' );

function bbloomer_add_checkout_fee_for_gateway() {

  $chosen_gateway = WC()->session->chosen_payment_method;
 
  if ( $chosen_gateway == 'cheque' ) {

  // Note: edit "Fee" and "5" below to control Label and Fee Amount
  WC()->cart->add_fee( __('Fee', 'woocommerce'), 5 );

  }

}

// Part 2: reload checkout on payment gateway change

add_action( 'woocommerce_review_order_before_payment', 'bbloomer_refresh_checkout_on_payment_methods_change' );

function bbloomer_refresh_checkout_on_payment_methods_change(){
    ?>
    <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                $('body').trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

40 thoughts on “WooCommerce: Add Checkout Fee for a Payment Gateway (e.g. PayPal)

  1. This is fantastic. The only thing I can’t figure out is what to I set the “Chosen_Gateway” to if I want to apply a fee when using Square. Can you help?

    1. Hello Matthew, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Nice post, but how can i add a fix feed only if the total cart amount is under 50 euros?

    1. Hello Florian, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Hi,

    Great snippet!
    How can i add the ‘fee’ outcome on the invoice?

    Thanks in advance.

    Regards,
    David

    1. Hi David, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. Even though not documented at https://docs.woocommerce.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/ it turns out WooCommerce passes the cart into the woocommerce_cart_calculate_fees function as a parameter. Hence, you could do something like this:

    function add_checkout_fee_for_paypal($cart)
    {
        global $woocommerce;
    
        if (is_admin() &amp;&amp; !defined('DOING_AJAX')) {
            return;
        }
    
        $chosen_gateway = $woocommerce-&gt;session-&gt;chosen_payment_method;
    
        // it's either 'paypal' or 'ppec_paypal' for PayPal Express Checkout
        if (strpos($chosen_gateway, 'paypal') !== false) {
            $percentage = 0.029;
            $surcharge = (($cart-&gt;cart_contents_total + $cart-&gt;shipping_total) * $percentage) + 0.30;
            // 'Fee' is the GUI label
            $cart-&gt;add_fee(__('Fee', 'woocommerce'), $surcharge);
        }
    }
    
    1. Nice!

  5. For the PHP Snippet #1, how can I make it optional? I mean the customer can choose to tick a checkbox to add on the additional fee.

    1. Hi Wilfred, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. Hi,

    I have added the above snippet for COD method and it was working fine but the tax amount is not updating when the cart amount gets updated.

    For Example if my cart amount is 103 inclusive of 3% tax.

    Then when I add Rs 50 for COD method

    the cart amount goes to 153 but the tax amount is still Rs 3. Any solution for this?

    1. Hello Toukir, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. Hello,
    Thanks for the snippet… Wondering how I can make it that it charged per item?

    Any idea how…

    Cheers

    1. Hey Juanito, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  8. Hi, thanks a lot for your awesome work and snippets! Works fine on my test website, on which i’ve added the Coinbase plugin to let customers pay by crypto (and adding a handling fee of USD2 in that case). Any easy way to add the fee based on a % of the basket?

    Best

    1. Hey Topraf, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  9. Hi Rodolfo.
    I tried your snippet, but I encountered an issue.
    Say that you have two shipping method, free_shipping and local_pickup.
    If I choose COD payment method, ok your snippet add a fee correctly. But if I switch from free_shipping to local_pickup… well, there’s no way to update cart by removing unwanted additional fee.
    Do you think there is a way to clear it?

    1. Hey Alessandro – thanks so much for your comment! Well, this snippet applies a fee when you switch payment methods, not when you change shipping rates – it will need to be customized accordingly ๐Ÿ™‚

  10. Hi Rodolfo,
    The second snippet for a specific payment gateway isn’t working. The cart isn’t updated if I choose another payment gateway. Is it because the payment gateways are after the cart?

    I found this and it works for me. The cart is updated when I choose a different payment gateway. Is this the right way to do it or…. ?

    function woocommerce_custom_fee( ) {
     
    	if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() )
    		return;
     
    	$chosen_gateway = WC()->session->chosen_payment_method;
    	$fee = 2;
    
    	if ( $chosen_gateway == 'cheque' ) { //test with paypal method
    		WC()->cart->add_fee( 'Fee', $fee, false, '' );
    	}
    }
    add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_fee' );
     
    function cart_update_script() {
        if (is_checkout()) :
        ?>
        <script>
    		jQuery( function( $ ) {
     
    			// woocommerce_params is required to continue, ensure the object exists
    			if ( typeof woocommerce_params === 'undefined' ) {
    				return false;
    			}
     
    			$checkout_form = $( 'form.checkout' );
     
    			$checkout_form.on( 'change', 'input[name="payment_method"]', function() {
    					$checkout_form.trigger( 'update' );
    			});
     
     
    		});
        </script>
        <?php
        endif;
    }
    add_action( 'wp_footer', 'cart_update_script', 999 );
    

    Kind regards!

    1. Hey Mieke, thanks for your comment. You’re totally right, so I revised my snippet and from now on you should use the latest version ๐Ÿ™‚

  11. Hello. When I select a certain country and choose COD where the fee is set, it is correctly showed in cart.
    Then, when I change a country for which isn’t available COD and so the default method (card payment) is automatically set, the fee for COD still stays in cart, although it isn’t calculated in total price when I try to pay. Don’t you know why? Thanks

    1. Hey Rad, thanks for your comment! You might need to “Ajaxify” the snippet in order to refresh the checkout fees when the country is selected. Unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  12. Sharing this with anyone who needs it.

    This is a customised code for those who are using PayPal Express Checkout.

    To know the gateway code, on checkout simply right on the payment gateway, click Inspect (Chrome) and
    just get whatever it is after the _method then edit: if ( $chosen_gateway == ‘CODE HERE’ )

    Hope this helps ๐Ÿ™‚

    
    add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee_for_gateway' );
     
    function bbloomer_add_checkout_fee_for_gateway() {
      
      global $woocommerce;
      
      $chosen_gateway = $woocommerce->session->chosen_payment_method;
       
      if ( $chosen_gateway == 'ppec_paypal' ) {
       
       
        $percentage = 0.05;
        $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;    
      $woocommerce->cart->add_fee( 'PayPal Fee', $surcharge, true, '');
      
      }
      
    }
    
    
    1. Very nice, thanks a lot ๐Ÿ™‚

    2. ppec_paypal is not wroking with PayPal Express Checkout for WooCommerce developed by wpgateways . Any Idea ?

  13. Hi Rodolfo, thank you so much for sharing great snippets with us. I have been following your blog for a while and perhaps cut-copy-paste some of them in my work ๐Ÿ˜›

    So I wrote a piece of code which adds a custom fee depending on the distance between seller’s & buyer’s locations. It was working great until woocommerce 3.1.2. Once I have update to 3.2.1 or 3.2.4, it’s no longer adding any fee.

    Here’s the code: (only if you have time)

    Just tell me if I did anything wrong or its the latest update that causes the code not working.
    Thank you very much for your time.

    1. Mahdy, thanks so much for your comment! Unfortunately this is custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R

  14. Hi
    Great snippets, great work!
    I am trying to find a way to hide a shipping method in the cart, when a flat rate postcode has been input’d by the user.
    For a Guest shopper they are shown a flat rate shipping fee.
    I have set up postcodes in the flat rate method.
    Once the matching postcode is entered the new rate appears.
    However the original cheaper rate still shows and is selectable.
    It is this cheaper rate I need to remove.

    If its not possible to unset this other rate on postcode entry, could I add a fee and take that to the checkout?
    I hope this is not too confusing and that you may be able to point me in the right direction.
    Cheers
    Andy

    1. Andy, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  15. Hello there,

    I cant seem to get the snippet working with a theme I’ve purchased. When I add the code into my functions.php it just kills the site – with a 500 error.

    Any chance I could give you access to a clone of my site and you could adjust it?

    1. Hello Ray! I just added the code to my test website and it doesn’t give error – try again ๐Ÿ™‚ Unfortunately I can’t offer free support to blog readers, thanks for your understanding!

  16. hi there,
    THANK YOU FOR YOUR BLOG, it really helped me!
    I want to share a snippet to add a custom fee based on percentage. Check this out

     add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee_for_gateway' );
    
    
    function bbloomer_add_checkout_fee_for_gateway() {
     
      global $woocommerce;
     
      $chosen_gateway = $woocommerce->session->chosen_payment_method;
      
      if ( $chosen_gateway == 'paypal' ) {
      
      
     	$percentage = 0.02;
     	$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;	
      $woocommerce->cart->add_fee( 'Commissioni', $surcharge, true, '');
     
      }
     
    } 
    1. Awesome, thank you so much Luca! Much appreciated ๐Ÿ™‚

    2. Hello,
      Thank you very much for your code.

      I am looking to add the commission for payment with paypal.
      However, in paypal they charge fixed commission of 0.34 โ‚ฌ + percentage.
      So, how can I add that fixed commission too?

      1. Nerea, adding extra commission for PayPal payments is against their terms.

    3. I would like to add 2 payment gateways here .. What would be sytanx ?

      $chosen_gateway == ‘PG1’ or ‘PG2’

      is not working .
      Pl help. thanks

  17. Hello,
    congratulations for the site !! I’ve had so many great ideas.
    I write because I am improving the site for my small farmhouse.
    After the guest has chosen the days of stay and goes in the cart I need to be added automatically the cost for final cleaning that is always fixed euro 50 for all apartments.
    Do you think I can use this here?
    Thank you

    1. Ciao Silvia, thanks so much for your comment! Of course you can use this snippet, and you can even rename the fee into “Pulizia Finale” ๐Ÿ™‚ Let me know!

  18. Hi,

    This is a nice snippet. But it would be even nicer if there was a way to only add a fee to a selected payment.

    For example: payment type paypal is 5 dollar fee. But if you choose another payment types, there is no fee. I hope we will see such snippet from you. Thanks!

    Regards,
    Jake

    1. Hey Jake, thanks for your comment! I added a possible solution to the blog, only thing is that it doesn’t update if you switch payment but ony when you change something in the checkout fields ๐Ÿ™‚ Let me know!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.