implement a 5% discount for customers who select the “Direct bank transfer”
-
Hi.
so what i want is when someone select Direct bank transfer, it gives 5% discount and if someone select other than direct bank transfer option he should get 0% discount. how can i implement this in WooCommerce wordpress.
This is the code (generated from ChatGPT, but this code not seem to be working. i tried some plugin as well, they are also not working by this rule. is their some setting in woo plugin which needs to be enabled/disabled…?! what am i missing something. i click on direct bank transfer, cash delivery, nothing happens in order summary, checkout page. same pricing. no discount applied.
Hope someone help me out.
Thank you
// Apply a 5% discount for Direct Bank Transfer payment method in WooCommerce
add_action('woocommerce_cart_calculate_fees', 'apply_discount_for_bank_transfer');
function apply_discount_for_bank_transfer($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
// Check if the selected payment method is Direct Bank Transfer
$chosen_gateway = WC()->session->get('chosen_payment_method');
if ($chosen_gateway == 'bacs') { // 'bacs' is the ID for Direct Bank Transfer
$discount = $cart->get_subtotal() * 0.05; // 5% discount
$cart->add_fee(__('Bank Transfer Discount', 'woocommerce'), -$discount);
}
}
// Ensure the discount is updated when the payment method is changed
add_action('wp_footer', 'refresh_cart_on_payment_method_change');
function refresh_cart_on_payment_method_change() {
if (is_checkout()) {
?>
<script type="text/javascript">
jQuery(function($) {
$('form.checkout').on('change', 'input[name^="payment_method"]', function() {
$(document.body).trigger('update_checkout');
});
});
</script>
<?php
}
}The page I need help with: [log in to see the link]
- You must be logged in to reply to this topic.