How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Add a Custom Fee (or Transaction Fee) in WooCommerce
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > 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.
[mc4wp_form]
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
Most Popular
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026
Caddy server — automatic HTTPS, 3-line Caddyfile vs 25-line nginx config
How to Install Caddy Server on Ubuntu (Automatic HTTPS, Drop-in nginx Alternative)
May 24, 2026
Cloudflare Tunnel — outbound-only connection from server, no inbound port forward
How to Install Cloudflare Tunnel on Ubuntu (Expose Local Services, No Port Forwarding)
May 24, 2026
WireGuard encrypted tunnel between server and clients with lock icons
How to Set Up WireGuard VPN on Ubuntu (Server, Linux Client, and iOS)
May 24, 2026

You Might Also Like

Replace Broken Images Automatically with JavaScript
Web Development

Replace Broken Images Automatically with JavaScript (and jQuery)

5 Min Read
WordPress $wpdb->insert_id — read after $wpdb->insert or $wpdb->query INSERT
Web Development

How to Retrieve the Last Inserted Row ID in WordPress

7 Min Read
PHP Bangladeshi number format — 1,00,23,456.79 grouping
Web Development

Format Numbers in Bangladeshi / Indian Style with PHP

6 Min Read
Select2 auto-focus on page load — .select2('open')
Web Development

How to Auto-focus a Select2 Dropdown on Page Load

4 Min Read
How7o

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

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?