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

For COD: You can add an additional charge for only cash on delivery (COD) in woocommerce shopping website. Please add the code in your theme at function.php file. In this code there has added 50 rupees as Handling Charges ( ‘$fee = 50;’ ) you can change this.

Here is the Code

// Add a custom fee based on 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 = 50;
        $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;
}

For Pre-Paid: You can give an additional discount for Online Payment in woocommerce shopping website. Please add the code in your theme at function.php file. In this code there has discounted 50 rupees Pre-Paid ( ‘$fee = -50;’ ) you can change this. (If you are using razorpay payment getway, then this is for you.) For another payment getway, only the method will be changed.

Here is the Code:

// Add a custom discount based on 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 ( 'razorpay' === WC()->session->get('chosen_payment_method') ) {
        $fee = -50;
        $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;
}
  • Go to theme editor in wordpress
  • Choose Child-theme then find edit function.php file
  • Copy and Paste the code under the file & save this