• SoniaGM

    (@soniagm)


    I’m trying to add a fee for the cod payment method.
    The problem is that when the user select the payment method during checkout, it is not updated, so the extra fee is added for all payment methods.

    Anybody knows how to update the payment method?

    Any help will be appreciated.

    Thanks.

    <?php
     add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
    function woocommerce_custom_surcharge() {
      wp_enqueue_script( 'wc-add-extra-charges', get_stylesheet_directory_uri() . '/app.js', array('wc-checkout'), false, true );
      global $woocommerce;
    
    	if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    		return;
    
    	$available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways();
        $current_gateway = '';
        if ( ! empty( $available_gateways ) ) {
               // Chosen Method
            if ( isset( $woocommerce->session->chosen_payment_method ) && isset( $available_gateways[ $woocommerce->session->chosen_payment_method ] ) ) {
                $current_gateway = $available_gateways[ $woocommerce->session->chosen_payment_method ];
            } elseif ( isset( $available_gateways[ get_option( 'woocommerce_default_gateway' ) ] ) ) {
                $current_gateway = $available_gateways[ get_option( 'woocommerce_default_gateway' ) ];
            } else {
                $current_gateway =  current( $available_gateways );
    
            }
    
        }
    
                $current_gateway_id = $current_gateway -> id;
    		//echo $current_gateway_id;
    		$payment_g = 'cod';	
    
    	if ($current_gateway_id == $payment_g)	{
    	//echo "yes";
            $percentage 	= 0.01;
     	$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
    	$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, 'standard' );
    	}
    
    }
    ?>
  • The topic ‘Woocommerce Add extra fee cost in delivery’ is closed to new replies.