Created
September 5, 2024 12:56
-
-
Save a1iraxa/2250a59d2a46413d26324bce4634f963 to your computer and use it in GitHub Desktop.
Wordpress WooCommerce Filter for woocommerce_cart_calculate_fees
This file contains 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_cart_calculate_fees', 'raza_fee_based_on_payment_method_and_total' ); | |
function raza_fee_based_on_payment_method_and_total( $cart ) { | |
if ( is_admin() && ! defined('DOING_AJAX') ){ | |
return; | |
} | |
$threshold_amount = 300; // Total amount to reach for no fees | |
$payment_method_id = WC()->session->get('chosen_payment_method'); | |
$cart_items_total = $cart->get_cart_contents_total(); | |
$fee = ( $payment_method_id === 'cod' ) ? 0 : 360; | |
$text = __("Fee"); | |
$cart->add_fee( $text, $fee, false ); // To make fee taxable change "false" to "true" | |
} | |
// jQuery - Update checkout on payment method change | |
add_action( 'wp_footer', 'raza_checkout_script' ); | |
function raza_checkout_script() { | |
if ( is_checkout() && ! is_wc_endpoint_url() ) : ?> | |
<script type="text/javascript"> | |
console.log('working...'); | |
jQuery( function($){ $('form.checkout').on('change', 'input[name="payment_method"]', function(){ $(document.body).trigger('update_checkout'); }); }); | |
</script> | |
<?php endif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment