Skip to content

Instantly share code, notes, and snippets.

@thierrypigot
Created October 7, 2022 13:49
Show Gist options
  • Save thierrypigot/327f9a5f67ed131c696f415cc76a9520 to your computer and use it in GitHub Desktop.
Save thierrypigot/327f9a5f67ed131c696f415cc76a9520 to your computer and use it in GitHub Desktop.
WooCommerce - Add a surcharge based on the delivery country (exclude France)
<?php
/**
* Add a 1% surcharge to your cart / checkout based on delivery country
* Taxes, shipping costs and order subtotal are all included in the surcharge amount
*/
add_action( 'woocommerce_cart_calculate_fees',' woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$county = array('FR');
$percentage = 0.01;
if ( !in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
endif;
}
@boutiquepcland
Copy link

Pour les pourcentages et exclure la France, j'ai réalisé le code suivant :

//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, '' );
	}
}

Changer "0,025" par le pourcentage que vous voulez !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment