Payment gateway based fees and discounts for woocommerce

Payment gateway based fees and discounts for woocommerce

When work with a WooCommerce ecommerce website you need to add payment gateway based fees and discounts for WooCommerce. This can achieve in many ways. There are lot of plugins out there to achieve this. But most of the plugins paid plugins. So, you need to buy the premium plugin version to add these payment gateway based fees.

Here you’ll learn to add payment gateway based fees and discounts for WooCommerce without using a plugin.

Add payment gateway based fees for all enabled payment gateways

As all know there can be several payment gateways enable for WooCommerce. We can add common fee to all payments gateways. For that you need to create a filter. For that you can use functions.php. If you’re using third party theme you need to add this code in a child theme. Otherwise your code snippets will vanish once theme get updated. Please see our step by step guide on how to create a child theme in WordPress.

add_action( 'woocommerce_cart_calculate_fees', 'nerodev_add_convenience_fee' );
  
function nerodev_add_convenience_fee() {
   // Label - "Convenience Fee"
   // Fee Value - "3"
   WC()->cart->add_fee( 'Convenience Fee', 3 );
}

This will add the 3% fee at the checkout under Convenience Fee label. You can change both of these fields to your own.

The Discount

You can follow the fee opposite for the discount. Use the same code and change the value to minus(-).

 
 add_action( 'woocommerce_cart_calculate_fees', 'nerodev_add_convenience_fee' ); 
 function nerodev_add_convenience_fee() { 
  // Label - "Convenience Fee" 
  // Fee Value - "3" 
  WC()->cart->add_fee( 'Discount', -3 ); 
 } 

In this way you can add the Discount to checkout.

Add payment gateway based fee for specific payment gateway

Even you can add fee as different values for each gateway like. 5% for paypal, 1% for Cash on Delivery, 2% for check deposits, etc like. For that you need to check for the payment gateway.

add_action('woocommerce_cart_calculate_fees', 'nerodev_add_convenience_fee_for_specific_gateway');
function nerodev_add_convenience_fee_for_specific_gateway()
{
 $gateway = WC()->session->get('chosen_payment_method');
 if ($gateway == 'paypal') {
  WC()->cart->add_fee('Convenience Fee', 5);
 } elseif ($gateway == 'cheque') {
  WC()->cart->add_fee('Convenience Fee', 2);
 } elseif ($gateway == 'cod') {
  WC()->cart->add_fee('Convenience Fee', 1);
 } elseif ($gateway == 'bacs') {
  WC()->cart->add_fee('Convenience Fee', 0.25);
 }
}

add_action('woocommerce_review_order_before_payment', 'nerodev_refresh_checkout_on_gateway_change');
function nerodev_refresh_checkout_on_gateway_change()
{
?>
  <script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name="payment_method"]', function() {
 $(document.body).trigger('update_checkout');
});
})(jQuery);
 </script>
<?php
}

Note: This code can be broken anytime due to woocommerce updates and/or introduces new hooks instead of existing actions and filter hooks. If you’re facing difficulties with the provided code snippet. Please comment below. I’ll update the revamped code snippet as soon as possible.

Hope you enjoyed this tutorial regarding payment gateway based fees and discounts for woocommerce. Happy learning ahead. Thanks for reading.

Recommended: Please follow our step by step guide how to create a WordPress Theme from scratch. You’ll be able to create your own theme at the end of these lessons.

You may also like...

Leave a Reply

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