Skip to content

Instantly share code, notes, and snippets.

@slash1andy
Created November 22, 2022 21:02
Show Gist options
  • Save slash1andy/dd4e958f0ed2f686a07317a295a93f79 to your computer and use it in GitHub Desktop.
Save slash1andy/dd4e958f0ed2f686a07317a295a93f79 to your computer and use it in GitHub Desktop.
Discount Specific WooCommerce Payment Method (BTCPay in this example)
add_action( 'woocommerce_cart_calculate_fees','ts_add_discount', 20, 1 );
function ts_add_discount( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// Mention the payment method e.g. cod, bacs, cheque or paypal
$payment_method = 'btcpaygf_default';
// The percentage to apply
$percent = 2; // 2%
$cart_total = $cart_object->subtotal_ex_tax;
$chosen_payment_method = WC()->session->get('chosen_payment_method'); //Get the selected payment method
if( $payment_method == $chosen_payment_method ){
$label_text = __( "Bitcoin Discount" );
// Calculating percentage
$discount = number_format(($cart_total / 100) * $percent, 2);
// Adding the discount
$cart_object->add_fee( $label_text, -$discount, false );
}
}
add_action( 'woocommerce_review_order_before_payment', 'ts_refresh_payment_method' );
function ts_refresh_payment_method(){
// jQuery
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment