Use the woocommerce_cart_calculate_fees
hook in your theme’s functions.php file. It allows you to add custom fees based on cart conditions, payment methods, or shipping info.
Want to add WooCommerce custom checkout fees based on conditions like location, payment method, cart total, or shipping class? You’re in the right place. This tutorial will walk you through the exact hooks, logic, and code to add dynamic fees to your checkout — without any plugins.
By the end, you’ll know how to add:
- 💸 Payment gateway surcharge
- 🚚 Delivery zone fee
- 📦 Handling fee for heavy products
- 🎯 Conditional fees by user role or cart value
Why Add Custom Fees in WooCommerce?
- 📊 Pass payment processor costs to customers (like PayPal/Stripe fees)
- 📍 Add fees based on shipping zone or delivery area
- 🔐 Add security insurance, packaging, or carbon offset fees
- 🧩 Enable or disable fees based on any business logic
WooCommerce Hook You’ll Use
The main hook for adding fees is:
add_action('woocommerce_cart_calculate_fees', 'your_function_name');
This fires during checkout right after totals are calculated. It’s powerful, flexible, and safe to use.
✅ Basic Example: Flat Custom Fee
add_action('woocommerce_cart_calculate_fees', 'add_flat_checkout_fee');
function add_flat_checkout_fee() {
if (is_admin() && !defined('DOING_AJAX')) return;
WC()->cart->add_fee('Handling Fee', 5.00, true); // true = taxable
}
This adds a $5 handling fee to all checkouts. Simple and clean.
🔁 Conditional Fee Based on Payment Gateway
Charge a surcharge if the user selects PayPal at checkout:
add_action('woocommerce_cart_calculate_fees', 'paypal_surcharge_fee');
function paypal_surcharge_fee() {
if (is_admin() && !defined('DOING_AJAX')) return;
if (WC()->session->get('chosen_payment_method') === 'paypal') {
WC()->cart->add_fee('PayPal Surcharge', 2.50, true);
}
}
Replace 'paypal'
with your payment gateway ID.
🌍 Fee Based on Shipping Country
Apply an international handling fee for orders outside the US:
add_action('woocommerce_cart_calculate_fees', 'country_based_fee');
function country_based_fee() {
if (is_admin() && !defined('DOING_AJAX')) return;
$country = WC()->customer->get_shipping_country();
if ($country !== 'US') {
WC()->cart->add_fee('International Order Fee', 8.00);
}
}
🛒 Fee Based on Cart Total
Add a small order fee for carts under $50:
add_action('woocommerce_cart_calculate_fees', 'small_order_fee');
function small_order_fee() {
if (is_admin() && !defined('DOING_AJAX')) return;
$cart_total = WC()->cart->get_subtotal();
if ($cart_total < 50) {
WC()->cart->add_fee('Small Order Fee', 3.50);
}
}
🎯 Advanced: Fee Based on Product Category
Charge a packaging fee if the cart contains products in a specific category (e.g., "fragile"
):
add_action('woocommerce_cart_calculate_fees', 'category_fee_fragile_items');
function category_fee_fragile_items() {
if (is_admin() && !defined('DOING_AJAX')) return;
$fragile_found = false;
foreach (WC()->cart->get_cart_contents() as $item) {
if (has_term('fragile', 'product_cat', $item['product_id'])) {
$fragile_found = true;
break;
}
}
if ($fragile_found) {
WC()->cart->add_fee('Fragile Item Packaging', 4.00);
}
}
Make sure to replace fragile
with your actual product category slug.
Final Thoughts
Adding custom WooCommerce checkout fees helps align your store with real-world business logic and profitability. Whether it’s payment surcharges, regional adjustments, or category-specific packaging — you can achieve it all using the woocommerce_cart_calculate_fees
hook.
Need help building advanced WooCommerce logic for your store? Contact Babar Ilyas for hands-on development or store optimization.
Also check out our guide on customizing the WooCommerce checkout page for even more control over your customer experience.