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 '(' . wc_price( 5 ) . ' fee)'; // The fee amount
}
}
// JavaScript to update the totals on payment method selection
add_action( 'wp_footer', 'update_checkout_totals' );
function update_checkout_totals() { ?>
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:
