How to Add Checkout Fee for Payment Gateway in WooCommerce?

In WooCommerce, you need to add a certain payment surcharge fee at the checkout time. It is applicable for every payment gateway or specific ones. The reason being not all payment gateways allow adding such surcharge. So, make sure that you check payment gateway terms before adding fees. In this article, you will learn to add checkout fee for payment gateway. 

One can easily enable this function with a simple PHP code. You just need to copy & paste it in your child theme functions.php file. So, enjoy reading this simple guide and apply it on your WooCommerce website.

Image suggesting to add checkout fee for payment gateway

PHP Snippet 1:  Add Checkout Fee for Payment Gateway – All payment gateways

add_action( 'woocommerce_cart_calculate_fees', 'phpsof_add_checkout_fee' );
  
function phpsof_add_checkout_fee() {
   // Edit "Fee" and "5" below to control Label and Amount
   WC()->cart->add_fee( 'Fee', 5 );
}

PHP Snippet 2: Add Checkout Fee for Payment Gateway – Specific payment gateway

add_action( 'woocommerce_cart_calculate_fees', 'phpsof_add_checkout_fee_for_gateway' );
  
function phpsof_add_checkout_fee_for_gateway() {
    $chosen_gateway = WC()->session->get( 'chosen_payment_method' );
     if ( $chosen_gateway == 'paypal' ) {
      WC()->cart->add_fee( 'PayPal Fee', 5 );
   }
}
 
add_action( 'woocommerce_after_checkout_form', 'phpsof_refresh_checkout_on_payment_methods_change' );
   
function phpsof_refresh_checkout_on_payment_methods_change(){
    wc_enqueue_js( "
       $( 'form.checkout' ).on( 'change', 'input[name^=\'payment_method\']', function() {
           $('body').trigger('update_checkout');
        });
   ");
}

Where do you add this snippet?

So, this is how you can add checkout fee for payment gateway in WooCommerce. Remember to add surcharge only after reading payment gateways terms. Also, it is easier to activate this function with this code. So, to apply this code, just add this PHP snippet at your child theme function.php file’s bottom. It should be placed before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file. Make sure that you are editing these files in a right manner for best results.

Is this snippet still valid?

So, this is how you can learn about add checkout surcharge fee in WooCommerce. I have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected when you code. Share it further if you find this snippet useful for you and it had saved your time. 

Also Read, How to Remove or Move Login or Registration at Checkout?

How to Create Custom Logs in WooCommerce? A Simple Guide

Important links: WooCommerce – https://woocommerce.com/