I'm trying to add an additional fee to the COD payment method.
The below code is working fine but only when I'm logetin as a administrator.
If I logout the fee doesn't show in the front end and I don't understand why.
For some reason the code is working only if I'm logget in as a administrator.
// add addition fee to cod payment method
if ( ICL_LANGUAGE_CODE=='cs' ){
add_action( 'woocommerce_cart_calculate_fees','cod_fee' );
function cod_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// get your payment method
$chosen_gateway = WC()->session->chosen_payment_method;
//echo $chosen_gateway;
$fee = 17;
if ( $chosen_gateway == 'cod' ) { //test with cash on delivery method
WC()->cart->add_fee( 'Delivery fee', $fee, false, '' );
}
}
}
// jQuery - Update checkout on payment method change
add_action( 'woocommerce_checkout_init', 'payment_methods_refresh_checkout' );
function payment_methods_refresh_checkout() {
wc_enqueue_js( "jQuery( function($){
$('form.checkout').on('change', 'input[name=payment_method]', function(){
$(document.body).trigger('update_checkout');
});
});");
}