A simple, powerful and independent e-commerce platform.
Sell anything with ease.

Add a payment gateway surcharge to checkout

Payment gateways such as PayPal and Stripe charge a small amount for processing a transaction. This code adds a credit card surcharge as a percentage of the cart total to cover that amount. Examples are shown for PayPal and Stripe, adding 3% for each.

		/**
 * Add a payment gateway surcharge to checkout
 */

// Part 1: assign fee

add_action('woocommerce_cart_calculate_fees', 'cc_add_checkout_fee_for_gateway');

function cc_add_checkout_fee_for_gateway() {
  $chosen_gateway = WC()->session->get('chosen_payment_method');
  $surcharge = 0.03; // Increase price by 3%
  $cart_total = WC()->cart->get_subtotal();
  if ( $chosen_gateway == 'paypal' ) {
    WC()->cart->add_fee('PayPal Processing Fee', $cart_total * $surcharge);
  }
  if ( $chosen_gateway == 'eh_stripe_pay' ) {
    WC()->cart->add_fee('Stripe Processing Fee', $cart_total * $surcharge);
  }
}

// Part 2: reload checkout on payment gateway change

add_action('woocommerce_review_order_before_payment', 'cc_refresh_checkout_on_payment_methods_change');

function cc_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
}
	
View Raw Code ID: 9895