skip to Main Content

I would like to know how I can modify the code so that all transactions have a percentage surcharge and exclude France?

 * Add a standard $ value surcharge to all transactions in cart / checkout
 */
add_action( 'woocommerce_cart_calculate_fees','wc_add_surcharge' ); 
function wc_add_surcharge() { 
global $woocommerce; 

if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
return;

$county = array('US');
// change the $fee to set the surcharge to a value to suit
$fee = 1.00;

if ( in_array( WC()->customer->get_shipping_country(), $county ) ) : 
    $woocommerce->cart->add_fee( 'Surcharge', $fee, true, 'standard' );  
endif;
}

I found the code on the woocommerce site

Thank you for your response.

3

Answers


  1. Chosen as BEST ANSWER

    Overload fees are fixed, ideally there should be price tiers

    Example from 01€ to 55€ =0.35€ 56 to 150€ = 1.00€ ...

    In any case the code works on my site and excludes France.


  2. Something like this based off how I am reading the code at least. Can’t really test it but it seemed simple enaugh.

    * Add a standard $ value surcharge to all transactions in cart / checkout
     */
    add_action( 'woocommerce_cart_calculate_fees','wc_add_surcharge' ); 
    
    function wc_add_surcharge() { 
        global $woocommerce; 
    
        if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
    
        $county = ['FR']; //france
        // change the $fee to set the surcharge to a value to suit
        $fee = 1.00;
    
        if ( !in_array( WC()->customer->get_shipping_country(), $county ) ){  //add the ! to exclude only countries in the list
            $woocommerce->cart->add_fee( 'Surcharge', $fee, true, 'standard' );  
        }
    }
    
    Login or Signup to reply.
  3. I got what I wanted by reworking the code, is it gives this:

    //Ajouter un supplément à votre paiement en fonction du pays de livraison exclu france
    add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' ); 
    function woocommerce_custom_surcharge() { 
        global $woocommerce; 
    
        if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
    
        $county = ['FR']; //france
        // change the $fee to set the surcharge to a value to suit
        
        $percentage = 0.025;
        if ( !in_array( WC()->customer->get_shipping_country(), $county ) ){
            $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;    
            $woocommerce->cart->add_fee( 'Frais de transaction', $surcharge, true, '' );
        }
    }
    

    All that’s left is to change the following country for those they don’t want to put charges on their country!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search