How to add payment Gateway Extra Charges in WooCommerce?

Learn how to add payment gateway extra charges in WooCommerce by configuring it with a few steps.

The topic that we are going to discuss today is about one of the major issues that most store-owners are annoyed with.  About 70% of the e-commerce store owners hate when the payment gateways deduct the amount by 3-20% for each transaction that is being made using credit cards.

Reach Millions with a website! Move to Ecommerce today, click here!!!

Recently, there was a store owner that connected with Webnexs in search of an alternative solution for these deductions.  We were able to help him with the problem by configuring some changes on the backend.

He is now really happy with the solution, and we thought that this blog would help a lot of store owners that are annoyed with the same problem.

As mentioned, each payment gateway charges a huge % of the amount on each credit card transaction.  There is no means of avoiding the charges that a payment gateway does, but there is an alternative way.

Add payment Gateway Extra Charges in WooCommerce

Add payment Gateway Extra Charges in WooCommerce

If a user is willing to purchase a product using a credit card then the user will be charged a certain % of the amount upon the checkout page. However, since this solution is not to default on the WooCommerce engine platform, I would like to share the code that was used to deal with it.

If you want to charge(3% fee) credit card purchases for all products and specific payment gateway, then use this below code:

add_action( 'woocommerce_cart_calculate_fees', 'anbu_add_checkout_fee_for_gateway' );
  
 function anbu_add_checkout_fee_for_gateway() {
 global $woocommerce;
 
 $chosen_gateway = WC()->session->get( 'chosen_payment_method' );

if ( $chosen_gateway == 'enter your payment gateway' && is_checkout() ){
$surcharge = $woocommerce->cart->subtotal * 0.03;
WC()->cart->add_fee( 'Credit Card Fee', $surcharge, true  );
}
}
 
add_action( 'woocommerce_review_order_before_payment', 'anbu_refresh_checkout_on_payment_methods_change' );
 
function anbu_refresh_checkout_on_payment_methods_change(){
     ?>
     <script type="text/javascript">
         (function($){
             $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                 $('body').trigger('update_checkout');
             });
         })(jQuery);
     </script>
     <?php
}

If you want to charge credit card purchases for a specific product and specific payment gateway then use the below code:

add_action( 'woocommerce_cart_calculate_fees', 'anbu_add_checkout_fee_for_gateway' );
  
 function anbu_add_checkout_fee_for_gateway() {
 global $woocommerce;
 
 $product_id = 392032; //enter product id
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
 $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
  
 $chosen_gateway = WC()->session->get( 'chosen_payment_method' );

if ( $chosen_gateway == 'enter your payment gateway' && $in_cart && is_checkout() ){
$surcharge = $woocommerce->cart->subtotal * 0.03;
WC()->cart->add_fee( 'Credit Card Fee', $surcharge, true  );
}
}
 
add_action( 'woocommerce_review_order_before_payment', 'anbu_refresh_checkout_on_payment_methods_change' );
 
function anbu_refresh_checkout_on_payment_methods_change(){
     ?>
     <script type="text/javascript">
         (function($){
             $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                 $('body').trigger('update_checkout');
             });
         })(jQuery);
     </script>
     <?php
}

I hope that the above content helped you with the payment gateway issue in WooCommerce. If you are interested in hiring a WooCommerce specialist for your business, then please visit us. We would be happy to help you with the same.

Comments

One response to “How to add payment Gateway Extra Charges in WooCommerce?”

  1. Miroslav Hrncir Avatar
    Miroslav Hrncir

    Hi, its great, but unfortunately once the fee is added to cart and then customer choose another shipping method, where is no COD payment available, the cart does not refresh and the fee is still in the cart until customer initiate cart refresh or choose another payment method.

Leave a Reply

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