Menu Close

WooCommerce: How to add checkout payment Fee for a specific payment gateway

0
(0)

PHP snippet to add a extra fee to the checkout for specific payment gateway.

You can copied and pasted in your theme’s functions.php file and get solution for this.

PHP Code

/**
 * @snippet       How to add checkout payment Fee for a specific payment gateway - WooCommerce
 */
  
// Step 1: Define Fee
  
add_action( 'woocommerce_cart_calculate_fees', 'mycode_add_checkout_fee' );
  
function mycode_add_checkout_fee() {
    $chosen_gateway = WC()->session->get( 'chosen_payment_method' );
    if ( $chosen_gateway == 'your-payment-gatway' ) {
       WC()->cart->add_fee( 'Payment Gateway Fee', 3 );
    }
}
  
// Part 2: Reload checkout page after select the payment gateway
  
add_action( 'woocommerce_review_order_before_payment', 'mycode_refresh_checkoutpage' );
  
function mycode_refresh_checkoutpage(){
    ?>
    <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                $('body').trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
} 

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Share

Posted in PHP, WooCommerce, WordPress

Leave a Reply

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

You can also read...