WooCommerce

Add an additional charges for cash on delivery payment method (cod) in Woocommerce.

We can add an additional charge for only cash on delivery (COD) in woocommerce shopping cart website.

Please add the below function code in your theme folder function.php file (we recommend you to use child theme).
In this code i have added 25 as Handling Charges( ‘$fee = 25;’ ) you can change this.

 

// Add a custom fee based o cart subtotal
add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee', 10, 1 );
function custom_handling_fee ( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
 
    if ( 'cod' === WC()->session->get('chosen_payment_method') ) {
        $fee = 25;
        $cart->add_fee( 'Handling Charges', $fee, true );
    }
}
 
// jQuery - Update checkout on methode payment change
add_action( 'wp_footer', 'custom_checkout_jqscript' );
function custom_checkout_jqscript() {
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('form.checkout').on('change', 'input[name="payment_method"]', function(){
            $(document.body).trigger('update_checkout');
        });
    });
    </script>
    <?php
    endif;
}