Created
April 17, 2024 13:27
-
-
Save giacomolanzi/6ae800935648b21afb3b07afd36ca94e to your computer and use it in GitHub Desktop.
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_action('woocommerce_checkout_init', 'payment_method_change_trigger_update_checkout_js'); | |
function payment_method_change_trigger_update_checkout_js() | |
{ | |
wc_enqueue_js("$('form.checkout').on( 'change', 'input[name=payment_method]', function(){ | |
$(document.body).trigger('update_checkout'); | |
});"); | |
} | |
add_filter('woocommerce_cart_calculate_fees', 'discount_based_on_payment_method', 10, 1); | |
function discount_based_on_payment_method($cart) | |
{ | |
if (is_admin() && !defined('DOING_AJAX')) | |
return; | |
if (!(is_checkout() && !is_wc_endpoint_url())) | |
return; | |
$targeted_payment_method = 'bacs'; // Here define the desired payment method | |
if (WC()->session->get('chosen_payment_method') === $targeted_payment_method) { | |
$discount = $cart->subtotal * 0.10; // 10% discount | |
$cart->add_fee('Sconto bonifico', -$discount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment