I need to apply an additional fee when a customer can place an order with free shipping, but wants to select COD payment. So, Free Shipping + COD payment => fee.
I tried unsuccessfully the following piece of code. Where am I wrong?
add_action( 'woocommerce_cart_calculate_fees','cod_fee' );
function cod_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$chosen_gateway = WC()->session->chosen_payment_method;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
$fee = 19;
if ( $chosen_shipping == 'free_shipping' && $chosen_gateway == 'cod' ) {
WC()->cart->add_fee( 'Spese per pagamento alla consegna', $fee, false, '' );
}
}
There is a mistake in your code and some additional code is needed. Try the following code that will add a specific fee when chosen payment method is Cash on delivery (cod) and when chosen shipping methods is "Free shipping":
Code goes in functions.php file of your active child theme (or active theme). tested and works.