Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Last active November 13, 2023 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save plugin-republic/4fc71e8e5c9da48459dbc8532f9ced55 to your computer and use it in GitHub Desktop.
Save plugin-republic/4fc71e8e5c9da48459dbc8532f9ced55 to your computer and use it in GitHub Desktop.
Add a charge to WooCommerce when the customer checks out via PayPal: https://pluginrepublic.com/woocommerce-payment-gateway-based-fees/
<?php
/**
* Add a fee when the user checks out with PayPal
*/
function wcfad_apply_payment_gateway_fee() {
$payment_method = WC()->session->get( 'chosen_payment_method' );
// Only apply the fee if the payment gateway is PayPal
// Note that you might need to check this slug, depending on the PayPal gateway you're using
if( $payment_method == 'ppec_paypal' ) {
$label = __( 'PayPal fee', 'wcfad' );
$amount = 5; // Change this value to whatever amount you wish
// Change the third parameter to false if you don't wish to apply tax to the fee
// Change the fourth parameter to a different tax class if required
WC()->cart->add_fee( $label, $amount, true, 'standard' );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'wcfad_apply_payment_gateway_fee' );
/**
* Add some JS
*/
function wcfad_script() {
?>
<script>
jQuery(document).ready(function($){
$('body').on('change','.checkout .input-radio',function(){
$('body').trigger('update_checkout');
});
});
</script>
<?php
}
add_action( 'woocommerce_after_checkout_form', 'wcfad_script' );
@janagan36
Copy link

where add this file or code?

@plugin-republic
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment