How to add payment Gateway Extra Charges in Woocommerce?

The topic that we are going to discuss today is about one of the major issue that most of the 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. Recently, there was a store owner that connected with Webnexs in search of alternative solution for these deductions.  We did 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 huge % of 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. If a user is willing to purchase a product using credit card then the user will be charged the certain % of amount upon the checkout page. However, since this solution is not default on 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 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 a solution. If you are interested in hiring a WooCommerce specialist for your business, then please visit Webnexs.com. We would be happy to help you on the same.

Last Update: December 18, 2019  

December 16, 2019   167   benny    Nodejs, Payment Gateway Chargers    
Total 0 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

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

Facebook
Twitter
INSTAGRAM
LinkedIn