0

By the following code I'm able to add 3% Additional fees on user select Payment Method COD.

<?php
add_action('woocommerce_cart_calculate_fees', 'wcc_apply_cod_payment_gateway_fee');
function wcc_apply_cod_payment_gateway_fee(){
    
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }
 
    $chosen_payment_method = WC()->session->get('chosen_payment_method');
    if ($chosen_payment_method == 'cod') {
        $cart_total = WC()->cart->cart_contents_total;
        $amount = ($cart_total * 3)/100;
        $amount = '+'.$amount;
        
        $label = __( 'Cod 3% Fee', 'txtdomain' );
        WC()->cart->add_fee( $label, $amount, true, 'standard' );
    }
}

With this code Additional Fees message showing on Cart and Checkout both pages and I want to display it only on the WooCommerce Checkout page and not on The Cart page.

ak digital
  • 11
  • 1
  • Part of your code is missing, a piece of jQuery that ensures that an update takes place when a different payment method is chosen. Furthermore, it is just a matter of adding a [conditional tag](https://woocommerce.com/document/conditional-tags/), `if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return`. So how it should be done, can be found in the [duplicate question](https://stackoverflow.com/a/50704247/11987538) – 7uc1f3r Aug 05 '22 at 07:01

1 Answers1

0

you can use this code for it.

add_action('woocommerce_cart_calculate_fees', 'wcc_apply_cod_payment_gateway_fee');
function wcc_apply_cod_payment_gateway_fee(){
    
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }
   if ( !is_page( 'cart' ) || !is_cart() ) {
    $chosen_payment_method = WC()->session->get('chosen_payment_method');
    if ($chosen_payment_method == 'cod') {
        $cart_total = WC()->cart->cart_contents_total;
        $amount = ($cart_total * 3)/100;
        $amount = '+'.$amount;
        
        $label = __( 'Cod 3% Fee', 'txtdomain' );
        WC()->cart->add_fee( $label, $amount, true, 'standard' );
    }
 }
}
Ashok kumawat
  • 492
  • 3
  • 15