How to Add a Payment Surcharge for PayPal in WooCommerce

If you run an international e-commerce store, you likely use PayPal as one of your primary payment gateways. However, as many merchants know, PayPal charges a transaction fee for every payment received. By default, the standard WooCommerce PayPal gateway doesn’t provide an option to pass this fee onto the customer. In many regions, merchants prefer to add a surcharge to offset these costs.

While there are several plugins available that support adding surcharges to the PayPal gateway, you can achieve this quite easily with a small amount of custom code. By hooking into the WooCommerce checkout process, we can dynamically add a fee based on the selected payment method. We can implement this using two simple snippets.

Code for Adding a Transaction Fee to the PayPal Gateway

First, we need to add the logic to calculate and add the fee to the cart. You can add the following PHP code to your theme’s functions.php file. This example adds a 5% surcharge when PayPal is selected.

/**
 * Add a 5% surcharge when the PayPal payment method is selected.
 */
add_action('woocommerce_cart_calculate_fees', function ()
{
    $chosen_gateway = WC()->session->get('chosen_payment_method');
    if ($chosen_gateway == 'paypal') {

        // Calculate the base amount for the fee
        if ( ! wc_prices_include_tax()) {
            $amount = WC()->cart->get_cart_contents_total() + WC()->cart->get_shipping_total();
        } else {
            $amount = WC()->cart->get_cart_contents_total() + WC()->cart->get_taxes_total() + WC()->cart->get_shipping_total();
        }

        // Add a 5% fee
        WC()->cart->add_fee('PayPal Fee', $amount * 0.05);
    }
});

The PHP code above handles the fee calculation when the checkout page loads. However, we also need to ensure that the fee is updated instantly if the user switches between payment methods on the checkout page without a full page refresh. We can do this with a small piece of JavaScript:

/**
 * Trigger checkout update when the payment method is changed.
 */
add_action('woocommerce_review_order_before_payment', function ()
{
    ?>
    <script type="text/javascript">
        (function($) {
            $('form.checkout').on('change', 'input[name^="payment_method"]', function() {
                $('body').trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
});

In simple terms, this JavaScript snippet listens for changes to the “payment_method” input and tells WooCommerce to refresh the checkout fragments (including the fees) whenever a different option is selected.

You can use this same logic to add surcharges for other payment gateways too. Simply find the ID of the gateway you want to target (e.g., ‘stripe’ or ‘cod’) and replace ‘paypal’ in the first snippet. You can also adjust the multiplier (0.05 in our example) to whatever percentage or flat fee you require for your business.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *