Checkout

WooCommerce Payment Method Fixed Fee

WooCommerce Payment Method Fixed Fee

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About This Solution

Firstly, it’s important to check you with your payment gateway provider and the appropriate laws that you are allowed to pass on fees for specific payment method selection before implementing fees. This guide shows you how you can add a fixed fee to the order as an order item based on the customers selected payment method. Specifically, this guide shows you how you can add a fixed (your currency here)5 fee to the order if the payment method Cash On Delivery is selected. A label is also added adjacent to the specific payment method with the fixed fee amount to make it super clear an additional cost will be added if said payment method is selected. You will need the Payment Method ID to implement this solution to meet your needs. Cash On Delivery’s Payment Method ID is ‘cod’ for example. You can get the Payment Method ID by using your browsers developer tools to inspect the payment gateway radio button. This is explained further on.

/**
 * Snippet Name:	WooCommerce checkout fee for specific payment method selected
 * Snippet Author:	ecommercehints.com
 */

// Add the fixed fee for the specific payment method
add_action( 'woocommerce_cart_calculate_fees', 'ecommercehints_payment_method_fixed_fee', 10 );
function ecommercehints_payment_method_fixed_fee() {
	if( 'cod' == WC()->session->get( 'chosen_payment_method' ) ) { // The payment method ID
		WC()->cart->add_fee( 'Cash on delivery fee', 5 ); // The fee label and amount
	}
}

// Show the fixed fee amount right of the payment method name
add_filter( 'woocommerce_gateway_icon', 'ecommercehints_show_fee_label', 10, 2 );
function ecommercehints_show_fee_label( $icon_html, $gateway_id ) {
	if( 'cod' === $gateway_id ) {
		return '<small>(' . wc_price( 5 ) . ' fee)</small>'; // The fee amount
	}
}

// JavaScript to update the totals on payment method selection
add_action( 'wp_footer', 'update_checkout_totals' );
function update_checkout_totals() { ?>
<script>
	jQuery( function( $ ) {
	$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
		$( 'body' ).trigger( 'update_checkout' );
	});
});
</script>
<?php }

How Can I Get The Payment Method ID?

Use your browsers developer tools to inspect the payment gateway you’d like to add the fee to, then copy the input value:

WooCommerce get payment method ID

Snippet Benefits

  • Cover costs of payment gateways by adding a fixed fee based on payment method selection.
  • Encourage customers to select a different payment method by introducing fixed fees for those you wish the user not to pick.

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 20% off on your first month with us.

26027
WELCOME OFFER