How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Add a Custom Fee (or Transaction Fee) in WooCommerce
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Web Development > How to Add a Custom Fee (or Transaction Fee) in WooCommerce
Web Development

How to Add a Custom Fee (or Transaction Fee) in WooCommerce

how7o
By how7o
Last updated: May 10, 2026
8 Min Read
WooCommerce add custom fee — woocommerce_cart_calculate_fees + WC()->cart->add_fee
SHARE

The woocommerce add fee pattern is always add_action('woocommerce_cart_calculate_fees', ...) + WC()->cart->add_fee('Label', $amount). WooCommerce recalculates the cart whenever something changes (item added, shipping country picked, payment method switched) and fires the hook — your callback decides whether to add a fee and how much. This guide covers fixed fees, percentage fees, threshold-based fees, and the payment-method-triggered variant that needs a small AJAX nudge.

Contents
  • TL;DR
  • Pattern 1 — Fixed fee
  • Pattern 2 — Percentage fee
  • Pattern 3 — Fee below a threshold
  • Pattern 4 — Fee by shipping country
  • Pattern 5 — Fee by shipping method
  • Pattern 6 — Fee by payment method (+ the JS bit)
  • Making fees taxable
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on WooCommerce 9.x with WordPress 6.5. Originally published 2024-03-23, rewritten and updated 2026-04-23.

TL;DR

add_action( 'woocommerce_cart_calculate_fees', function () {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }
    WC()->cart->add_fee( __( 'Transaction Fee', 'how7o' ), 5 );
} );

Pattern 1 — Fixed fee

add_action( 'woocommerce_cart_calculate_fees', function () {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }
    WC()->cart->add_fee( __( 'A small fee', 'how7o' ), 5 );
} );

The simplest form: every cart gets a flat $5 fee added. The first argument to add_fee is the user-visible label; the second is the amount in the store’s base currency.

Pattern 2 — Percentage fee

add_action( 'woocommerce_cart_calculate_fees', function () {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    $percentage     = 0.05;  // 5%
    $subtotal       = WC()->cart->get_cart_contents_total() + WC()->cart->get_shipping_total();
    $percentage_fee = $subtotal * $percentage;

    WC()->cart->add_fee( __( 'Processing fee (5%)', 'how7o' ), $percentage_fee );
} );

A 5% fee on cart + shipping. get_cart_contents_total() is the items subtotal (after cart-level discounts); get_shipping_total() is the chosen shipping rate. Summing both and multiplying gives a percentage fee that mirrors how PayPal/Stripe transaction fees actually work.

woocommerce add fee — cart_calculate_fees hook fires on every recalculation with context conditions

Pattern 3 — Fee below a threshold

add_action( 'woocommerce_cart_calculate_fees', function () {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    $cart_total = WC()->cart->get_cart_contents_total();

    if ( $cart_total < 500 ) {
        WC()->cart->add_fee( __( 'Small order fee', 'how7o' ), 50 );
    }
} );

Adds a surcharge on carts below $500. Encourages larger orders or covers the fixed overhead of small ones. The same pattern flips for a threshold-based discount: add a negative fee (with the same caveats about coupons being the better tool).

Pattern 4 — Fee by shipping country

add_action( 'woocommerce_cart_calculate_fees', function () {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    $country = WC()->customer->get_shipping_country();
    if ( $country === 'NO' ) {
        WC()->cart->add_fee( __( 'Norway shipping surcharge', 'how7o' ), 50 );
    }
} );

Country-specific fees (customs, logistics, regulatory) live in the customer object. get_shipping_country() returns the two-letter ISO code; pair with in_array() for multiple countries. WooCommerce re-fires woocommerce_cart_calculate_fees automatically when the billing/shipping country changes on the checkout.

Pattern 5 — Fee by shipping method

add_action( 'woocommerce_cart_calculate_fees', function () {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    $chosen_shipping = WC()->session->get( 'chosen_shipping_methods' );

    if ( ! empty( $chosen_shipping[0] ) && str_starts_with( $chosen_shipping[0], 'flat_rate' ) ) {
        WC()->cart->add_fee( __( 'Flat-rate handling fee', 'how7o' ), 50 );
    }
} );

The chosen shipping method IDs take the form flat_rate:1, free_shipping:2, local_pickup:3. str_starts_with matches a whole method family regardless of which specific zone-rate instance was picked.

Pattern 6 — Fee by payment method (+ the JS bit)

add_action( 'woocommerce_cart_calculate_fees', function () {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    $chosen_payment = WC()->session->get( 'chosen_payment_method' );
    if ( $chosen_payment === 'paypal' ) {
        WC()->cart->add_fee( __( 'PayPal fee', 'how7o' ), 50 );
    }
} );

// Trigger a recalc when payment method changes (WooCommerce doesn't do this automatically)
add_action( 'woocommerce_review_order_before_payment', function () {
    ?>
    <script>
    (function($){
        $('form.checkout').on('change', 'input[name^="payment_method"]', function() {
            $('body').trigger('update_checkout');
        });
    })(jQuery);
    </script>
    <?php
} );

Unique case: WooCommerce doesn’t automatically fire update_checkout when the payment method radio changes (only when shipping changes). The small script manually triggers it, so woocommerce_cart_calculate_fees runs again with the new payment method, and your fee updates inline.

Making fees taxable

WC()->cart->add_fee(
    __( 'Handling fee', 'how7o' ),
    10,
    true,              // taxable
    'standard'         // tax class (optional)
);

Third argument is the taxable flag; fourth is the tax class (default '' uses the standard rate). Flip to true when your jurisdiction requires VAT/GST on the fee.

Frequently asked questions

What’s the canonical woocommerce add fee hook?

woocommerce_cart_calculate_fees. WooCommerce fires this on every cart recalculation, and inside the callback you can call WC()->cart->add_fee('Label', $amount) to add the fee. The fee automatically flows into the cart subtotal, the checkout totals, tax calculations if taxable, and the final order. No database writes needed — fees live for the current cart session.

Why the if (is_admin() && !defined('DOING_AJAX')) return guard?

Cart calculations can fire in the admin (manual order creation, subscription renewal) — and there you often don’t want customer-facing surcharges to apply. The guard skips the fee logic in those admin contexts while still letting AJAX requests (checkout updates, cart refresh) through. Most fee snippets should keep it; remove only if you specifically want the fee on admin-created orders.

Can the fee amount be negative for discounts?

Technically yes — add_fee('Discount', -5) subtracts from the total. But use WooCommerce’s coupon system for real discounts; negative fees don’t show up in reports as discounts, don’t respect coupon restrictions, and can confuse tax calculations. Negative fees are appropriate only for “rounding adjustments” or small corrections.

How do I make the fee trigger an AJAX refresh when the payment method changes?

WooCommerce’s checkout JS only fires update_checkout on certain field changes — billing country, shipping method. For payment-method changes you need a small script that triggers it manually: $('form.checkout').on('change', 'input[name^="payment_method"]', function() { $('body').trigger('update_checkout'); });. Hook it on woocommerce_review_order_before_payment.

Are fees taxable?

WC()->cart->add_fee('Label', $amount, $taxable, $tax_class) — third argument is a boolean, fourth is the tax class. Pass true if the fee should be taxed at the standard rate, or name a specific tax class ('reduced-rate') for other rates. Default is non-taxable, which is appropriate for most transaction-fee add-ons.

Related guides

  • How to Remove Checkout Fields in WooCommerce — companion checkout-surface customization.
  • How to Display Orders Instead of Dashboard on the WooCommerce My Account Page — another filter-driven tweak.
  • How to Dynamically Change Currency in WooCommerce — currency-aware pricing around fees.
  • How to Add a Link or Button After the Login Form in WooCommerce — My Account hook family.

References

WooCommerce WC_Cart::add_fee reference: woocommerce.github.io/code-reference/classes/WC-Cart.html#method_add_fee.

TAGGED:jQueryphpWooCommercewordpress

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article WooCommerce My Account login form hooks — five positions for injecting content How to Add a Link or Button After the Login Form in WooCommerce
Next Article WooCommerce auto add to cart on visit — template_redirect hook and cart dedup How to Automatically Add a Product to Cart on Visit in WooCommerce
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Display PHP errors — ini_set + php.ini configuration
How to Display PHP Errors
May 10, 2026
PHP convert string to uppercase — strtoupper and mb_strtoupper
How to Convert a String to Uppercase in PHP
May 10, 2026
PHP string to float conversion with cast, regex cleanup, NumberFormatter
How to Convert a String to Float in PHP
May 10, 2026
PHP merge arrays without duplicates — union operator and array_unique
How to Combine Two Arrays Without Duplicates in PHP
May 10, 2026
PHP delete array element — unset, array_splice, array_filter, array_search
How to Delete an Element from a PHP Array
May 10, 2026

You Might Also Like

WooCommerce My Account login form hooks — five positions for injecting content
Web Development

How to Add a Link or Button After the Login Form in WooCommerce

6 Min Read
Check if Laravel scheduler is running (cron + php artisan schedule:run)
Web Development

How to Check if Laravel Scheduler Is Running (Cron + Logs)

6 Min Read
Dynamically set site title and tagline in WordPress by country
Web Development

How to Dynamically Set Site Title and Tagline in WordPress (By Country)

6 Min Read
Laravel cURL error 60 SSL certificate problem — CA bundle wiring in php.ini
Web Development

How to Fix cURL Error 60 SSL Certificate Problem in Laravel

9 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?