Last active
November 13, 2023 20:44
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where add this file or code?