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

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

by Tutor Aspire

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 for all payment gateways – WooCommerce

/**
 * @snippet       WooCommerce add fee to checkout
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 5.1
 * @donate $9     https://tutoraspire.com
 */
 
add_action( 'woocommerce_cart_calculate_fees', 'tutoraspire_add_checkout_fee' );
 
function tutoraspire_add_checkout_fee() {
   // Edit "Fee" and "5" below to control Label and Amount
   WC()->cart->add_fee( 'Fee', 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        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 3.7
 * @donate $9     https://tutoraspire.com
 */
 
add_action( 'woocommerce_cart_calculate_fees', 'tutoraspire_add_checkout_fee_for_gateway' );
 
function tutoraspire_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', 'tutoraspire_refresh_checkout_on_payment_methods_change' );
  
function tutoraspire_refresh_checkout_on_payment_methods_change(){
    wc_enqueue_js( "
    $( 'form.checkout' ).on( 'change', 'input[name^='payment_method']', function() {
        $('body').trigger('update_checkout');
        });
");
}

You may also like