Adding a dynamic payment gateway fee in WooCommerce is a practical way to offset transaction costs associated with specific payment methods like PayPal, Stripe, or Bank Transfer. By charging a small fee for certain gateways, you can reduce your processing expenses.
In this guide, we’ll show you how to add dynamic payment gateway fees in WooCommerce — all without a plugin. You’ll have full control over which gateways are charged fees and how much to charge.
Step 1: Define Fees for Payment Gateways
The first step is to define the fees for each payment gateway. You can set a fixed fee or a percentage-based fee for each gateway.
Code to Define Payment Gateway Fees
Add this code to your theme’s functions.php file or your child theme:
/*
* Snippet: How to Add Dynamic Payment Gateway Fees in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1793
* Tested with WooCommerce 10.7.0
* "This function defines the payment gateway fees"
*/
function wcsuccess_get_payment_gateway_fees() {
return array(
'paypal' => array( 'type' => 'percentage', 'value' => 2.5 ), // 2.5% fee for PayPal
'cod' => array( 'type' => 'fixed', 'value' => 5 ), // $5 fixed fee for Cash on Delivery
'stripe' => array( 'type' => 'percentage', 'value' => 1.5 ), // 1.5% fee for Stripe
);
}
Explanation:
- Gateway Fees: Defines fees for each gateway, e.g., PayPal, COD, and Stripe.
- Fee Types: Supports percentage-based fees and fixed fees.
- Customisation: Update the list of gateways and fees as required for your store.
Step 2: Apply Fees When the Payment Gateway Is Selected
Now that we have defined the fees for each gateway, we’ll dynamically add them when the user selects a gateway during checkout.
Code to Add Dynamic Fees
Add this code to your functions.php file:
/*
* Snippet: How to Add Dynamic Payment Gateway Fees in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1793
* Tested with WooCommerce 10.7.0
* "This function applies dynamic fees based on the selected payment gateway"
*/
function wcsuccess_add_payment_gateway_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
// Get the selected payment method
$chosen_gateway = WC()->session->get( 'chosen_payment_method' );
// Get the gateway fees
$gateway_fees = wcsuccess_get_payment_gateway_fees();
if ( isset( $gateway_fees[ $chosen_gateway ] ) ) {
$fee_info = $gateway_fees[ $chosen_gateway ];
$fee_amount = 0;
if ( $fee_info['type'] === 'fixed' ) {
// Add a fixed fee
$fee_amount = $fee_info['value'];
} elseif ( $fee_info['type'] === 'percentage' ) {
// Calculate a percentage-based fee
$cart_total = $cart->cart_contents_total + $cart->get_shipping_total();
$fee_amount = ( $cart_total * $fee_info['value'] ) / 100;
}
if ( $fee_amount > 0 ) {
WC()->cart->add_fee( __( 'Payment Gateway Fee', 'woocommerce' ), $fee_amount );
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'wcsuccess_add_payment_gateway_fee', 10, 1 );
Explanation:
- Fee Calculation: Calculates fees for the selected gateway.
- Percentage and Fixed Fees: Supports both fixed fees and percentage-based fees.
- Dynamic Application: Only applies the fee when the gateway is selected.
Step 3: Ensure the Payment Method Is Saved in the WooCommerce Session
To ensure the fee is applied properly when the payment method changes, you must save the payment method in the WooCommerce session.
Code to Save the Chosen Payment Method
Add this code to your functions.php file:
/*
* Snippet: How to Add Dynamic Payment Gateway Fees in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1793
* Tested with WooCommerce 10.7.0
* "This function saves the selected payment gateway in the session"
*/
function wcsuccess_store_chosen_payment_gateway() {
if ( isset( $_POST['payment_method'] ) ) {
WC()->session->set( 'chosen_payment_method', sanitize_text_field( $_POST['payment_method'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_review', 'wcsuccess_store_chosen_payment_gateway' );
Explanation:
- Session Storage: Stores the selected payment method in WooCommerce’s session.
- Checkout Process: Runs when the checkout form is updated.
Step 4: Display Payment Gateway Fees in the Checkout
When the payment gateway is selected, WooCommerce should display the fee to customers in the order summary.
Code to Display the Payment Gateway Fee
This part is handled automatically with the WC()->cart->add_fee() function, which displays the fee on the checkout page. No additional code is required.
The total fee is visible in the order summary and appears as Payment Gateway Fee.
Customisation Options
Customise the Fee Label
If you want to change the label Payment Gateway Fee, update this line in the fee calculation:
WC()->cart->add_fee( __( 'Processing Fee', 'woocommerce' ), $fee_amount );
Change Processing Fee to any label you prefer.
Customise Gateway Fees
If you want to charge different fees for different products, you can customise the logic. Here’s an example of applying a product-based fee.
$category_slug = 'special-products'; // Products from this category get an extra fee
foreach ( $cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
if ( has_term( $category_slug, 'product_cat', $product_id ) ) {
$fee_amount += 2; // Add an extra $2 fee for products in this category
}
}
This logic applies an additional $2 fee to products in a specific product category.
Example Workflow
- Customer Chooses Payment Method:
- The customer selects PayPal at checkout.
- Dynamic Fee Calculation:
- A 2.5% fee is calculated on the total cart and shipping cost.
- Fee Display:
- The fee is shown in the order summary under Payment Gateway Fee.
- Order Confirmation:
- The fee is included in the total amount and visible in the order email.
Example Use Case
| Gateway | Fee Type | Fee Amount |
|---|---|---|
| PayPal | Percentage | 2.5% of cart total |
| COD (Cash on Delivery) | Fixed | $5 |
| Stripe | Percentage | 1.5% of cart total |
If a customer chooses PayPal and the cart total is $100, a 2.5% fee ($2.50) is added to the total.
If they choose Cash on Delivery (COD), a fixed fee of $5 is added, regardless of the total cart value.
Example Customer Experience
- Cart Total: $100
- Shipping: $10
- Selected Payment Gateway: PayPal (2.5% fee)
- Fee Calculation: 2.5% of $110 = $2.75
- Total Payable: $100 + $10 + $2.75 = $112.75
Conclusion
Adding dynamic payment gateway fees to WooCommerce gives you control over how payment fees are charged. By following this guide, you can set fixed or percentage-based fees for PayPal, Stripe, Cash on Delivery, or any other payment gateway.
Test these changes in a staging environment before applying them to your live store. For more WooCommerce customisation tips, check out our WooCommerce Visual Hooks Guide or use our wp-config generator for advanced configurations.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

