WooCommerce Guides

How to Add a Conditional Fee or Payment Surcharge in WooCommerce

· 4 min read

Share

Some orders cost more to fulfill than others. Heavy parcels, remote delivery zones and certain payment methods all add real costs. So many store owners want to add a fee only when those conditions apply, instead of raising prices for everyone.

However, WooCommerce has no built-in setting for this. In this guide, you will learn two ways to add a WooCommerce conditional fee: with a code snippet, and with a plugin.

When a WooCommerce conditional fee makes sense

  • Payment method surcharges. For example, a small fee for a payment method that costs you more to process.
  • Heavy or oversized orders. A handling fee when the cart weight passes a set limit.
  • Remote shipping zones. An extra fee for zones that cost more to reach.
  • Small orders. A small-order fee when the cart subtotal is below a threshold.

Before you add a payment surcharge, check the rules in the countries you sell to. For example, the EU restricts surcharges on most consumer card payments.

Option 1: Add a WooCommerce conditional fee with code

WooCommerce lets developers add fees through the woocommerce_cart_calculate_fees hook, which is covered in the WooCommerce developer documentation. The snippet below adds a 3% fee when the customer chooses a specific payment method. Replace cod with the ID of the payment method you want to target.

add_action( 'woocommerce_cart_calculate_fees', 'xt_payment_method_fee' );

function xt_payment_method_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return;
    }

    $chosen = WC()->session ? WC()->session->get( 'chosen_payment_method' ) : '';

    if ( 'cod' === $chosen ) {
        $fee = $cart->get_subtotal() * 0.03;
        $cart->add_fee( 'Payment method fee', $fee );
    }
}

add_action( 'wp_footer', 'xt_refresh_checkout_on_payment_change' );

function xt_refresh_checkout_on_payment_change() {
    if ( ! is_checkout() ) {
        return;
    }
    ?>
    <script>
    jQuery( 'form.checkout' ).on( 'change', 'input[name="payment_method"]', function () {
        jQuery( document.body ).trigger( 'update_checkout' );
    } );
    </script>
    <?php
}

The second function refreshes the checkout totals when the customer switches payment method, so the fee appears straight away.

This approach works, but it has limits. Each new condition means more code to write and maintain. Also, the refresh script is written for the classic checkout, so the WooCommerce Checkout Block needs a different approach. As always, test code on a staging site first.

Option 2: Use a plugin for conditional fees and discounts

If you need more than one rule, a plugin is much easier to manage. Our Order Discounts and Conditional Fee for WooCommerce plugin adds fees or discounts automatically when a cart meets your conditions.

It supports more than 15 conditions, including:

  • Cart subtotal, weight and dimensions.
  • User role and purchase history.
  • Shipping zone and payment method.
  • Coupon codes and product category.

You can also combine several conditions in a single rule, and run discounts and fees at the same time. Plus, fees and discounts update in real time as customers change their cart, and the cart page shows which rules apply.

Examples of conditional fee rules

  • Handling fee: add a fee when the cart weight exceeds 20 kg.
  • Zone fee: add a fee for orders shipped to a remote zone.
  • Payment incentive: add a small fee for one payment method, and then offer a discount for bank transfer.
  • Loyalty reward: give a discount to customers with several previous orders.

Tips for fair conditional fees

  • Be transparent. Name each fee clearly, such as “Heavy item handling”, so customers understand it.
  • Keep fees proportional. A fee should reflect your real extra cost.
  • Offer a way to avoid it. For instance, a payment method without a surcharge.
  • Test every rule. Place test orders to check that fees appear only when they should.

Conclusion

A WooCommerce conditional fee lets you recover real costs without raising prices for everyone. For one simple fee, a code snippet can work. However, if you want several rules, discounts as well as fees, and no code to maintain, Order Discounts and Conditional Fee for WooCommerce handles it for you.

Would you rather block small orders entirely? Then read our guide on how to set a minimum order amount in WooCommerce.

Found this useful? Share it

1 comment

Leave a comment

Your email address will not be published. First-time comments are reviewed before they appear.