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 CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_checkout_fee' );
 
function bbloomer_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 CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
// 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->get( 'chosen_payment_method' );
  	if ( $chosen_gateway == 'paypal' ) {
		WC()->cart->add_fee( 'PayPal Fee', 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
}
Share:

Leave a Reply