In this article, I’m going to look at two ways to apply fees and discounts in WooCommerce depending on which payment gateway the user selects:

  • Using a plugin to apply fees and discounts according to the payment gateway
  • Programmatically apply fees and discounts according to the payment gateway

What is a WooCommerce payment gateway?

The payment gateway is the method used by the customer to pay for their order. Examples are PayPal, Stripe (for credit card payments), bank transfer, cash on delivery.

Why apply different charges according to the payment gateway used?

There are several reasons to apply different charges based on the payment gateway, including:

  • Credit card providers usually charge you a percentage of each transaction. You can recoup this fee by adding the equivalent amount to the cost of the order and therefore passing the transactional fees from your bank on to your customer.
  • PayPal charges you a fee on every transaction. You can add a flat fee or a percentage of the overall order total to recover this cost from the user.
  • With PayPal and credit card gateways, you can often find that you are subject to disputes or chargebacks. The costs for these vary between gateway providers but by adding a small surcharge to users who check out through one of these gateways, you can provide a small insurance for yourself against future disputes.
  • If you accept payment via BACS or bank transfer, you might be charged per transaction by your bank. You can add a small fixed fee to the cart to cover these bank charges.
  • You can offer incentives to your customers to pay by your preferred payment gateway by applying a discount. For instance, if you would like to encourage users to pay via bank transfer, you can offer a fixed or percentage discount.

Using a plugin to apply fees and discounts based on payment gateway

The WooCommerce payment gateway fees plugin that I’m using is WooCommerce Fees and Discounts.

WooCommerce Fees and Discounts banner

I’m going to provide a couple of different examples using this plugin to show how to apply fees and discounts, either as fixed amounts or percentages of the overall total.

Install WooCommerce Fees and Discounts

  • Once you’ve downloaded the Fees and Discounts plugin, upload it to your WordPress admin via Plugins > Add New > Upload.
  • Activate the plugin then go to WooCommerce > Settings > Fees and Discounts.
  • Ensure that the ‘Enable Payment Gateway Fees and Discounts’ option is selected. You should see all the active payment gateways on your site available as clickable links below the main tabs.

WooCommerce payment gateways

You can set fees and discounts for each payment gateway separately.

How to add a fee to PayPal in WooCommerce

In this example, I’ll walk through charge to add a fixed fee to customers who check out via PayPal.

  • Click on the PayPal Checkout link
  • In ‘Label’ enter the text that you’d like to use to denote the extra charge
  • Choose ‘Fixed fee’ in the ‘Type’ dropdown
  • Enter the amount of the fixed fee in the ‘Amount’ field
  • Decide whether to include shipping costs in the cart total. (Note that this is mainly relevant if you’re charging a percentage rather than a fixed fee and/or if you are setting minimum or maximum values for the cart)
  • In ‘Minimum cart amount’, you can enter a minimum total for the cart before any charges are applied.
  • In ‘Maximum cart amount’, you can enter a maximum total for the cart above which no charges will be applied.
  • Check ‘Apply tax’ if you would like tax to be applied to the extra charge
  • Select the ‘Tax class’
  • Click ‘Save changes’

WooCommerce add fee to PayPal

When you choose ‘PayPal’ at the checkout, you’ll see an additional fee added to the totals:

WooCommerce additional fee for PayPal

How to apply a discount for certain payment gateways in WooCommerce

In this example, I’ll walk through applying a discount for customers who choose to pay via direct bank transfer. For the sake of the example, the discount will be calculated as a percentage of the overall order total.

  • Click on the ‘Direct bank transfer’ link
  • In ‘Label’ enter the text that you’d like to use to describe the discount
  • Choose ‘Percentage discount’ in the ‘Type’ dropdown
  • Enter the percentage in the ‘Amount’ field. In this example, we’ll give the customer a 1% discount for checking out via direct bank transfer.
  • Decide whether to include shipping costs in the cart total.
  • In ‘Minimum cart amount’, you can enter a minimum total for the cart before any charges are applied. In this example, we’ve set a minimum of 75 – meaning that the customer will need to spend 75 of whatever currency being used before they qualify for the discount
  • In ‘Maximum cart amount’, you can enter a maximum total for the cart above which no charges will be applied.
  • In ‘Minimum fee/discount amount’, you can enter a minimum value for the discount
  • In ‘Maximum fee/discount amount’, you can set a maximum value for the discount. In this example, we’ve limited the discount to 5
  • Check ‘Apply tax’ if you would like tax to be applied to the extra charge
  • Select the ‘Tax class’
  • Click ‘Save changes’

WooCommerce payment gateway discount

Now, when the user adds at least $75 worth of goods to the cart, they’ll qualify for 1% discount to a maximum of $5.

WooCommerce percentage based discount at checkout

Programmatically apply fees and discounts according to the payment gateway

If you prefer to create your own solution, here is some code to help with applying a fee at checkout based on payment gateway.

<?php
/**
* Add a fee when the user checks out with PayPal
*/
function wcfad_apply_payment_gateway_fee() {
$payment_method = WC()->session->get( 'chosen_payment_method' );
// Only apply the fee if the payment gateway is PayPal
// Note that you might need to check this slug, depending on the PayPal gateway you're using
if( $payment_method == 'ppec_paypal' ) {
$label = __( 'PayPal fee', 'wcfad' );
$amount = 5; // Change this value to whatever amount you wish
// Change the third parameter to false if you don't wish to apply tax to the fee
// Change the fourth parameter to a different tax class if required
WC()->cart->add_fee( $label, $amount, true, 'standard' );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'wcfad_apply_payment_gateway_fee' );
/**
* Add some JS
*/
function wcfad_script() {
?>
<script>
jQuery(document).ready(function($){
$('body').on('change','.checkout .input-radio',function(){
$('body').trigger('update_checkout');
});
});
</script>
<?php
}
add_action( 'woocommerce_after_checkout_form', 'wcfad_script' );

This function gets the selected payment gateway for the user’s session and checks to see if it’s PayPal. Note that I’m using the ‘PayPal Checkout’ gateway rather than the older, standard ‘PayPal’ checkout.

If the user has selected the payment gateway, use WC()->cart->add_fee to add the fee. There are four parameters: label, amount, is_taxable, tax_class.

Finally, the function is hooked to woocommerce_cart_calculate_fees so it runs whenever the cart calculates fees.

Now, we just need a small piece of JavaScript to detect when the payment gateway is changed by the user. This triggers the WooCommerce update_checkout event which, in turn, will re-trigger woocommerce_cart_calculate_fees.

Wrapping up

I hope this has been helpful in demonstrating a couple of different ways to add fees or discounts to the WooCommerce checkout. Any questions, please leave a comment. You can find out more about the WooCommerce Fees and Discounts here.

WooCommerce Fees and Discounts banner


Popular Posts

Leave a Reply

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